Mouse Simulation using Arduino Leonardo

Hello,

I have used this code from humanHardDrive but apparently I need to tweak a bit. Well i'm not sure if it is possible.

Condition is: It will randomly double click in between 40-50 sec. How am I able to work on this.

#include <Mouse.h>

void setup()
{
  pinMode(2,INPUT);
  digitalWrite(2,HIGH);

  randomSeed(millis());
}

void loop()
{
  if(digitalRead(2) == LOW)
  {
    Mouse.begin();
    
    switch(random(0,3))
    {
    case 0: //Press the right mouse button
      Mouse.press(MOUSE_RIGHT);
      delay(100);
      Mouse.release(MOUSE_RIGHT);
      break;

    case 1: //Jiggle the mouse
      Mouse.move(random(-128,128),random(-128,128), 0);
      break;
      
    case 2: //Double click left mouse button
      Mouse.press(MOUSE_LEFT);
      delay(100);
      Mouse.release(MOUSE_LEFT);
      delay(100);
      Mouse.press(MOUSE_LEFT);
      delay(100);
      Mouse.release(MOUSE_LEFT);
      break;
    }
    
    delay(random(60*1000,5*60*1000));
  }
  else
  {
    Mouse.end();
  }
}

Thanks

What do you want it to do different?

MorganS:
What do you want it to do different?

MorganS:
What do you want it to do different?

That It will "only" randomly double click in between 40-50 sec.

So delete case 0 and case 1. Adjust the random near the bottom to the time period you require.

Okay so I have this code now but the timing doesn't work well. The condition should be

1.) wait for 40 sec.
2.) then 1st click after 40 seconds
3.) then 2nd click after 1 second after the 1st click
4.) Loop back again for 40 sec.

The catch is it should start clicking randomly in between 40-45 sec.

Thanks for the help.

/*Description:
 * This project is about coding for Arduino Leonardo, it is a simple code the to move and click my mouse randomly. 
 * Funtions: 
 * - It click mouse on a specific time with a random intervals also it will move on a random location 
 * - It would probably double click withing 40 or 50 sec 
 * - Mouse will move only on that small area:100x20px randomly.
 * - Our minimum doble click interval would be 2sec. That would play around 2-5sec
 * - Once started the arduino it will automatically move and click the mouse 
 * - It should always simulate until I stop it.
 * - Just make the mouse action human like 
 */
#include <Mouse.h>  //library for the control mouse

unsigned long timeNow;   //current time
unsigned long timeLast;  //last time
unsigned long stanby=8000; //40000 milliseconds
unsigned long intervalMove;  //reference time to move the mouse
unsigned long intervalClick; //reference time to do click
long timeDelay;     // random time for the mouse to move
signed char x;     // value axis x to move the mouse   Mouse.move(x,y);
signed char y;     // value axis y to move the mouse   Mouse.move(x,y);
long moveX;        //random value axis x to move the mouse   
long moveY;        //random value axis y to move the mouse 
long currentPosX;  // current position axis x of mouse
long currentPosY;  // current position axis y of mouse
long lastPosX;     // last position axis x of mouse
long lastPosY;     // last position axis y of mouse
int numberClick;   //how much clicks

#define control 2  //connect a button for control mouse

void setup() {
Mouse.begin();            //initialization of mouse
Mouse.move(30,32);       //initial position of mouse
 lastPosX=30;
 lastPosY=32;
 pinMode(control,INPUT);   //button as input
 Serial.begin(9600);

}

void loop() {

// if(digitalRead(control)==HIGH){      // uncomment if you want to add a button

    
   
   timeStandby();   //standby 1 

     //////////////////////interval 1////////////////////////////////
Serial.println("interval 1");
      timeLast=millis();  //take the last time
      timeNow=millis();   //take the current time
      intervalMove=millis();  //take the interval time reference for move
      timeDelay=random(0,2000);  //take the interval time randomly for move
      
     while(numberClick<2){  
           timeNow=millis();
          if(timeNow-intervalMove>timeDelay){
             timeDelay=random(0,5000);
             intervalMove=millis();
             
             moveX=random(-4,4);
             moveY=random(-4,4);
             currentPosX=lastPosX+moveX;
             currentPosY=lastPosY+moveY;
             if(currentPosX>30 && currentPosX<32){   //100px,20py
               x=moveX;
               lastPosX=currentPosX;
             }
             if(currentPosY>30 && currentPosY<32){    //100px,20py
               y=moveY;               
               lastPosY=currentPosY;
             }  
              Serial.print(lastPosX); 
              Serial.print(",");
              Serial.println(lastPosY);
              Serial.println(timeDelay);
              Mouse.move(x,y);
             if(numberClick==0){
                  Mouse.click();
                  numberClick=1;
                  Serial.println("firts click");
                  intervalClick=millis();
             }
                                 
                }
            if(numberClick==1 && timeNow-intervalClick>=100){
              numberClick=2;
              Mouse.click();
              Serial.println("second click");
            }
     }
     numberClick=0; 

     timeStandby();   // stanby 2  

     //////////////////////interval 2////////////////////////////////
      Serial.println("interval 2");
      timeLast=millis();      //take the last time
      timeNow=millis();       //take the current time
      intervalMove=millis();  //take the interval time reference for move
      timeDelay=random(0,2000); //take the interval time randomly for move
      
     while(numberClick<2){  // time stanby
           timeNow=millis();
          if(timeNow-intervalMove>timeDelay){
             timeDelay=random(0,5000);
             intervalMove=millis();
             
             moveX=random(-4,4);
             moveY=random(-4,4);
             currentPosX=lastPosX+moveX;
             currentPosY=lastPosY+moveY;
             if(currentPosX>30 && currentPosX<32){   //100px,20py
               x=moveX;
               lastPosX=currentPosX;
             }
             if(currentPosY>30 && currentPosY<32){    //100px,20py
               y=moveY;               
               lastPosY=currentPosY;
             }  
              Serial.print(lastPosX); 
              Serial.print(",");
              Serial.println(lastPosY);
              Serial.println(timeDelay);
              Mouse.move(x,y);
             if(numberClick==0){
                  Mouse.click();
                  Mouse.click();
                  numberClick=1;
                  Serial.println("firts click");
                  intervalClick=millis();
             }
                                 
                }
            if(numberClick==1 && timeNow-intervalClick>=100){
              numberClick=2;
              Mouse.click();
              Mouse.click();
              Serial.println("second click");
            }
     }
     numberClick=0; 

     timeStandby();   // stanby 3

      //////////////////////interval 3////////////////////////////////
Serial.println("interval 3");
      timeLast=millis();        //take the last time
      timeNow=millis();         //take the current time
      intervalMove=millis();    //take the interval time reference for move
      timeDelay=random(0,2000); //take the interval time randomly for move
      
     while(numberClick<2){  // time stanby
           timeNow=millis();
          if(timeNow-intervalMove>timeDelay){
             timeDelay=random(0,5000);
             intervalMove=millis();
             
             moveX=random(-4,4);
             moveY=random(-4,4);
             currentPosX=lastPosX+moveX;
             currentPosY=lastPosY+moveY;
             if(currentPosX>30 && currentPosX<32){   //100px,20py
               x=moveX;
               lastPosX=currentPosX;
             }
             if(currentPosY>30 && currentPosY<32){    //100px,20py
               y=moveY;               
               lastPosY=currentPosY;
             }  
              Serial.print(lastPosX); 
              Serial.print(",");
              Serial.println(lastPosY);
              Serial.println(timeDelay);
              Mouse.move(x,y);
             if(numberClick==0){
                  Mouse.click();
                  numberClick=1;
                  Serial.println("firts click");
                  intervalClick=millis();
             }
                                 
                }
            if(numberClick==1 && timeNow-intervalClick>=100){
              numberClick=2;
              Mouse.click();
              Serial.println("second click");
            }
     }
     numberClick=0; 

}

void timeStandby(void){
  Serial.println("stanby ");
      timeLast=millis();  //take the last time
      timeNow=millis();   //take the current time
      intervalMove=millis();
      timeDelay=random(0,2000);
      
     while(timeNow-timeLast<=stanby){  // time stanby
           timeNow=millis();
          if(timeNow-intervalMove>timeDelay){
             timeDelay=random(0,5000);
             intervalMove=millis();
             
             moveX=random(-4,4);
             moveY=random(-4,4);
             currentPosX=lastPosX+moveX;
             currentPosY=lastPosY+moveY;
             if(currentPosX>30 && currentPosX<32){   //100px,20py
               x=moveX;
               lastPosX=currentPosX;
             }
             if(currentPosY>30 && currentPosY<32){    //100px,20py
               y=moveY;               
               lastPosY=currentPosY;
             }  
              Serial.print(lastPosX); 
              Serial.print(",");
              Serial.println(lastPosY);
              Serial.println(timeDelay);
              Mouse.move(x,y);
               
          }
     }  
}