Media Controller

Arduino Code:

void setup() {
Serial.begin( 9600 );
pinMode( 2, INPUT );
}
void loop() {
int accelX = analogRead( 0 );
int accelY = analogRead( 1 );
int switchState = digitalRead( 2 );
Serial.print(accelX, DEC);
Serial.print(“,”);
Serial.print(accelY, DEC);
Serial.print(“,”);
Serial.println(switchState, DEC);
}

Processing Code:

import processing.serial.*;Serial myPort;
int state = 0;float xpos, ypos;
void setup() { size(1200, 700); background(1); println(Serial.list()); myPort = new Serial(this, Serial.list()[0], 9600); myPort.bufferUntil(‘\n’);}
void draw() {
fill( 0, 100, 150 ); stroke( 255, 255, 0 ); if(state == 1) {   ellipse(xpos, ypos, 10, 10); }}
void serialEvent (Serial myPort) { String myString = myPort.readStringUntil(‘\n’);
if(myString != null) {   myString = trim(myString);   int sensors[] = int(split(myString, ‘,’));
for(int sensorNum = 0; sensorNum < sensors.length; sensorNum++)   {     print(“Sensor ” + sensorNum + “: ” + sensors[sensorNum] + “\t”);   }
println();
if(sensors.length > 1)   {     //print(“hello”);     xpos = map(sensors[0], 250, 375, 0, width);     ypos = map(sensors[1], 370, 250, 0, height);     state = sensors[2];   } }

}

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment