Skip to content

Instantly share code, notes, and snippets.

@shohei
Created February 10, 2012 04:11
Show Gist options
  • Save shohei/1786528 to your computer and use it in GitHub Desktop.
Save shohei/1786528 to your computer and use it in GitHub Desktop.
Processing Arduino Serial communication test
Youtube movie
http://www.youtube.com/watch?v=hCfSdrqmkRw
require controlP5
http://www.sojamo.de/libraries/controlP5/
//Reading value
int val=0;
void setup(){
//Start serial communication
Serial.begin(9600);
}
void loop(){
//if data exists
if(Serial.available()>0){
//read the data
val=Serial.read();
//Send signal data (1 byte)
Serial.write(65);
}
//Set read value into analog output (No.11 pin)
analogWrite(11,val);
}
//Import serial library
import processing.serial.*;
Serial port;
import controlP5.*;
ControlP5 controlP5;
//Value for X coordinate
int x=0;
void setup(){
size(255,100);
//Configuration of the serial port
//**Change this value according to your environment**
port=new Serial(this,"/dev/tty.usbserial-A60049U2",9600);
controlP5 = new ControlP5(this);
controlP5.addSlider("LED",0,255,128,10,10,200,30);
}
void draw(){
background(100);
}
void serialEvent(Serial p){
//if data exists
if(port.available()>0){
//Send X coordinate
port.write(x);
//Read the signal data from Arduino
//and empty the buffer
port.read();
}
}
public void LED(int theValue){
x = theValue;
}
void keyPressed(){
//When "S" key is pushed,
if(key=='s'){
//Send the data for starting communication
port.write(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment