Servo must turn with 5 sec delay after pushing button

I have the Arduino Code in the attachment. It turns on the servo if the button is pushed off. But I don't need a direct turn after the push button is changed, but I want it to turn 5 seconds after changing the push button. What I've to change in my code? Thanks in advance!!

(see attachment for code)

Altduino.ino (413 Bytes)

When the code is that small, don't bother attaching it, just post it surrounded by code tags. If your code doesn't have to do ANYTHING* during those 5 seconds, then use delay(). Otherwise, see the BlinkWithoutDelay example on how to perform timed actions without having to block the code.

  • Except service interrupts.

Either way I suggest that you always use braces round the lines to be executed in if/else (and other) commands as it makes it much clearer what is going on. Putting each brace on a new line helps too.

Your code reformatted.

#include <Servo.h>

Servo myservo; // creating myservo object
int buttonPin = 2;
int servoPin = 13;
int buttonState = 1; // set buttonState 

void setup()
{
  myservo.attach(servoPin); 
  pinMode(buttonPin, INPUT); 
}

void loop()
{ 
  buttonState = digitalRead(buttonPin); // read and save to the variable "buttonState" the actual state of button
  if (buttonState == LOW)
  {
    myservo.write(150); 
  }
  else
  {
    myservo.write(0);
  }
}