Created
October 16, 2015 19:38
-
-
Save andyinabox/e853ba4628a5e4d5107d to your computer and use it in GitHub Desktop.
Serial Communication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Arduino code for serial communication | |
// set our sensor pin | |
int sensorPin = A1; | |
// set our sensor value | |
int sensorVal; | |
void setup() { | |
pinMode(sensorPin, INPUT); | |
Serial.begin(9600); | |
// because this contains a `while` loop | |
// `setup` will not complete until we've | |
// connected | |
establishContact(); | |
} | |
void loop() { | |
// better safe than sorry, only when we | |
// know that there's a connection available | |
if(Serial.available() > 0) { | |
// now read the analog pin | |
sensorVal = analogRead(sensorPin); | |
// return the sensor value using the | |
// serial connection | |
Serial.write(sensorVal); | |
// hold your horses | |
delay(10); | |
} | |
} | |
// we use this function to initialize | |
// contact ("handshake") with processing | |
void establishContact() { | |
// keep trying until we've established | |
// a connection | |
while(Serial.available() <= 0) { | |
// keep sending our handshake message | |
// while we're waiting | |
Serial.println("A"); | |
// add a bit of a delay to be friendly | |
delay(300); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Processing code for serial communication | |
// bring in the serial library | |
import processing.serial.*; | |
// variable for our serial communication port | |
Serial myPort; | |
int portIndex; | |
// this indicates whether we've made contact | |
// with the arduino | |
boolean firstContact = false; | |
// will store the sensor value when we're recieving data | |
int sensorVal; | |
void setup() { | |
// setting my sketch size | |
size(500, 500); | |
// print a list of available serial ports | |
printArray(Serial.list()); | |
// on my computer, arduino is connected to the 7th port | |
portIndex = 7; | |
// setup the serial port with the correct port number | |
myPort = new Serial(this, Serial.list()[portIndex], 9600); | |
} | |
void draw() { | |
// clear the background to BLACK | |
background(0); | |
// set styles | |
noStroke(); | |
fill(255); | |
// use the sensor input to draw a circle! | |
ellipse(width/2, height/2, sensorVal, sensorVal); | |
} | |
// this function is called whenever data is recieved | |
// from the serial port | |
void serialEvent(Serial myPort) { | |
// read the incoming byte into a variable | |
int inByte = myPort.read(); | |
// if we haven't yet made contact yet | |
if(firstContact == false) { | |
// is this the phone call we've been waiting for? | |
if(inByte == 'A') { | |
// we've made contact! | |
firstContact = true; | |
// empty the serial buffer | |
myPort.clear(); | |
// yes! hello! | |
myPort.write('A'); | |
} | |
} else { | |
// helpful for debugging to keep a transcript | |
println(inByte); | |
// store in our global variable | |
sensorVal = inByte; | |
// keep the connection | |
myPort.write('A'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment