{\rtf1\ansi\ansicpg1252\cocoartf1671\cocoasubrtf500 {\fonttbl\f0\fswiss\fcharset0 Helvetica;} {\colortbl;\red255\green255\blue255;} {\*\expandedcolortbl;;} \paperw11900\paperh16840\margl1440\margr1440\vieww17700\viewh15040\viewkind0 \pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0 \f0\fs24 \cf0 /*\ 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 Serial(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\ Serial.begin(9600); //initialize serial communications at 9600 baud rate\ establishContact(); // send a byte to establish contact until receiver responds\ \}\ \ void loop() \{\ if (Serial.available() > 0) \{ // If data is available to read,\ val = Serial.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 \{\ Serial.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 (Serial.available() <= 0) \{\ Serial.println("A"); // send a capital A\ delay(300);\ \}\ \}\ }