We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I've made a small program to turn on and off relays via ardiuno and it works. Now I would like to make a proper interface so basically have rectangle on screen and show which button/light is turned on. So when I press 1 the rectangle labled light 1 should turn red and go back to the when I release the key. the problem i'm having is that when the rectangle turns red it stays red without going back even when key is release. I could use the keyReleased function but then how would I include that into my current program?
import processing.serial.*;
Serial myPort; // Create object from Serial class
void setup()
{
size(200, 200);
String portName = Serial.list()[5];
myPort = new Serial(this, portName, 9600);
printArray(Serial.list());
}
void draw() {
if ((keyPressed == true)&&(key == '1'))
{
myPort.write('a');
println("1");
} else if ((keyPressed == true)&&(key == '2')) {
myPort.write('b');
println("2");
} else if ((keyPressed == true)&&(key == '3')) {
myPort.write('c');
println("3");
} else if ((keyPressed == true)&&(key == '4')) {
myPort.write('d');
println("4");
} else if ((keyPressed == true)&&(key == '5')) {
myPort.write('e');
println("5");
} else
{
myPort.write('0');
}
}
Here is the code showing the idea about the rectangle turning red I've used ellipse just to show you the idea.
void setup(){
size(400,400);
ellipse(20,20,20,20);
}
void draw(){
if((keyPressed == true)&&(key == '1')){
ellipse(20,20,20,20);
fill(255,0,0);
}
}
Answers
Fairly simple actually, all you have to do is add an else statement after your if statement, and inside it draw an ellipse with the other color.
BTW, your first program may not work properly when pressing more than one key at a time, though I could be wrong.
One way to do things....
Kf
P.S. I am not fan of managing keyPressed in draw. I have seen some unwanted behavior before.
@kfrajer it is a good example.
More complex version, here you just need the ASCII code of a character to know if it is pressed on the keyBoard(will add support for special, i.e. coded, characters later):