Hi
My concept is if button is pressed less than 10 seconds then led should turn on.
if button is pressed more than 10 seconds then device should reset.
problem is device does not reset if i press the button more than 10 seconds.
to reset the device i have connected digital pin to reset pin of arduino.
can someone please check the code and help me
const int ledPin = 5;
const int ledPin1 = 6;
const int buttonPin = 3;
int Reset = 10;
int programState = 0;
int buttonState;
long buttonMillis = 0;
const long intervalButton = 10000;
long ledMillis = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, LOW);
digitalWrite(ledPin, LOW);
Serial.println("siren connection on ");
digitalWrite(ledPin1, LOW);
Serial.print("buzzer connection off ");
digitalWrite(Reset, HIGH);
delay(200);
pinMode(Reset, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);
{
if (buttonState == HIGH && programState == 0) {
buttonMillis = currentMillis;
programState = 1;
}
else if (programState == 1 && buttonState == LOW) {
programState = 0; //reset
}
if(currentMillis - buttonMillis < intervalButton && programState == 1) {
programState = 2;
ledMillis = currentMillis;
digitalWrite(ledPin, HIGH);
Serial.print("SIREN connection off ");
Serial.print (" / ");
if (digitalRead(ledPin) == HIGH) {
digitalWrite(ledPin1, HIGH);
Serial.print("buzzer connection on ");
}
}
}
{if (buttonState == HIGH && programState == 0) {
buttonMillis = currentMillis;
programState = 1;
}
else if (programState == 1 && buttonState == LOW) {
programState = 0; //reset
}
if(currentMillis - buttonMillis > intervalButton && programState == 1) {
programState = 2;
ledMillis = currentMillis;
digitalWrite(Reset, LOW);
Serial.println("reset done");
}
}}