Off Timer in Arduino

Hello everyone,

I am doing something with servo motors and I need yall help with it. I’m trying to make 5 servo motors to do a thing then a button is pressed, but servo motors should turn off after 30s or more, basically I’m trying to do is off timer. In code you can see I was trying to make variable (ServoFlag) and with that variable control servo motor off time, but it didn’t work as planed. How can I make it work? Thank you for answers in advanced.


Bear in mind that I’m new to programming and if you could answer the question, like you trying to explain it for dumb person (me) and try to ignore the comments and other random sh*t.

#include <Servo.h>           //
#include <Stepper.h>         // Pasirenkamos bibliotekos pritaikytos naudojamams įrenginiams
#include <SerialMP3Player.h> //

int servopina = 3;  //
Servo Servo1;       // Sukuriami servo variklių kintamieji.
int servopinb = 10; // Taip pat virtualūs kintamieji, valdantys
Servo Servo2;       // servopin* =n.
int servopinc = 9;  //
Servo Servo3;       //
int servopind = 5;  //
Servo Servo4;       //
int servopine = 6;  //
Servo Servo5;       //

int buttonpin = 11;  // Mygtukui priskiriamas kintamasis
int buttonstate = 0; // in virtualus kintamasis skaitantis mygtuko reikšmę

long randomNumber;  // Ilgasis skaičius nenuspėjamai keičiantis servo variklių reikšmes.
int ServoFlag = 0;  // 
int Servomotor = 0; // Kintamasis naudojamas *switch* funkcijoje.

int TX = 7;         // Nustatomi kintamieji MP3 grotuvo
int RX = 0;         //
SerialMPPlayer mp3 (RX, TX);

void setup() {
  Serial.begin (9600);
  mp3.begin (9600);

  randomSeed(analogRead (A0)); // Nustatomas kontaktas iš kurio skaitoma *random* reikšmė.
  Servo1.attach (servopina); //
  Servo2.attach (servopinb); //Priskiriami kintamieji.
  Servo3.attach (servopinc); //
  Servo4.attach (servopind); //
  Servo5.attach (servopine); //
  pinMode (buttonpin, INPUT); // Mygtukas nusakomas kaip įėjimas.
  Servo1.write(100); // Veiksmas vyksta tik vieną kartą
  delay(500);        // įsitikinti, kad servo varikliai vis dar veikia
  Servo2.write(90);  // ir sugražinti jų reikšmes į pradinę padėtį.

  mp3.sendCommand (CMD_SEL_DEV, 0, 2); // Pasirenkama MicroSD kortelė

void loop() {
  buttonstate = digitalRead (buttonpin);
  if (buttonstate == HIGH){;
    digitalWrite(ServoFlag, HIGH);
    digitalWrite(ServoFlag, LOW);
  if (ServoFlag == HIGH){
    Servomotor = random (1, 6); // Veiksmas išrenka atsitiktinai *Switch* funkcijos skaičių
  if (ServoFlag == LOW){
    Servomotor = 0;
  switch (Servomotor){     // *Switch* funkcija naudojama servo 
    case 1:                // variklio nustatymui pagal random funkcija.
      Servo1.write (100);  // servo variklių skaičiai eina nuo 1 iki 5
      delay (1000);
      Servo1.write (20);
    case 2:
      Servo2.write (90);
      Servo2.write (10);
      delay (1000);
    case 3:
      Servo3.write (15);
      delay (1000);
      Servo3.write (100);
      delay (1000);
    case 4:
      Servo4.write (40);
      delay (1000);
      Servo4.write (130);
      delay (1000);
    case 5:
       Servo5.write (30);
       delay (1000);
       Servo5.write (115);
       delay (1000);


ServoRandom.ino (3.12 KB)

delay() blocks everything. Nothing happens until it's complete. So you're setting servoFlag HIGH then 30 seconds later setting it LOW. Only then do you check to so see if it's isn't!

You need to learn about timing using millis(). There are loads of tutorials around, try Several things at a time or Using millis() for timing


several suggestions

  • no need for delays after each function call in setup()

  • unless you have an external pull-up/down resistor on the buttonpin, configure it as an INPUT_PULLUP and connect the switch between the input and ground

  • detect a change in the button state by having a buttonLst variable, reading the button state, comparing it to the last state and recognizing a press when the button state is LOW after a change (delay(10) after a change to debounce)

  • as already suggested and similar to detecting a button press, have a msecLst variable that is compared to the current msec = millis() and used to determine when a period of time has passed. compare msec - msecLst >= interval. msecLst = msec when time expired.

  • every elapsed period invoke you switch statement

  • think of your servoMotor variable as a step in a sequence instead of just what you want to do. "servomotor" determines the next thing to do, possibly the time until the next event and is either incremented in value or set to zero when the sequence is completed. (of course, the sequences can be more that just moving back and forth

  • when servomotor is not zero, a button press can force it to zero.

of course a bunch of ideas. work on each then the next

Thank you all for quick replay and information i will try to learn more about millis() and see what happens. Again thank you!