0

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?

  • The problem you have is not with the Arduino. You need to debug the code on you PC and rather than squirting it down the serial port write it to the console. Once you have it all working then try going multi platform, where you will probably find its something to do with the bit order, or summut. – Code Gorilla Nov 18 '16 at 13:01
1

As @Matt said,

The problem you have is not with the Arduino

Put if(port.available()>0) in the timer, or as event instead of void draw(). Furthermore, I can't found the calling of draw in your code.

  • In processing 'draw()' is equal to the Arduino 'loop()' function, afaik. – Paul Dec 6 '16 at 13:45
0

What do you see if you write(port.avaliable) and write(data) in processing? Are those data valid? If no, the problem is in com port or whatever

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.