Sorry if this questions is a little basic, but I’m tearing my hair out trying to get my code to work. After some troubleshooting i narrowed the offending section to the following snippet:
void setup(){
size(400,400);
}
void draw(){
while (true) {
if (mousePressed == true) {break;}
}
println("OK!!!!");
delay(10000);
}
I expect that pressing the mouse will break the while loop and print “OK!!!”, but none of that is happening. Turns out mousePressed is not updating to true within the while loop. Anyone know why this is happening?
i am a little bit confused by your question,
because this is not your first processing program,
as you do already arduino, processing, serial communication…
your idea goes so much against any processing thinking
( but also arduino or any PLC like programming )
now in draw:
you draw ( design ) something, but
processing loop execution is like:
read mouse, keyboard, ( file lines… ) in a input cycle
run code from draw ( and use the input information )
do all outputs like
send drawing as resulting picture to graphic card output
write to files…
and unless you disable it all this is executed 30 times per second
( default frameRate )
IN -> CALC -> OUT .
now for that reason pls
– not use delay in draw
– break while loops by logic ( or not use at all )
( as keyboard and mouse only can be read before draw / while )
The code snippet is not meant to be complete or even functional, its merely an example to show (what I think is) anomalous behavior. Instead of dumping my entire code here, I’ve done my homework and posted just a summary of my problem. I couldn’t find any documentation about how the mousePressed variable actually updates, which is why i’m posting here.
Now I understand there are probably twenty easier ways to register a mouse click, but for now I’m trying to understand why this particular way doesn’t work.
Hope this clears things up.
You might have guessed, but the delay(10000) is throwaway. It only exists to make execution success (print “OK”) easier for me to observe.
Best coding practices aside, there is no reason why I can’t break a while loop with break. In fact, the following doesn’t work as well:
I had assumed mousePressed works like an interrupt when a button is clicked but it actually polls between each draw() loop for a click, which is why mousePressed never updates within a loop.
My original intention was to send a command to a waiting arduino with a mouse click. Since I can’t while loop indefinitely I replaced the while with an if(waiting state) and it works properly now.
That is correct – and true for the whole event model. draw() renders the frame, and then then keyboard, mouse, and various post- event updates from any libraries you are using are all processed. frameCount increments, pre-events are called, draw renders again, et cetera. In general, this is why delaying within draw often fails to work as a debugging method – although I have also been down that rabbit hole.
There are some other things that have similar behavior that might be surprising. For example, exit() might surprise you – try running this sketch: