Hi- I am very new to programming, and am stuck. I need to read the state of a button press, and if it's held longer than 500ms, I would like to turn an LED on. If it's < 500ms, the loop should continue. I"m frustrated because this seems like it should be easy, but my code isn't working:
//GSwitch timer test
const int ledPin = 11;
const int startPin = 10;
long startTime;
unsigned long duration;
void setup()
{
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
pinMode(startPin, INPUT);
digitalWrite(startPin, HIGH);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(digitalRead(startPin) == LOW)
{
// here if the switch is pressed
startTime = millis();
while(digitalRead(startPin) == LOW)
; // wait while the switch is still pressed
duration = millis() - startTime;
}
if(duration >= 500);
{
digitalWrite (ledPin,HIGH);
Serial.println(duration);
}
}
When I watch in the serial monitor, I can see the button presses registering and reasonable times are being reported out, but within half a second of resetting, my LED pin goes high (#11), regardless of button activity.