int buttonPin = 10; // Pin connected to the button
int led1Pin = 5; // Pin connected to LED1
int led2Pin = 2; // Pin connected to LED2
int sensor = A0;
bool buttonState = LOW; // Current state of the button
bool previousButtonState = LOW; // Previous state of the button
bool led1State = LOW; // Current state of LED1
bool led2State = LOW; // Current state of LED2
int sensordata = 0;
bool check = LOW;
unsigned long buttonPressTime = 0; // Time when the button was pressed
const unsigned long led1OnTime =3000; // LED1 on time in milliseconds
const unsigned long led2OnTime = 15000; // LED2 on time in milliseconds
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with internal pull-up resistor
pinMode(led1Pin, OUTPUT); // Set LED1 pin as output
pinMode(led2Pin, OUTPUT); // Set LED2 pin as output
}
void loop() {
buttonState = digitalRead(buttonPin); // Read the state of the button
sensordata = analogRead(sensor);
// Check if the button is pressed and the previous state was not pressed
if (buttonState == LOW && previousButtonState == HIGH) {
buttonPressTime = millis(); // Record the button press time
led1State = HIGH; // Turn on LED1
}
// Check if the LED1 on time has passed
if (led1State == HIGH && millis() - buttonPressTime >= led1OnTime) {
led1State = LOW; // Turn off LED1
led2State = HIGH; // Turn on LED2
check = HIGH;
}
// Check if the LED2 on time has passed
if (led2State == HIGH && millis() - buttonPressTime >= led1OnTime + led2OnTime) {
// Turn off LED2
led2State = LOW;
check = LOW;
}else{
if(sensordata >800 && check == HIGH){
led2State = LOW;
}
if(sensordata <400 && check == HIGH){
led2State =HIGH ;
}
}
digitalWrite(led1Pin, led1State); // Update LED1 state
digitalWrite(led2Pin, led2State); // Update LED2 state
previousButtonState = buttonState; // Update previous button state
}