Mouse simulator


Sometimes you come across weird PCs. They behave very curious and whatever you try it won't help.

You all know how to configure the screen saver. But you also know it's not only a matter of the operating system but also the mainboard, the graphics device and depending on your environment the network access (in your office, you won't have administrator rights). And so it might happen the screen saver strikes for no specific reason after a short time and wants you to reenter your password when you just had a slip of coffee.

It would help if your daughter played around with the mouse because any user action prevents the screen saver from striking. But your daughter went to the nursery. So who could replace her? Well, take an Arduino Leonardo and make it do the job:

/*
 performes regularily short mouse movements
 (disabling the screen saver)
 */

void setup() { 
  Mouse.begin();
}

int dy = 2;

void loop() {
      // move mouse up/down
      Mouse.move(0, dy);
      dy = -dy;
      delay(10000);
}

Actually, you are not the only one who happened to come across such a situation. If you search the net you find people offering ready-made devices called Jiggler, looking like a pen drive.

While working with Photoshop or similar software you should disable the Leonardo not to get unwanted mouse-moves in your picture.

If you get aware that in this sketch your Arduino Leonardo is wasting most of its time just waiting you can add some visual control using the RX and TX LEDs.
(Obviously, the PWM analogWrite does not work with these two port pins. So you have to do the fading of the LEDs on your own.)
This is another advantage of the Leonardo board; with the other boards the RX and TX LEDs are connected to the USB controller (MEGA8U2, MEGA16U2, MEGA32U2) so your sketch has no access to them.

/*
 For ARDUINO LEONARDO
 Performs small mouse movements regularily
 deactivating the screen saver.
 Visual control: the RX and TX LEDs
 fading away alternatively.
*/
#define RX 0
#define TX 2

uint8_t dy = 2;
const long dt = 10; // milli seconds
long t;
long q; // time since last call of update()
int n = 256 * dt;
byte pin = RX;
boolean Old, New;

void setup() { 
  Mouse.begin();   // initialize mouse control
  asm("SBI %0, %1 " : : "I" (_SFR_IO_ADDR(DDRB)), "I" (PORTB0) );
  asm("SBI %0, %1 " : : "I" (_SFR_IO_ADDR(DDRD)), "I" (PORTD5) );
  update();
}

void loop() {
  q = millis() - t; // normally positive
  New = 256L * q < n;
  if (New != Old) LED(pin, Old = New);
  if (q > dt) {
    update();
    n = n - dt;
    if (n < 0) {
      Mouse.move(0, dy = -dy);
      if (pin == RX) pin = TX; else pin = RX;
      LED(RX,false);
      LED(TX,false);
      n = 256 * dt;
    }
  }   
}

void update() {
  t = millis();
}

void LED(byte nr, boolean on) {
  switch (nr + on) {
    case RX + true : asm("CBI %0, %1" : : "I" (_SFR_IO_ADDR(PORTB)) , "I" (PORTB0) ); break;
    case RX + false: asm("SBI %0, %1" : : "I" (_SFR_IO_ADDR(PORTB)) , "I" (PORTB0) ); break;
    case TX + true : asm("CBI %0, %1" : : "I" (_SFR_IO_ADDR(PORTD)) , "I" (PORTD5) ); break;
    case TX + false: asm("SBI %0, %1" : : "I" (_SFR_IO_ADDR(PORTD)) , "I" (PORTD5) ); break;
  }
}


The picture shows an Arduino Pro Micro which is more or less compatible with the Arduino Leonardo.
As you can clearly see the device needs no additional hardware except for the USB cable.

P.S.
On this site I found the assembler instructions can be replaced by TXLED0, RXLED0, TXLED1, RXLED1. That really makes life easier.


contact: nji(at)gmx.de