/* Connecting Arduino to Processing https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/to-processing 3. Shaking Hands - when both A and P are both sending and receiving data */ #include #define RX 0 #define TX 1 SoftwareSerial mySerial(RX, TX); char val; // Data received from the serial port int ledPin = 8; // boolean ledState = LOW; //to toggle our LED void setup() { pinMode(ledPin, OUTPUT); // Set pin as OUTPUT mySerial.begin(9600); //initialize serial communications at 9600 baud rate establishContact(); // send a byte to establish contact until receiver responds } void loop() { if (mySerial.available() > 0) { // If data is available to read, val = mySerial.read(); // read it and store it in val if(val == '1') //if we get a 1 { ledState = !ledState; //flip the ledState digitalWrite(ledPin, ledState); } delay(100); } else { mySerial.println("Hello, world!"); //send back a hello world delay(100); } //Serial.println("dO S"); //delay(100); } /* This function just sends out a string to see if it hears anything back - indicating that Processing is ready to receive data. */ void establishContact() { while (mySerial.available() <= 0) { mySerial.println("A"); // send a capital A delay(300); } }