Hello,
I am assuming your hardware works and you can see output on the serial monitor.
Do not use serial monitor when serial is communicating with Processing.
Moving on…
I simplified your code so you can see what you are receiving on Processing.
Arduino
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println('1'); //write ASCII 1 to the monitor
Serial.println();
delay(1000);
Serial.println('2'); //write ASCII 2 to the monitor
Serial.println();
delay(1000);
}
Processing
import processing.serial.*;
Serial myPort;
int note;
void setup()
{
size(400,400);
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[2],9600);
}
void draw()
{
while(myPort.available()>0)
{
note = myPort.read();
println(note);
if(note == 1)
{
background(255, 0, 0);
println("RED");
}
else if(note == 2)
{
background(0, 255, 0);
println("GREEN");
}
}
}
You can see that you are sending extra ASCII characters that are NOT EQUAL to a number (1, 2, 3, etc.)
Consider using:
if(note == '1'); // compares to ASCII 1
println() is also sending ASCII 10 (LF Linfe Feed) and 13 (CR Carriage Return)
And you are sending this twice!
Do some more exploration on this…
You may even what to consider just sending the value with Serial.write()
That is enough to get you started.
References:
- Serial \ Libraries \ Processing.org
- ASCII
- Electronics \ Processing.org
- And lots more out there on the subject…
:)