Interval adjustment using Leonardo

How to adjust the second click //interval 2// for only 1 sec after the //interval 1//?

/*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=10000; //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(910,530);       //initial position of mouse
 lastPosX=910;
 lastPosY=530;
 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,5000);  //take the interval time randomly for move
      
     while(numberClick<2){  
           timeNow=millis();
          if(timeNow-intervalMove>timeDelay){
             timeDelay=random(0,5000);
             intervalMove=millis();
             
             moveX=random(-10,10);
             moveY=random(-10,10);
             currentPosX=lastPosX+moveX;
             currentPosY=lastPosY+moveY;
             if(currentPosX>910 && currentPosX<1010){   //100px,20py
               x=moveX;
               lastPosX=currentPosX;
             }
             if(currentPosY>530 && currentPosY<550){    //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>=3000){
              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,5000); //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(-10,10);
             moveY=random(-10,10);
             currentPosX=lastPosX+moveX;
             currentPosY=lastPosY+moveY;
             if(currentPosX>910 && currentPosX<1010){   //100px,20py
               x=moveX;
               lastPosX=currentPosX;
             }
             if(currentPosY>530 && currentPosY<550){    //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>=3000){
              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,5000); //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(-50,50);
             moveY=random(-10,10);
             currentPosX=lastPosX+moveX;
             currentPosY=lastPosY+moveY;
             if(currentPosX>910 && currentPosX<1010){    //100px,20py
               x=moveX;
               lastPosX=currentPosX;
             }
             if(currentPosY>530 && currentPosY<550){    //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>=3000){
              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,5000);
      
     while(timeNow-timeLast<=stanby){  // time stanby
           timeNow=millis();
          if(timeNow-intervalMove>timeDelay){
             timeDelay=random(0,5000);
             intervalMove=millis();
             
             moveX=random(-50,50);
             moveY=random(-10,10);
             currentPosX=lastPosX+moveX;
             currentPosY=lastPosY+moveY;
             if(currentPosX>910 && currentPosX<1010){   //100px,20py
               x=moveX;
               lastPosX=currentPosX;
             }
             if(currentPosY>530 && currentPosY<550){    //100px,20py
               y=moveY;               
               lastPosY=currentPosY;
             }  
              Serial.print(lastPosX); 
              Serial.print(",");
              Serial.println(lastPosY);
              Serial.println(timeDelay);
              Mouse.move(x,y);
               
          }
     }  
}

itan:
How to adjust the second click //interval 2// for only 1 sec after the //interval 1//?

I can’t say I understand what you want to happen, or even what happens now - maybe you can describe the current behaviour and then explain, by reference to that, what you want to happen that is different.

It seems to me the time interval is controlled by the variable timeDelay which is set at random. If I’m right them maybe all you need to do, at the appropriate place is to change from the random value to 1000.

As a general comment all those WHILEs seem likely to cause trouble as they block the Arduino from doing other things until they complete.

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. Note that it does not use WHILE.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

…R