Guest User

Untitled

a guest
Sep 11th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. const char start_char = '$';
  2. const char stop_char = ';';
  3.  
  4. String serial_buff = "";
  5. byte enable_rec = 0;
  6.  
  7.  
  8. void setup(){
  9.   Serial.begin(9600);
  10. }
  11.  
  12. void loop(){
  13.   if(Serial.available()){
  14.     char c = Serial.read();
  15.    
  16.     if(c == start_char){
  17.       if(enable_rec == 0) enable_rec = 1;
  18.       else if(enable_rec == 1) serial_buff = "";   //missing stop char
  19.     }
  20.     else if(c == stop_char){
  21.       process_rec_data();   //use received command
  22.       enable_rec = 0;   //disable receiving
  23.       serial_buff = "";   //clear buffer      
  24.     }
  25.     else{
  26.       serial_buff += c;
  27.     }
  28.   }
  29. }
  30.  
  31.  
  32.  
  33. void process_rec_data(){
  34.   Serial.println("Received command:>" + serial_buff + "<");
  35.   if(serial_buff == "on"){
  36.     //turn x on
  37.   }
  38.   else if(serial_buff == "off"){
  39.     //turn x off
  40.   }
  41. }
  42.  
  43. void sendCommand(String command){
  44.   Serial.print(start_char + command + stop_char);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment