We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello.
I have this and Im using a button to get a number (0-1) in the port. I want that when the read is 1, in the keyboard use the key RIGHT. And when the read is 0 it stops, the problem is that it doesn't stops.
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import processing.serial.*;
Serial port;
int val = 0;
Robot robot;
void setup() {
size(200, 200);
try {
robot = new Robot();
}
catch (AWTException e) {
e.printStackTrace();
}
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
}
void draw()
{
if (0<port.available())
{
val = port.read();
if ( val ==1);
println(val);
{
//robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyPress(key=RIGHT);
}
} else {
if (val == 0);
{
//robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(key=RIGHT);
}
}
}
Thank you.
Answers
I guess the problem lies at your
else
block being bound to the outerif
block rather the innerif
!Also variable val is used exclusively inside draw()'s
if
block. So why is it declared globally instead of locally?That would help you out to discover the bug since val wouldn't exist at the outer
else
block! [-(Anyways, here's my attempt. Not good about Serial & Robot stuff though: :|
WOOOOOOOW THANK YOU SO MUCH IT WORKS :D Finally after maaaaaany attempts.
Greetings from Colombia: D