Wednesday 20 January 2010

The code so far...

Here is the Arduino & Processing code for the system below:

Arduino:

int potpinx = 0; //analog input
int potpiny = 1; //analog input
int potpinzoom = 2; //analog input
int onoff = 3; //digital input
int scroll = 5; //analog input
int onoffled = 13; // digital output

int sensorValue = 0; //analog value
int onoffvalue = 0; //digital value

void setup() {
// configure the serial connection:
Serial.begin(9600);
// configure the digital input
pinMode(onoff, INPUT);
// configure the digital output
pinMode(onoffled, OUTPUT);
}

void loop() {
// read the sensor:
sensorValue = analogRead(potpinx);
// print the results:
Serial.print(sensorValue, DEC);
Serial.print(",");

// read the sensor:
sensorValue = analogRead(potpiny);
// print the results:
Serial.print(sensorValue, DEC);
Serial.print(",");

// read the sensor:
sensorValue = analogRead(potpinzoom);
// print the results:
Serial.print(sensorValue, DEC);
Serial.print(",");

// read the sensor:
sensorValue = digitalRead(onoff);
Serial.print(sensorValue, DEC);
Serial.print(",");

//read the sensor:
sensorValue = analogRead(scroll);
Serial.println(sensorValue/255, DEC);

delay(90);

//read the onoff switch again & assign the output as a different name
onoffvalue = digitalRead(onoff);

//if the digital switch is in the on position, turn on the led
if (onoffvalue == HIGH){
digitalWrite(onoffled, HIGH);
}
else{
digitalWrite(onoffled, LOW);
}
}


Processing:

import processing.video.*; // import theProcessing video library
import processing.serial.*; // import the Processing serial library
Serial Arduino; // The serial port
Capture webCam; // The webcam feed
PImage hide; //Declare variable "hide" of type PImage

float xpos, ypos, zoom, onoff; // Starting position of the webcam feed

void setup() {
size(640,480,P3D);

// List all the available serial ports & cameras
//println(Serial.list());
//println(Capture.list());

webCam = new Capture(this, width/2, height/2, "USB PC Camera-WDM", 30);
Arduino = new Serial(this, Serial.list()[1], 9600);

// read bytes into a buffer until you get a linefeed (ASCII 10):
Arduino.bufferUntil('\n');

}

void draw() {
background(153, 153, 0);
scale(zoom/100);
if (webCam.available()){
webCam.read();
}
if (onoff == 1){
image(webCam, xpos, ypos);
}
}

void serialEvent(Serial Arduino) {
// read the serial buffer:
String myString = Arduino.readStringUntil('\n');
// if you got any bytes other than the linefeed:
if (myString != null) {

myString = trim(myString);

// split the string at the commas
// and convert the sections into integers:
int sensor[] = int(split(myString, ','));

// print out the values you got:
for (int sensorNum = 0; sensorNum <>
print("sensor " + sensorNum + ": " + sensor[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
if (sensor.length > 1) {
xpos = map(sensor[0], 0,width,0,width);
ypos = map(sensor[1], 0,height,0,height);
zoom = map(sensor[2], 1, 1023, 1, 1023);
onoff = map(sensor[3], 0, 1, 0, 1);
}
}
}


No comments:

Post a Comment