I have written a processing code to work along with my arduino code for the arduino UNO. In this I'm trying to control the colour of the processing window by the arrow keys. However, I'm doing this in a bit roundabout manner because I wanted to see how the 2 codes work together to exchange information.
The processing code:
import processing.serial.*;
Serial port;
int data=0;
char c;
void setup()
{
port=new Serial(this,"COM3",9600);
background(0,0,0);
size(500,500);
}
void draw()
{
if(port.available()>0)
{
data=port.read();
c=(char)data;
if(c=='U')
background(255,0,0);
if(c=='D')
background(0,255,0);
if(c=='L')
background(0,0,255);
if(c=='R')
background(255,255,0);
}//end of if
}
void keyPressed()
{
if(key==CODED)
{
if(keyCode==UP)
port.write('u');
if(keyCode==DOWN)
port.write("d");
if(keyCode==LEFT)
port.write("l");
if(keyCode==RIGHT)
port.write("r");
}
}
The arduino code:
// Arrow key controller
char data;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while(Serial.available()>0)
{
data=Serial.read();
if(data=='u')
Serial.print("U");
else if(data=='d')
Serial.print("D");
else if(data=='l')
Serial.print("L");
else if(data=='r')
Serial.print("R");
}
}
Here what Im doing is, i'm reading the arrow key press from processing. The correspoding small case letter is printed into the serial buffer, eg u for UP. The arduino code reads this letter(u) from the serial buffer and prints the corresponding upper case letter(U) into the serial buffer. The processing code then reads the uppercase letter from the buffer and changes the colour accordingly. But nothings happening when i push the arrow keys. What's wrong?