Arduino leonardo auto fast clicker

Hello,
i found a code online from "Bobby Zhang -bobbbert-CMU"
i want to use this code to make my arduino leonardo do 1000 clicks per second.
When i uploaded the code left mouse started clicking, i tested it and is doing only 50cps
Can someone help me to edit the code so it can make 1000cps ?
Thanks in advance .
Here is the code:

//Emulates a left mouse button auto-clicker
//For Arduino Leonardo
//Press the button once to turn on the clicker and LED
//Press the button again to turn off the clicker and LED
//Requires use of the Mouse library

#include <Mouse.h>

// set pin numbers for the button and light:
// Pin 2 for LED see reference https://www.arduino.cc/en/Tutorial/Blink
// Pin 7 for button see reference: https://www.arduino.cc/en/Tutorial/KeyboardAndMouseControl
const int mouseButton = 7;
const int ledLight = 2;

//Sets flags and delays on the button
boolean isClicking = false;
const int buttonDelay = 500;  // in milliseconds
const int clickPerSecond = 1;   // number of clicks per second 
const int loopSleep = 10;   // in milliseconds
int buttonDelayCnt = 0;
int clickDelayCnt = 0;

void setup() { 

  // initialize the button input and light output:
  pinMode(mouseButton, INPUT);
  pinMode(ledLight, OUTPUT);

  // initialize mouse control:
  Mouse.begin();
}

void loop() {
  int buttonDelayMax = buttonDelay/loopSleep;  
  int clickDelayMax = (1000/clickPerSecond)/loopSleep-1;
  int buttonDelayCnt = 0;
  int clickDelayCnt = 0;
  if(isClicking == true && clickDelayCnt == 0 )
  {
     Mouse.click(MOUSE_LEFT);
     clickDelayCnt = clickDelayMax;
  }  

  if (digitalRead(mouseButton) == HIGH && buttonDelayCnt == 0 ) {
    if(isClicking == true)
    {
      //Stops the clicker and turns off the LED
      isClicking = false;
      digitalWrite(ledLight, LOW);  
    }
    else
    {
       //Starts the clicker and turns on the LED
       isClicking = true;
       digitalWrite(ledLight, HIGH);  
    }

    buttonDelayCnt = buttonDelayMax;
  }

  //Delays the next button read
  if( buttonDelayCnt > 0)
    buttonDelayCnt--;

  //Delays the click speed
  if( clickDelayCnt > 0)
    clickDelayCnt--;

  delay(loopSleep);

}


No idea if 1000 clicks can be achieved, but these both constants control the sketch:

const int clickPerSecond = 1;   // number of clicks per second 
const int loopSleep = 10;   // in milliseconds

loopSleep of 10 msec allows not more than 100 loops/sec (without considering the other functions).

If you want to come close to 1000 clicks/sec you must change loopSleep to 1 msec. In addition you have to set clickPerSecond to 1000.

const int clickPerSecond = 1000;   // number of clicks per second 
const int loopSleep = 1;   // in milliseconds

If you have done this, it will depend on the rest of the functions (mainly Mouse.click(MOUSE_LEFT); ) how close you come to 1000 clicks/sec.

In case you can came close you may change the delay():

delay(loopSleep);  // by
delayMicroseconds(loopSleep);  // and set loopSleep to an appropriate time like 500 for 0,5 msec

See https://www.arduino.cc/reference/de/language/functions/time/delaymicroseconds/

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.