[solved] PS2 mouse/ How to print data only when the mouse state changes ?

Hi,

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.

Here is the code I used:

#include <ps2.h>

PS2 mouse(6, 5);

void mouse_init()
{
  mouse.write(0xff);  // reset
  mouse.read();  // ack byte
  mouse.read();  // blank */
  mouse.read();  // blank */
  mouse.write(0xf0);  // remote mode
  mouse.read();  // ack
  delayMicroseconds(100);
}

void setup()
{
  Serial.begin(9600);
  mouse_init();
}

void loop()
{
  char mstat;
  char mx;
  char my;
  char prevmstat;
  char prevmx;
  char prevmy;

  mouse.write(0xeb);  // give me data!
  mouse.read();      // ignore ack
  mstat = mouse.read();

  if (mstat != prevmstat){ Serial.println( mstat, DEC ) ;
  
  prevmstat = mstat;
  }

  delay(100); 
}
/* send the data back up */

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.

Hi,

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...

Cheers

#include <ps2.h>

PS2 mouse(6, 5);

void mouse_init()
{
  mouse.write(0xff);  // reset
  mouse.read();  // ack byte
  mouse.read();  // blank */
  mouse.read();  // blank */
  mouse.write(0xf0);  // remote mode
  mouse.read();  // ack
  delayMicroseconds(100);
}

void setup()
{
  Serial.begin(9600);
  mouse_init();
}

void loop()
{
  char mstat;
  char mx;
  char my;
  char prevmstat;
  char prevmx;
  char prevmy;

  mouse.write(0xeb);  // give me data!
  mouse.read();      // ignore ack
  mstat = mouse.read();

  if (mstat != prevmstat){ Serial.println( mstat, DEC ) ;
 
  prevmstat = mstat;
  }

  delay(100);  /* twiddle */

I solved my problem, I moved

char mstat;
  char mx;
  char my;
  char prevmstat;
  char prevmx;
  char prevmy;

from loop to the beginning of the sketch.
Cheers Abhik, I was fixing my attention on the wrong place.

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.

Hope it proves useful :slight_smile: