Servo motors, millis and timer display

Hello,
First time with Arduino,
I'm building a robot for an academic project that should exercise (Push-Ups with 2 servo motors).

The process should be:
when clicking a button,
timer counts 5 seconds (timer display on LCD 1602 screen),
30 seconds of moving servo motors,
40 seconds of break
30 moving
40 break
30 moving
end of process

the timer should be always displayed on the lcd screen.

I know how to move the servo motor ("for" loop) and I know how to set a timer (every 1 second, reduce by one) but I don't know how to combine.

I will be happy to get some explainations.

thank you :slight_smile:

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

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

...R

Take a look at:

Hello,

I used this code and I would like to get some comments.
(I didn’t connect it to LCD screen yet so I only used serial.prinln to se the result:

#include <Servo.h>
Servo righthand; // right hand servo of the robot
Servo lefthand; // left hand servo of the robot

int pos = 0; // variable to store the servo position
unsigned long CurrentTime;
unsigned long PreviousTime = 0;
const unsigned long TimeInterval = 1000;
int NTimer = 6;
boolean ProcessEnd = false;

void setup() {

lefthand.attach(5); // attaches the servo on pin 5 to the servo object
righthand.attach(6); // attaches the servo on pin 6 to the servo object

lefthand.write(180); //initial position
righthand.write(90); //initial position

pinMode(2,INPUT);

Serial.begin(9600);


}

void loop(){

if (digitalRead(2)==HIGH)
{


}

else
{

ProcessEnd = true;

}
 

if (ProcessEnd == true)
{
      Serial.println ("Get Ready");
      while (NTimer > 1)
      {
      CurrentTime = millis();
      
      if (CurrentTime - PreviousTime >= TimeInterval) {
            NTimer = NTimer - 1;
            Serial.println (NTimer);
            PreviousTime = CurrentTime;
            }
      }

for (int i=0; i<3; i++){

 NTimer = 31; // Set exercise timer
      Serial.println ("Start");
while (NTimer>0){
      for (pos = 180; pos >= 90; pos -= 1) { // goes from 180 degrees to 90 degrees in steps of 1 degree
     lefthand.write(pos); // tell servo to go to position in variable 'pos'
     righthand.write(270-pos);
     CurrentTime = millis();
      
     if (CurrentTime - PreviousTime >= TimeInterval) {
     NTimer = NTimer - 1;
     Serial.println (NTimer);
     PreviousTime = CurrentTime;
         }
     
     delay(15); // waits 15ms for the servo to reach the position
     }
     
     for (pos = 90; pos <= 180; pos += 1) { // goes from 90 degrees to 180 degrees
     lefthand.write(pos); // tell servo to go to position in variable 'pos'
     righthand.write(270-pos);
     CurrentTime = millis();
      
     if (CurrentTime - PreviousTime >= TimeInterval) {
     NTimer = NTimer - 1;
     Serial.println (NTimer);
     PreviousTime = CurrentTime;
         }
     
     delay(15); // waits 15ms for the servo to reach the position
      }
}


  NTimer = 41; // Set break timer
      Serial.println ("Take A Rest");
      while (NTimer > 1)
      {
      CurrentTime = millis();
      
      if (CurrentTime - PreviousTime >= TimeInterval) {
            NTimer = NTimer - 1;
            Serial.println (NTimer);
            PreviousTime = CurrentTime;
            }
      }
}

Serial.println ("Excellent Job!");
ProcessEnd = false;
NTimer = 6;

}

}

+1 karma for using code tags.

Please "auto format" your code before you post it each time.

I still see delay() used inside for() loops. You have not grasped the advice given. Study the links and examples again.