Incorporating button into Servo Loop. Help!

I am extremely new so please bare with me. I am attempting to control a servo by both the internal timer and by button. Essentially the servo will open a door for 6 seconds every 24 hours (i know the delay time isn't right) OR if you press the button

The loop for the timer works but the button doesn't. However if i just upload the button code it works fine. Help please! Even better if the servo could go to 180 when the button is held down and return to 0 when released would be ideal.

Here is my code. Tell me where I messed up please!


#include <Servo.h>
Servo myservo;
const int BUTTON_PIN = 8;
const int SERVO_PIN  = 9;

int angle = 0;          // the current angle of servo motor
int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button

void setup(){
  myservo.attach(SERVO_PIN);
Serial.begin(9600); 
 pinMode(BUTTON_PIN, INPUT_PULLUP);
 myservo.write(angle);
  currentButtonState = digitalRead(BUTTON_PIN);
  } 
  
void loop(){

  myservo.write(0);
  delay(18000);
   myservo.write(180);
  delay (6000);

  lastButtonState    = currentButtonState;      // save the last state
  currentButtonState = digitalRead(BUTTON_PIN); // read new state

  if(lastButtonState == HIGH && currentButtonState == LOW) {
    Serial.println("The button is pressed");

    // change angle of servo motor
    if(angle == 180)
      angle = 0;
    else
    if(angle == 0)
      angle = 180;

    // control servo motor arccoding to the angle
    myservo.write(angle);
  }
}

You will want to read about millis() for timing.

I think you may be better off using a RTC module for the time thingy.

The button don't work cause when the delay is doing the thing the CPU is doing NOP's (no operations) and cannot detect the button press.

put the first 4 lines of the loop() in a condition which allows them to run only every 24 hours. now write(0) executes immediately after write(angle)

Thanks. Can you please elaborate on how to "put the first 4 lines of the loop() in a condition which allows them to run only every 24 hours."'

Someone else gave me this code which allows the button to start the cycle but the servo only goes to 180 when I hit the button. Any ideas on how to allow the 24 hour cycle to play out but be overridden by pressing the button?

#include <Servo.h>
Servo myservo;
const int BUTTON_PIN = 7;
const int SERVO_PIN  = 9;

void setup(){
  myservo.attach(SERVO_PIN);
  Serial.begin(9600); 
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  myservo.write(0);
  } 
  
void loop(){
  if(!digitalRead(BUTTON_PIN))
  {
    myservo.write(180); //(open?)
    delay(6000);
    myservo.write(0);
    delay(86400000);
  }
}

Okay I now know I need to use the blink without delay method but have no idea how so if anyone can help!

using millis for timing clicky clicky.

@walden7788, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

Thanks for using code tags in your first post.

Do note that there is generally no need to delete posts as you can use the "edit" function - the pencil icon - to correct them.

This could be interpreted 3 different ways:

  1. Button triggers the servo to go to 180 degrees for 6 seconds and then servo goes to 0. Next automatic cycle happens 24 hours after the button is pressed.

  2. Button triggers the servo to go to 180 degrees for 6 seconds and then servo goes to 0. Next automatic cycle happens 24 hours after the last 24 hour cycle completed.

  3. Button interrupts the 6 second delay in an automatic cycle and returns the servo to 0.

Which is it?

It was suggested that you might want a RTC to handle the 24 hour interval. If you need the cycle to happen at the same time every day and with accuracy then I would recommend an RTC. If all you require is that the cycle happen approximately every 24 hours and time of day is not a concern then using the millis() timer to accomplish this is probably OK.