I am trying to read data from a photocell resistor and my Arduino Decimila, and then draw it in real time using processing.
It should be excruciatingly simple; but for me it turned into a nightmare for me.
code I run my Arduino:
int photoPin; void setup(){ photoPin = 0; Serial.begin( 9600 ); } void loop(){ int val = int( map( analogRead( photoPin ), 0, 1023, 0, 254 ) ); Serial.println( val );
code that is executed in processing:
import processing.serial.*; Serial photocell; int[] yvals; void setup(){ size( 300, 150 ); photocell = new Serial( this, Serial.list()[0], 9600 ); photocell.bufferUntil( 10 ); yvals = new int[width]; } void draw(){ background( 0 ); for( int i = 1; i < width; i++ ){ yvals[i - 1] = yvals[i]; } if( photocell.available() > 0 ){ yvals[width - 1] = photocell.read(); } for( int i = 1; i < width; i++ ){ stroke( #ff0000 ); line( i, yvals[i], i, height ); } println( photocell.read() );
I tested both bits of code separately and I know that they work. This only happens when I try to get input from Arduino to handle problems starting.
When I look at the data in the Arduino "Serial Monitor", I get a nice steady stream of data that looks correct.
But when I read the same data through Processing, I get a repeating pattern of random values.
Halp?
source share