Getting the Arduino to return to start mode

Hi, i have built a simple project that moves a servo.

The idea is that when I press the switch it moves the servo to a position, waits fot 4 sec, then moves it to another position, and waits for 40 sec.
this cycle is repeated five times and than it should wait for an other press on the switch again and do it all again.

Everything works as it should except I can't get it to start again with the second press.

Can anyone give me a hint what's wrong in my code?

#include <Servo.h>

Servo myservo;
const int switchPin = 2; // set switch input pin
int switchState = HIGH; // initial state of switch
int lastSwitchState = HIGH; // last state of switch
unsigned long lastDebounceTime = 0; // last time switch state changed
unsigned long debounceDelay = 50; // debounce delay in milliseconds

void setup() {
  myservo.attach(3);  // attach servo to pin 3
  pinMode(switchPin, INPUT_PULLUP); // set switch pin as input with pull-up resistor
  myservo.write(53);   // move servo to 53 degrees
}

void loop() {
  switchState = digitalRead(switchPin); // read switch state
  if(switchState != lastSwitchState) { // check if switch state has changed
    lastDebounceTime = millis(); // reset debounce timer
  }
  if((millis() - lastDebounceTime) > debounceDelay) { // check if debounce delay has passed
    if(switchState == LOW) { // check if switch is pressed
      for(int i=0; i<5; i++) {  // repeat the cycle 5 times
        myservo.write(23);   // move servo to 23 degrees
        delay(4000);         // wait for 4 seconds
        myservo.write(53);   // move servo to 53 degrees
        delay(40000);        // wait for 40 seconds
      }
      myservo.detach();      // stop the servo
    }
  }
  lastSwitchState = switchState; // update last switch state 
}

your comment describes not what is happening.
If you analyse carefully you set the servo to angle 23
what happens after this command?
Your servo moves quickly to angle 23 and then stops and then stays at angle 23 for 4 seconds
Because the microcontroller is creating the according servo-signal of angle 23 all the time continiously

then you set servo to angle 53
Your servo moves quickly to angle 53 and then stops and then stays at angle 53 for 40 seconds
Because the microcontroller is creating the according servo-signal for the given angle all the time continiously

A servo stays at the last given position and is "stopped" because the microcontroller is creating the according servo-signal of angle angle all the time continiously

until you execute

myservo.write(angle); 

with a different angle
.
.
.

myservo.detach();

does the following:
stop creating the servo-signal which is almost the same as cut the servo-signal-wire

simply remove the

myservo.detach();

and it will work

You should learn what kind of signal servos expect and how servos internally work to fully understand this

best regards Stefan

1 Like

Thank you!
Great explaining.
That did it!

Its my first project and that line was left behind from a earlier attempt :slight_smile:

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