evenjc

Raspberry PI and Arduino Communication [Arduino]

Mar 21st, 2019
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void setup() {
  2.   //Initiating serial communication and flushing serial port, just for good measure
  3.   Serial.begin(9600);
  4.   Serial.flush();
  5. }
  6.  
  7. byte expected_byte1 = 0x31;
  8. byte expected_byte2 = 0x32;
  9.  
  10. void loop() {
  11.   // Check if there is something on the serial port
  12.   if (Serial.available()) {
  13.     //read available data, save to recieved_data
  14.     int recieved_data = Serial.read();
  15.  
  16.     //does the recieved data match our expectations?
  17.     if (recieved_data == expected_byte1) {
  18.       Serial.print("You sent the number 1!");
  19.       delay(50);
  20.     }
  21.  
  22.     //Other expectation for concept purposes
  23.     if (recieved_data == expected_byte2) {
  24.       Serial.print("You sent the number 2!");
  25.       delay(50);
  26.     }
  27.   }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment