Advertisement
MrRockchip

blinker_part2560

Nov 9th, 2021
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. String Serial_String = "";
  2.  
  3. // Initial setup
  4. void setup() {
  5.   Serial.begin(9600); // Initialize the debug serial port
  6.  /*
  7.   * It is recommended to use the lower baud rate. The higher the baud rate,
  8.   * the higher the bit error rate will be, and the control action might fail.
  9.   */
  10.   Serial3.begin(4800); // Initialize the ESP8266 <=> MEGA2560 serial port
  11.   pinMode(LED_BUILTIN, OUTPUT); // Initialize the digital pin LED_BUILTIN as an output
  12. }
  13.  
  14. // Read a command from ESP8266 over serial
  15. void read_string() {
  16.   while (Serial3.available() > 0) {
  17.     Serial_String += (char)(Serial3.read());
  18.     delay(2);
  19.   }
  20. }
  21.  
  22. // The loop function runs over and over again forever
  23. void loop() {
  24.   if (Serial3.available() > 0) {
  25.     Serial_String = "";
  26.     read_string();
  27.     Serial.println(Serial_String);
  28.   }
  29.   if (Serial_String == "1")
  30.     digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  31.   if (Serial_String == "0")
  32.     digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement