Category : General Programming Toggle

Arudino to Processing  : Serial Communication

Arudino to Processing : Serial Communication

Arduino Code

void setup()
{
//initialize serial communications at a 9600 baud rate
Serial.begin(9600);
}

void loop()
{
//send ‘Hello, world!’ over the serial port
Serial.println(“Hello, world!”);
//wait 100 milliseconds so we don’t drive ourselves crazy
delay(100);
}


 

Processing Code

import processing.serial.*;

Serial myPort; // The serial port

void setup () {
size(200, 200);
println(Serial.list());
myPort = new Serial(this, Serial.list()[2], 9600);
myPort.bufferUntil(‘\n’);
}

void draw () {
// everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = new String(myPort.readBytesUntil(‘\n’));

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
}
println(inString);
}

ShutDown