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);
}
}
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?
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.
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.
Button interrupts the 6 second delay in an automatic cycle and returns the servo to 0.
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.