Hello!
I am very new to Arduino and am trying to make something where a button turns a servo but can only be pressed every _ hours.
I am using an Arduino Uno and a SG-5010 tower pro servo.
I have code for the servo to turn when a button is pressed but dont know about timers on buttons.
If you have any ideas please add!
Thanks!
code:
#include <Servo.h>;
// pushbutton pin
const int buttonPin = 8;
// servo pin
const int servoPin = 3;
Servo servo;
//create a variable to store a counter and set it to 0
int counter = 0;
void setup()
{
servo.attach (servoPin);
// Set up the pushbutton pins to be an input:
pinMode(buttonPin, INPUT);
}
void loop()
{
// local variable to hold the pushbutton states
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) // light the LED
{
counter++;
delay(150);
}
if(counter == 0)
servo.write (20); // zero degrees
else if(counter == 1)
servo.write(180);
else if(counter == 2)
servo.write (20);
//else reset the counter to 0 which resets thr servo to 0 degrees
else
counter = 0;
}