Unable to delay a servo motor spinning

Attached is a stolen servo library which I have modified

The issue I’m having is that I’m unable to put a delay on an individual movement

What I want to do is have one movement delayed by 10-20 seconds and the other have no delay, however, nothing I’ve tried has worked so far

Any suggestions would be fantastic

ServoPush.ino (1010 Bytes)

That’s not a library. It’s a program or sketch. It uses the Servo library via #include< Servo.h>;

If you want a 10 second delay after the button press before one of the writes to the servo just put delay(10000); before that write. Unless there’s something more complex about your requirement that you haven’t mentioned?

Steve

ocapaq:
have one movement delayed by 10-20 seconds

Does that mean you need to be able to choose a value between 10 and 20 while the program is running, or it must select a random number between those two limits while running, or just that you want to hard-code one particular value into the sketch, or what?

slipstick:
That’s not a library. It’s a program or sketch. It uses the Servo library via #include< Servo.h>;

If you want a 10 second delay after the button press before one of the writes to the servo just put delay(10000); before that write. Unless there’s something more complex about your requirement that you haven’t mentioned?

Steve

I have tried to do that and the software prompts me to add an if as seen in the first picture attached
It then prompts me to do several other things and I end up with a code like the second picture which just makes the servo spaz around without a button press after being uploaded

havingFunSoFar:
Does that mean you need to be able to choose a value between 10 and 20 while the program is running, or it must select a random number between those two limits while running, or just that you want to hard-code one particular value into the sketch, or what?

No sorry just using those times as an example, I need a singular value i.e. 20 seconds before the servo spins again

The delay() and servo.write() after the else if need to be in a {...}. Without the {...}, only the delay() is under the control of the else if, and the servo.write() will always happen

That also explains why the last else is complaining about no previous if- the ; after the delay closes the if/esle if, the servo.write() is a new command, and so the else at the bottom is orphaned from the if/else if which was already closed.

as seen in the first picture attached

Please don't post pictures of code and error messages. Post the text using code tags to make them easy to copy to an editor for examination.

UKHeliBob:
Please don’t post pictures of code and error messages. Post the text using code tags to make them easy to copy to an editor for examination.

Sorry my bad

havingFunSoFar:
The delay() and servo.write() after the else if need to be in a {…}. Without the {…}, only the delay() is under the control of the else if, and the servo.write() will always happen

So I’ve done that and now the servo won’t rotate back anymore?

Thanks for all the help so far lads and sorry for my late responses, I’ve been at work

ServoPush.ino (1.02 KB)

ocapaq:
So I’ve done that and now the servo won’t rotate back anymore?

Yeah I just meant it would fix the compile error, not that it would actually make the code do what you want it to :wink:

havingFunSoFar:
Yeah I just meant it would fix the compile error, not that it would actually make the code do what you want it to :wink:

Fair enough it did its job :slight_smile:

Sorry my bad

The code was attached, which is better than pictures of it, but posting it directly would have been even better.

 #include <Servo.h>;
 
 // pushbutton pin
 const int buttonPin = 8;

 // servo pin
 const int servoPin = 3;
 Servo servo;

unsigned long elapsedTime;
unsigned long onTime;

void setup()
{
 onTime = millis();
 onTime = elapsedTime;  
 servo.attach (servoPin);
  
 // Set up the pushbutton pins to be an input:
 pinMode(buttonPin, INPUT); 
}

void loop()
{
  int buttonState;  

  //read the digital state of buttonPin with digitalRead() function and store the           //value in buttonState variable
  buttonState = digitalRead(buttonPin);

  //if the button is pressed increment counter and wait a tiny bit to give us some          //time to release the button
  if (buttonState == LOW)
  {
    delay(150);
  }
 if (buttonState == LOW)   
        {
         servo.write(180);  // servo goes to 180 degrees  
         onTime = millis();  
         }
     if(onTime > 0 && millis() - onTime > 180000)  
           {      
           servo.write(0);  // servo goes to 0 degrees
            onTime = 0;
           }
           
}

This ended up being my code to make it work, the servo spins to the 180 degrees at the push of a button then after 3 minutes it spins back and stays there until the next button press, thank you guys for your help :slight_smile: