I'm trying to make a button only be able to be pressed every _ hours

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;
}

I guess you could add a 1 hour delay before reading the switch.
Paul

Oh! good idea, I bet that will work, thanks!

To avoid frustration you should probably have an LED that turns on when it is permitted to press the button.

Or else, perhaps, check button presses regularly and if the time is not right print a message telling the user how much longer he will have to wait before a press is acted on.

...R

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.