Switching different sequences of rotation of servo motor

i create 3 different loops sequences for servo motor in as below in code.
and i want to run these loops from 3 input buttons.



#include <Servo.h>

Servo myservo;  

int Seq = 0;
void setup() {
  
  Serial.begin(9600);
  myservo.attach(D0);  // attaches the servo on pin 9 to the servo object
    pinMode(D1, INPUT_PULLUP);
      pinMode(D2, INPUT_PULLUP);
        pinMode(D3, INPUT_PULLUP);
      
}

void loop() {
    if (digitalRead(D1) == LOW) { Seq = 1;    }
      if (digitalRead(D2) == LOW) { Seq = 2;    }
        if (digitalRead(D3) == LOW) { Seq = 3;    }

        if (Seq == 1) { loop1 ();    }  
         if (Seq == 2) { loop2 ();    }
          if (Seq == 3) { loop3 ();    }
                      
}
 void loop1 () {
    Serial.println("loop1 running");
     myservo.write(0);      delay(2000); 
   for (int A = 0; A <91; A++) {   myservo.write(A);      delay(10);    }  // 0 to 90
   delay(2000);
    for (int A = 90; A <181; A++) {   myservo.write(A);     delay(10);    }   // 90 to 180
    delay(2000);
     for (int A = 180; A > 89; A--) {   myservo.write(A);     delay(10);    }  // 180 to 90
    delay(2000);
      for (int A = 90; A >(-1); A--) {   myservo.write(A);     delay(10);    }  // 90 to 0
    delay(2000);
 }
 void loop2 () {
    Serial.println("loop2 running");
     myservo.write(0);      delay(2000); 
   for (int A = 0; A <91; A++) {   myservo.write(A);      delay(10);    }  // 0 to 90
   delay(2000);
      for (int A = 90; A >(-1); A--) {   myservo.write(A);     delay(10);    }  // 90 to 0
    delay(2000);
 }
  void loop3 () {
    Serial.println("loop3 running");
     myservo.write(0);      delay(2000); 
 }

when any button is pressed, loop (accord to given Seq )should be start immediately from beggining.but because i have used delay(); function in blocks. it failed.
how to modify this code removing delay(); function ???

Two suggestions:

  • Format your code so it can be more easily followed.
    In the Arduino IDE, use Ctrl T or CMD T.
    Place { and } on separate lines by themselves.

  • Look into how millis( ) can be used to create a non blocking TIMER, BWD (Blink Without Delay).
    Look at how a State Machine works.

Examples for above in the Tutorial section on this WEB site.

#include <Servo.h>

Servo myservo;

int Seq = 0;
unsigned long lastTime = 0;
unsigned long interval = 2000;
void setup() {
  Serial.begin(9600);
  myservo.attach(D0);
  pinMode(D1, INPUT_PULLUP);
  pinMode(D2, INPUT_PULLUP);
  pinMode(D3, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(D1) == LOW) { 
    Seq = 1;    
    lastTime = millis();
  }
  if (digitalRead(D2) == LOW) { 
    Seq = 2;    
    lastTime = millis();
  }
  if (digitalRead(D3) == LOW) { 
    Seq = 3;    
    lastTime = millis();
  }

  if (Seq == 1) { 
    loop1();
  }  
  if (Seq == 2) { 
    loop2();
  }
  if (Seq == 3) { 
    loop3();
  }
}

void loop1() {
  Serial.println("loop1 running");
  if (millis() - lastTime < interval) { 
    myservo.write(0);
  } else if (millis() - lastTime < 2 * interval) {
    myservo.write(map(millis() - lastTime, interval, 2 * interval, 0, 90));
  } else if (millis() - lastTime < 3 * interval) {
    myservo.write(90);
  } else if (millis() - lastTime < 4 * interval) {
    myservo.write(map(millis() - lastTime, 3 * interval, 4 * interval, 90, 0));
  } else {
    Seq = 0;
  }
}

void loop2() {
  Serial.println("loop2 running");
  if (millis() - lastTime < interval) {
    myservo.write(0);
  } else if (millis() - lastTime < 2 * interval) {
    myservo.write(map(millis() - lastTime, interval, 2 * interval, 0, 90));
  } else if (millis() - lastTime < 3 * interval) {
    myservo.write(map(millis() - lastTime, 2 * interval, 3 * interval, 90, 0));
  } else {
    Seq = 0;
  }
}

void loop3() {
  Serial.println("loop3 running");
  if (millis() - lastTime < interval) {
    myservo.write(0);
  } else {
    Seq = 0;
  }
}

Hope this helps(and is correct as I'm not good at prgramming). :sweat_smile:

  • D0, D1, D2 and D3 are not defined for Arduino
  • shoudln't Seq be cleared (e.g. 0) otherwise the sequence immediately repeats

If your board is an Uno or Nano, you should not use [edit] DIO pin 1 or DIO pin 0. Those pins [/edit] should be reserved for Hardware Serial (communication with the IDE serial monitor).

Your sketch has no need to read buttons or move servos while performing the assigned loop. Each "loop" should perform its task and return home. Only then should buttons be read again.

I see no reason for "millis()" in this sketch. "delay()" work fine here.

As long as ChatGPT didn't guess at it.

Thank you all of you for reply,
My servo motor stopped working,I will test it after buy new one. My board is esp8266. I will write next reply after get new servo motor.
Thank you.

what makes you sure that the servo-motor is damaged / destroyed?
there are several different reasons why the servo-motor does not "work"

broken wire GND, +5V, or signal-wire

connecting signal wire to the wrong IO-pin
failed power-supply.

In the meantime until your new servo arrives you could use the wokwi-simulation with an ESP32
to test the logic of your code.

where did you get this highly compressed hard to read code-layout from?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.