Servo with Countdown Timer

Hi guys , im new on Arduino programming . im so confuse on how to use millis on servo motor. i have a program here that need millis rather than delay because its affecting my countdown timer, i want my to program synchronous (Servo together with coundown time). can somebody help me out ? thank you so much in advance.

PS. i put some part of the code.
P.S im using Arduino Mega 2560.

//Servo Motors
int pos = 0;
int count = 0;
int top = 0;
int ServoPin1 = 11;
int ServoPin2 = 12;
int out = 2;

//Timer Count
int a, b, c = 0; // ones sec, tens sec, minute


//Button
int setBtn = 53;
int redBtn = 51;
int startBtn = 49;
int previouslyPressed1, previouslyPressed2, previousStart = 0;

//Others
int x = -1;
int AA = 0;

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Servo.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
Servo Servo1;
Servo Servo2;

void setup()
{
  pinMode(setBtn, INPUT);
  pinMode(redBtn, INPUT);
  pinMode(startBtn, INPUT);

  Servo1.attach(ServoPin1);          // Make servo go to 90 degrees
  Servo1.write(90);
  delay(500);

  Servo2.attach(ServoPin2);          // Make servo go to 0 degrees
  Servo2.write(0);
  delay(500);

  lcd.clear();
  lcd.begin(16, 2);
  lcd.setCursor(1, 0);
  lcd.print("_*!WELCOME!*_");
  delay(2000);
  lcd.clear();
}
void loop()
{

  while (x == -1)
  {
    a = 0;
    b = 0;
    c = 0;
    lcd.setCursor(0, 0);
    lcd.print ("Auto ?");
    delay(100);
    lcd.setCursor(0, 1);
    lcd.print ("Manual ?");
    delay(100);

    if (digitalRead(startBtn) == HIGH) {
      lcd.clear();
      lcd.print("Auto Selected");
      delay(1000);
      lcd.clear();
      x = 0;
    }
    else if (digitalRead(setBtn) == HIGH)
    {
      lcd.clear();
      lcd.print("Manual Selected");
      delay(1000);
      lcd.clear();
      x = 1;
    }
  }
  
  while (x == 1)
  {
    while (AA == 0)
    {
      lcd.setCursor(4, 0);
      lcd.print("SET TIME");
      delay(1500);
      AA = 1;
      lcd.clear();
    }

    lcd.setCursor(2, 0);
    lcd.print("SETTING TIME");

    if ((digitalRead(setBtn) == HIGH)   &&  (previouslyPressed1 == 0))
    {
      c = c + 5;
      previouslyPressed1 = 1;
      delay(50);
    }
    if ((digitalRead(setBtn) == LOW)   &&  (previouslyPressed1 == 1))
    {
      previouslyPressed1 = 0;
      delay(50);
    }

    if ((digitalRead(redBtn) == HIGH)   &&  (previouslyPressed2 == 0))
    {
      c = c - 5;
      previouslyPressed2 = 1;
      delay(50);
    }
    if ((digitalRead(redBtn) == LOW)   &&  (previouslyPressed2 == 1))
    {
      previouslyPressed2 = 0;
      delay(50);
    }

    if (digitalRead(startBtn) == HIGH)
    {
      x = 2;
    }

    if (c < 0)
    {
      c = 0;
    }

    if (c < 10)
    {
      lcd.setCursor(5, 1);
      lcd.print("0");
      lcd.setCursor(6, 1);
      lcd.print(c);
      lcd.setCursor(7, 1);
      lcd.print(":");
      lcd.setCursor(8, 1);
      lcd.print(b);
      lcd.setCursor(9, 1);
      lcd.print(a);
    } else {
      lcd.setCursor(5, 1);
      lcd.print(c);
      lcd.setCursor(7, 1);
      lcd.print(":");
      lcd.setCursor(8, 1);
      lcd.print(b);
      lcd.setCursor(9, 1);
      lcd.print(a);
    }

  }

  while (x == 2)
  {
    void startServo();
    void timer();

    timer();
    digitalWrite (out , HIGH);

    lcd.setCursor(1, 0);
    lcd.write("REMAINING TIME");
    lcd.setCursor(5, 1);

    if ((a == 0) && (b == 0) && (c == 0))
    {
      digitalWrite (out , LOW);
      lcd.clear();
      lcd.setCursor(3, 0);
      lcd.print("TIME's UP!");
      x = -1;
      AA = 0;
      delay(3000);
      lcd.clear();
    }

    if (digitalRead(setBtn) == HIGH) // PAUSE
    {
      x = 0;
    }

    if (digitalRead(redBtn) == HIGH) // RESET
    {
      a = 0;
      b = 0;
      c = 0;
      lcd.clear();
      delay(1500);
      x = -1;
      AA = 0;
    }


    if (digitalRead(startBtn) == HIGH) // PLAY
    {
      x = 2;
    }

    if (digitalRead(redBtn) == HIGH) // RESET
    {
      a = 0;
      b = 0;
      c = 0;
      lcd.clear();
      delay(1500);
      x = -1;
      AA = 0;
    }
  }
}


void startServo()
{
  for ( count = 0; count <= 4; count++)
  {
    for (pos = 40; pos <= 140; pos += 1) { // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      Servo1.write(pos);              // tell servo to go to position in variable 'pos'
      delay(5);                       // waits 15ms for the servo to reach the position
    }
    for (pos = 140; pos >= 40; pos -= 1) { // goes from 180 degrees to 0 degrees
      Servo1.write(pos);              // tell servo to go to position in variable 'pos'
      delay(5);                        // waits 15ms for the servo to reach the position
    }
    Serial.println(count);
    if (count == 4)
    {
      for (pos = 40; pos <= 90; pos += 1) {
        Servo1.write(pos);
        delay(5);
        top = 1;
      }
    }
  }
  if (top == 1)
  {
    for ( count = 0; count <= 4; count++)
    {
      for (pos = 40; pos <= 140; pos += 1) {
        Servo2.write(pos);
        delay(5);
      }
      for (pos = 140; pos >= 40; pos -= 1) {
        Servo2.write(pos);
        delay(5);
      }
      Serial.println(count);
      if (count == 4)
      {
        for (pos = 40; pos <= 90; pos += 1) {
          Servo2.write(pos);
          delay(5);
        }
      }
    }
  }
}

void timer()
{
  if ((a == 0) && (b == 0) && (AA == 1))
  {
    lcd.setCursor(8, 1);
    lcd.print("0");
    lcd.setCursor(9, 1);
    lcd.print("0");
    delay(1000);
    c--;
    b = 5;
    a = 9;
  }

  if (c < 0)
  {
    c = 0;
  }

  if (a < 0)
  {
    a = 9;
    b--;
  }

  if (c < 10)
  {
    lcd.setCursor(5, 1);
    lcd.print("0");
    lcd.setCursor(6, 1);
    lcd.print(c);
    lcd.setCursor(7, 1);
    lcd.print(":");
    lcd.setCursor(8, 1);
    lcd.print(b);
    lcd.setCursor(9, 1);
    lcd.print(a);
    delay(1000);
    a--;
  } else {
    lcd.setCursor(5, 1);
    lcd.print(c);
    lcd.setCursor(7, 1);
    lcd.print(":");
    lcd.setCursor(8, 1);
    lcd.print(b);
    lcd.setCursor(9, 1);
    lcd.print(a);
    delay(1000);
    a--;
  }

}

manual.ino (5.91 KB)

What does the code actually do? How does that differ from what you want? Are there pull down resistors on the switch inputs? It is always helpful to include a schematic in your question.

More people will see your code and be able to help if you post the code properly so that they don't have to download it. Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

hi ! the code will start the servo motor once you set the desire time , and once pressed start, the countdown timer will start together with the servos ,and when the Time is up the Servo will stop. but my problem is , the delay i used in servo affects the countdown timer and not counting right .

Firstly, You SHOULD use pull-up or pull down for button.

Secondly, to use millis() instead of delay() function, see example Blink Without Delay

The beginner's guide to millis() may have additional information on non blocking timing.

Hi,
Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: