I had a lot of trouble trying to differentiate between input from different sensors in Processing. I tried three different ways which all failed by either causing random java exceptions and just not printing out code anymore (for no reason) or not taking input in correctly. You can see remnants of these attempts commented out in my code.
Ideally I wanted one pot to control a moving spaceship, the FSR to control firing bullets, and there would be enemy spaceship in front of you like a simplified version of space invaders.
With all of my issues trying to recognize input, I wasn't able to do this, so all I ended up with was the photocell controlling the size of the circles on the screen. In almost darkness this gives an "outerspace"-like feel.
Hopefully I can return to this and fix it to the way I want before our next lab is due.
/*
* Resistive Sensor Input
* Takes the input from a resistive sensor, e.g., FSR or photocell
* Dims the LED accordingly, and sends the value (0-255) to the serial port
* Sends the source of input to the serial port as well
* F - FSR, C - photocell, P - pot (e.g. F125)
*/
int sensorPin1 = 1; // select the input pin for the sensor (F - FSR)
int sensorPin2 = 0; // select the input pin for teh sensor (C - photocell)
int sensorPin3 = 2; // select the input pin for the sensor (P - pot)
int val1 = 0; // variable to store the value coming from the sensor
int val2 = 0; // variable to store the value coming from the sensor
int val3 = 0; // variable to store the value coming from the sensor
//int ledPin = 11; // select the output pin for the LED
void setup() {
Serial.begin(9600);
}
void loop() {
val1 = analogRead(sensorPin1); // read the value from the sensor, 0-1023
analogWrite(11, val1/4); // dimming the BLUE LED can be between 0-255
val2 = analogRead(sensorPin2); // read the value from the sensor, 0-1023
analogWrite(10, val2/4); // dimming the GREEN LED can be between 0-255
//val3 = analogRead(sensorPin3); // read the value from the sensor, 0-1023
//analogWrite(9, val3/4); // dimming the RED LED can be between 0-255
//Serial.print('F');
//Serial.println(val1/4 + 256); // writing the value to the PC via serial connection
// 256 - 511
//Serial.print('C');
Serial.println(val2/4); // writing the value to the PC via serial connection
//Serial.print('P');
//Serial.println(val3/4); // writing the value to the PC via serial connection
delay(50); // rest a little...
}
Processing Code
/*
* Arduino Ball Paint
* (Arduino Ball, modified)
* ----------------------
*
* Draw balls randomly on the screen, size controlled by a device
* on a serial port. Press space bar to clear screen, or any
* other key to generate fixed-size random balls.
*
* Receives an ASCII number over the serial port,
* terminated with a carriage return (ascii 13) then newline (10).
*
* This matches what Arduino's " Serial.println(val)" function
* puts out.
*
* Created 25 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbserial-A4001o19"; // or "COM5"
Serial port;
String buf="";
char flag;
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
void setup() {
size(800,600);
frameRate(10);
smooth();
background(40,40,40);
noStroke();
port = new Serial(this, portname, 9600);
}
void draw() {
}
void keyPressed() {
if(key == ' ') {
background(40,40,40); // erase screen
}
// if (key == 'f') {
//fill
else {
int x = int(random(0,width));
int y = int(random(0,height));
drawball(x,y, 50);
}
}
// draw balls
void drawball(int x, int y, int r) {
for (int i=0; i<100; i++ ) {
// fill(255-i,i,240);
fill(255, 255, 255);
ellipse(x,y,r,r);
}
}
// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf && c != cr) {
//if (c == 70)
//flag = 'F';
// else if (c == 67)
// flag = 'C';
//else if (c == 80)
//flag = 'P';
//else
buf += char(c);
}
// all input from serial has been read in
if (c == lf) {
// thanks to Shawna for point out charAt() and substring() functions
//String in = buf;
//int value = 0;
//char flag = in.charAt(0);
//String num = in.substring(1);
//int value = int(num);
//println("input flag = " + flag);
//println("input value = " + value);
int val = int(buf);
//if (val <= 255)
//flag = 'C';
//else if (val > 255)
//flag = 'F';
//if (flag == 'C') {
println("val="+val);
int x = int(random(0,width));
int y = int(random(0,height));
drawball(x,y,val);
buf = "";
// }
//if (flag == 'F' && val >= 384) {
//val -= 256;
//println("val="+val);
//background(40,40,40); // erase screen
//buf = "";
// }
}
}