I have no idea how to solve this problem at the moment. I have tried "fizzling" around with the milli() and the delay() but none of the things I tried worked out.
As to the layout of my code. I originally learned to program with K&R style and I have sticked to that since. I have never programming in this before only web dev and Python.
Whilst using millis() may be the preferred way to do this as the technique will be needed eventually in a program, the objective of locking out use of the button is easily achieved by using delay()
start of loop
if button press detected
sweep the servo once each way
delay for 2 minutes (minus the time taken for the servo sweep if you want to be precise)
end of if
end of loop
Yes, I know that the delay will block the code for 2 minutes, but if it meets the stated requirement, so what ?
I could get that to work, here's my code as it as atm.
#include <Servo.h>
const int buttonPin = 2; //The number for the pushbutton in
Servo myservo;
int pos = 0; //variable to store the servo position
int buttonState = 0;
int lastButtonState = 0;
long previousMillis = 0;
long interval = 10000; // interval at which to blink (milliseconds)
void setup()
{
pinMode(buttonPin, INPUT);
myservo.attach(9); //attaches the servoon pin 9 to the servo object
}
void loop()
{
//Read the state of the pushbutton value:
buttonState= digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH){
for (pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >=1; pos -=1)
{
myservo.write(pos);
delay(15);
}
}
delay(120000);
}
lastButtonState = buttonState;
}