I got my ps2 mouse to send its values to the serial monitor.
Then I tried to output the value only when changes occur
but I can't figure out how to do that. ( even though I'm
able to do this with rotary encoders )
I just added:
if (mstat != prevmstat){ Serial.println( mstat, DEC ) ;
prevmstat = mstat;
}
to the code provided with the ps2 mouse library
Any help would be greatly appreciated, cheers.
if (mstat != prevmstat){ Serial.println( mstat, DEC ) ;
prevmstat = mstat;
}
;
delay(100); /* twiddle */
}
What is the lonely semicolon doing before the delay? I don't think it has got any use there. . .
Also, if you want to send back the values every time the state changes use code similar to what you have written just replace the variables and values to what you need to check and send.
And, put your code in the code tags, so that others can read it properly.
thanks for your reply Abhik
I don't really understand your answer, but maybe my message wasn't clear enough.
I get data from the mouse sent to serial constantly, even though I tried to send the data
only when anything has changed. ( To do so I tried to compare the current value with the
previous like this:
mstat = mouse.read();
if (mstat != prevmstat){ Serial.println( mstat, DEC ) ;
prevmstat = mstat;
Still the mouse data is constantly sent to the serial monitor.
Do you have an idea what's wrong? ( or maybe the answer IS in your previous post but I can't seem to get what you meant...
Great! that you solved your problem. Here is a short explanation of what was causing the problem:
As you might know your loop() function runs continuously as long as your Arduino has power. Declaring the variables at the beginning of the loop() function caused the variables holding the current and previous states of your mouse to be different in each iteration of the loop(). And since computers (including all Arduinos) do not posses common sense, you received values in your serial monitor every time loop() was executed.
Moving the variables to the beginning of your code made them "Global Variables" i.e. variables that can be accessed from anywhere in the code. And now the variables storing the previous state of the mouse retained their values unaffected by the loop() function.