LEDsensor from Arduino to Processing

I found a great script for using a LED as photovaltic sensor. Its here below. I have tried to translate that into processing, but something seems to go wrong.

As you can tell I’m pretty new at this.

// ARDUINO

//TOUCH SENSING BETA - LED TURNS ON WHEN LIGHT IS PRESENT
//BY: RICARDO DE LEMOS 1/17/2007
int led1 = 13;
int cath = 2; // negative
int ando = 3; // positive

void setup()
{
pinMode(led1, OUTPUT);
pinMode(cath, OUTPUT);
pinMode(ando, OUTPUT);
Serial.begin(9600);
}

void loop()
{
//REVERSE BIAS
digitalWrite(cath, HIGH);
digitalWrite(ando, LOW);
//CHARGE LED
delay(6);
//READ LED CAP
pinMode(ando, INPUT);
delay(6);
//WRITE TO LED
if (digitalRead(ando) == LOW)
{
digitalWrite(led1,HIGH);
}
else
{
digitalWrite(led1,LOW);
}
//RESET
pinMode(ando, OUTPUT);
}

And the translated version for Processing.

//Processing
//biblio
import processing.serial.;
import cc.arduino.
;
Arduino arduino;

//variabler
int turnOnLED = 13;
int sensorP = 3;
int sensorN = 2;
//setups
void setup(){
size (500,500);
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0], 9600);
arduino.pinMode(turnOnLED, Arduino.OUTPUT);
arduino.pinMode(sensorP, Arduino.OUTPUT);
arduino.pinMode(sensorN, Arduino.OUTPUT);
}
//drawing
void draw(){
//charging LED
arduino.pinMode(sensorP, Arduino.OUTPUT);
arduino.digitalWrite(sensorN, Arduino.LOW);
arduino.digitalWrite(sensorP, Arduino.HIGH);
delay(6);
arduino.pinMode(sensorP, Arduino.INPUT);
if(arduino.digitalRead(sensorP) == Arduino.LOW){
background(255);
}
println((arduino.digitalRead(sensorP)));
println((arduino.digitalRead(sensorN)));
}

So what I wanna do is just to make Processing tell me the amount of light there is in my room and react whenever.
Hope somebody sees something I dont. Cheers. :-/

Okay, this is where I am so far. Is there anybody out there that has a clue why the results come back so unstable. The LED reckognizes the ligth, it works so to speak, but only half of the results are true. Right now Im thinking that LEDs just sucks as sensors.... Hope there are better suggestions out there.

import processing.serial.; import cc.arduino.; Arduino arduino; int turnOnLED = 13; int sensorP = 3; int sensorN = 2; int x = 0; void setup(){ size (1200,500); println(Arduino.list()); arduino = new Arduino(this, Arduino.list()[0], 115200); arduino.pinMode(turnOnLED, Arduino.OUTPUT); arduino.pinMode(sensorP, Arduino.OUTPUT); arduino.pinMode(sensorN, Arduino.OUTPUT); } void draw(){ arduino.pinMode(sensorP, Arduino.OUTPUT); arduino.digitalWrite(sensorN, Arduino.HIGH); arduino.digitalWrite(sensorP, Arduino.LOW); delay(2); arduino.pinMode(sensorP, Arduino.INPUT); delay(2); println(arduino.digitalRead(sensorP)); int y = arduino.digitalRead(sensorP); noStroke(); fill(0); rect(x, (y*250)+125, 2, 2); x = x+1; if(x>width){x=0; background(125);} if(arduino.digitalRead(sensorP) != 0){ arduino.digitalWrite(turnOnLED, Arduino.LOW); } else{arduino.digitalWrite(turnOnLED, Arduino.HIGH);} }

The reason is that the original program is very time sensitive and you’ve ruined that by trying to control it all remotely with Processing over a serial link. Can’t you just have your Processing program receive sensor values output by the original Arduino program?

Thanks a LOT. Works like a charm. It spits out nothing but nice clean numbers. Now I just have to figure out how to get some decent analogRead's.

Cheers again.

/*

ARDUINO

//TOUCH SENSING BETA - LED TURNS ON WHEN LIGHT IS PRESENT //BY: RICARDO DE LEMOS 1/17/2007 int led1 = 13; int cath = 2; // negative int ando = 3; // positive int analog0 = 0;

void setup() { pinMode(led1, OUTPUT); pinMode(cath, OUTPUT); pinMode(ando, OUTPUT); Serial.begin(9600); }

void loop() { //REVERSE BIAS digitalWrite(cath, HIGH); digitalWrite(ando, LOW); //CHARGE LED delay(6); //READ LED CAP pinMode(ando, INPUT); delay(6); //WRITE TO LED if (digitalRead(ando) == LOW) { digitalWrite(led1,HIGH); } else { digitalWrite(led1,LOW); } int analogValue = digitalRead(ando); Serial.print(analogValue, BYTE); // print as a raw byte value //RESET pinMode(ando, OUTPUT); } */

import processing.serial.*; int val = 0; int x = 0; Serial port; void setup() { size(1000, 300); println(Serial.list()); port = new Serial(this, Serial.list()[0] , 9600); } void draw() { while (port.available() > 0) { val = port.read(); println(val); }

rect(x, val*250, 2, 2); x = x+1; if (x>width){x=0;} }

Hi, See:- http://www.thebox.myzen.co.uk/Workshop/LED_Sensing.html