We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I am learning processing 3 to control LED via bluetooth on Arduino. On the canvas, there are 2 buttons, one is "Turn ON", another is "Turn OFF", with below code,
// If the button "Turn ON" is pressed
if(mousePressed && mouseX>50 && mouseX<200 && mouseY>100 && mouseY<150){
//myPort.write('1'); // Sends the character '1' and that will turn on the LED
i++;
myPort.write(i);
// Highlighs the buttons in red color when pressed
stroke(255,0,0);
strokeWeight(2);
noFill();
rect(50, 100, 150, 50, 10);
}
every click, line " myPort.write(i);" was repeated different times, below is output from Arduino
First Click
123
456
789
10
Second Click
1112
13141516
What I wanna achieve is as below:
First Click
1
Second Click
2
Your help is highly appreciated.
Fei
Answers
Use
mousePressed()
, the function, notmousePressed
, the boolean.The conditional statement that looks at the boolean will get checked each time draw runs, which is about 60 times a second, and a click lasts for longer than that.
The function, however, will only run once per click.
Your sketch is running at 30fps. MousePressed is issued as long as your key is pressed. If you pressed it for 1 seconds, you should multiple outputs, exactly as you reported. Instead of mousePressed within draw, use mouseRelesed() like this:
More at https://processing.org/reference/mouseReleased_.html
Kf
Thanks for your great help, @ kfrajer and TfGuy44
Now it is under my control. :-)