I am working on a small project with Processing and Arduino, in which, depending upon the value of a photoresistor, it displays whether it is day/night/evening.
I read the value from the photoresistor and write the value to the serial port. Then in Processing I read the value from the serial port and display depending on the value.
But I get a very flickery output, and there is always a -1 imprinted on the screen as shown in the video.
Please help me out.
Code for Arduino:
int ldr1 = A0;
int x,y;
void setup() {
pinMode(ldr1,INPUT);
Serial.begin(9600);
}
void loop() {
x = analogRead(ldr1);
Serial.write(x);
delay(100);
}
Code for Processing:
import processing.serial.*;
Serial port;
int x,y;
void setup() {
size(640, 320);
port = new Serial(this, "COM3", 9600);
}
void draw() {
background(0);
x = port.read();
text(x,100,100);
if(x < 400 && x > 200) {
textSize(32);
fill(0,0,255);
text("it is evening",200,200);
}
if(x < 200 && x > 20) {
textSize(32);
fill(0,255,0);
text("it is night",200,200);
}
if(x > 400 && x < 900) {
textSize(32);
fill(255,0,0);
text("it is morning",200,200);
}
}