Clicking with servo

I am attempting to create a device that will use a servo to push the left hand mouse button. The actual armature that holds the mouse and the servo in position has been created, but I am having issues with the code. The code needs to do the following:

-Move servo to 45 degrees and then return to 0 at a random amount of time between 5-10Ms
-wait 1.55 to 1.65 seconds before starting again
-print the random times generated for each variable time.

I am really just starting out and don’t know much about coding so any advice or help is appreciated.


Usual advice - post your code, please.


Arduino boards based on the 32u4 can emulate a HID device, and thus act like a mouse when plugged into USB. The due can too, but it's a more advanced board. See

Wouldn't that be a better way to achieve your goal?

Alternately, what about putting a relay across the switch on the LMB?

Using a servo for this seems... a bit ridiculous... unless you're doing it for show.

Simple servo button code.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
  pinMode(button1, INPUT);
  digitalWrite(4, HIGH); //enable pullups to make pin high

void loop()
  press1 = digitalRead(button1);
  if (press1 == LOW)

I buy mice for $1 at garage sales.

You can plug in multiple USB mice with no problems (except for 2-year-olds).

Use a screwdriver - hack one, you will generally find a microswitch which switches to ground.

Attach an NPN transistor across microswitch or even better - an opto-coupler. Control it (using a suitable resistor - 220 ohms for opto, 10k for transistor) with an Arduino port.