Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const char start_char = '$';
- const char stop_char = ';';
- String serial_buff = "";
- byte enable_rec = 0;
- void setup(){
- Serial.begin(9600);
- }
- void loop(){
- if(Serial.available()){
- char c = Serial.read();
- if(c == start_char){
- if(enable_rec == 0) enable_rec = 1;
- else if(enable_rec == 1) serial_buff = ""; //missing stop char
- }
- else if(c == stop_char){
- process_rec_data(); //use received command
- enable_rec = 0; //disable receiving
- serial_buff = ""; //clear buffer
- }
- else{
- serial_buff += c;
- }
- }
- }
- void process_rec_data(){
- Serial.println("Received command:>" + serial_buff + "<");
- if(serial_buff == "on"){
- //turn x on
- }
- else if(serial_buff == "off"){
- //turn x off
- }
- }
- void sendCommand(String command){
- Serial.print(start_char + command + stop_char);
- }
Advertisement
Add Comment
Please, Sign In to add comment