0

I have 3 LEDs connected to my arduino which are controlled via laptop key presses with processing.

While the LEDs are able to light up when the respective buttons are pressed, i am unable to light up all 3 LEDs simultaneously when i press all 3 keys at the same time. I am however able to light up 2 LEDs by pressing 2 buttons simultaneously.

All the LEDs light up when their respective buttons are pressed, so i know that it is not a defective LED and/or faulty connections/wiring at play but something else that is causing the issue.

My processing code:

import processing.serial.*;
Serial myPort;

void setup(){
  println("The available ports are: ");
  println(Serial.list());
  myPort = new Serial(this,Serial.list()[0],9600);
  myPort.buffer(64);
  size(500, 500);
}

void draw(){
  if(keyPressed){
    if(key == 'r' || key == 'R'){
      myPort.write(1);
      println("red");
    }
    else if(key == 'y' || key == 'Y'){
      myPort.write(2);
      println("yellow");
    }
    else if(key == 'g' || key == 'G'){
      myPort.write(3);
      println("green");
    }
  }

}

void keyReleased(){
 if(key == 'r' || key == 'R'){
      myPort.write(4);
      println("red released");
    }
    else if(key == 'y' || key == 'Y'){
      myPort.write(5);
      println("yellow released");
    }
    else if(key == 'g' || key == 'G'){
      myPort.write(6);
      println("green released");
    }

  println("released");
}

My Arduino code:

int red = 7;
int yellow = 6;
int green = 5;

void setup(){
  Serial.begin(9600);
  pinMode(red,OUTPUT);
   pinMode(yellow,OUTPUT);
    pinMode(green,OUTPUT);
}

void loop(){

}

void serialEvent(){
    int input = Serial.read();
    if(input == 1){
      digitalWrite(red,HIGH);
    }
    else if(input == 2){
      digitalWrite(yellow,HIGH);
    }
    else if(input == 3){
      digitalWrite(green,HIGH);
    }
    else if(input == 4){
      digitalWrite(red,LOW);
    }
    else if(input == 5){
      digitalWrite(yellow,LOW);
    }
    else if(input == 6){
      digitalWrite(green,LOW);
    }
}

Things i've tried.

  1. I've tried increasing the buffer size from 10(initially) to 64(in the code above)
  2. I've tried increasing the baud rate from 9600 to 12000, just in case it was the serial information not being read fast enough.

None of the things i've tried, listed above, was above to solve my problem, and i dont see what could be causing the problem.

Any advice would be much appreciated.

  • could you include a screen dump of your serial port? ie, whilst you have the serial monitor open, press the keys in the sequences you're interested in, eg (CAPs is on, lowercase is off): RrYyGgRYryRYGrygYGygGYRgyr as I said, as an example. Then check to see if the serial data is as you'd expect. ie, in the above example you should see: 1425361245123456.... and so on – Madivad Jun 8 '14 at 9:05
  • I'll add, I thought initially it might be a limitation of the keyboard, ie three main characters (ie not modifier keys) probably can't all be registered at the same time, but I just tried it on my Macbook Pro keyboard and it did, if you have an onscreen keyboard viewer, see if all the keypresses register as expected. – Madivad Jun 8 '14 at 9:07
  • I'm unable to open the Serial monitor on my Arduino as i get the error Serial port already in use, try quitting any programs that might be using it. I suspect it is because i'm running the processing sketch. However, i've been looking at the processing display, and the when i press 3 keys, the 3rd key does not print to the screen.(e.g when 'g' is the 3rd key pressed,"green" is not printed to the screen). I'm not sure why it works on your Macbook Pro, but i'm guessing the problem lies with my laptop's keyboard's limitation in that it does not register 3 key presses at once! – Kenneth .J Jun 8 '14 at 9:49
  • 1) what hardware and OS are you using? 2) You could try changing your keys, use the modifier keys instead, you should be able to press any combination of shift, control, option/alt, and windows/command. It's usuallt even possible to determine left from right modifier keys. – Madivad Jun 8 '14 at 12:00
1

I'm guessing it is a hardware issue as I discussed in the comments above.

I have just set up a breadboard and copied your code verbatim into the Arduino IDE and Processing (I edited the port index to use). It works as desired. Pressing R, G, or Y will turn on the respective LED and releasing it will turn it off.

I can press these three keys in any combination of none on, one only, any two, or all three, and the LED gives the expected result.

Keyboards were not initially designed to have multiple "main" keys pressed down at any one time, the exceptions to these being the modifier keys (Windows/Command, Option/Alt, Control and possibly some others (I'm thinking the older keyboards here that had "other" keys—Scroll Lock, Print Scr, etc—I'm really unsure).

It appears now, that some hardware and software may have overcome this. As I've said, your project works fine for me. Also, you could try three different keys, it could be a result of how the keys are scanned and you may stumble across three that don't interfere with each other.

As I've suggested in the comments to your question, modify the code to utilise the modifier keys instead, or use the keyboard keys as toggles (ie press once for on, press again to turn it off).

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.