#define BUTTON_PIN 2
#define LED_PIN 4
//led blinking lgoic
unsigned long LastBlink=0;
const unsigned long interval=500;
int lastState=LOW;
bool ledblinkstate=true;
//debounce state
unsigned long lastdebounce = 0;
unsigned long debouncedelay = 50;
int buttonstate = HIGH;
int lastButtonReading = HIGH;
void setup()
{
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop()
{
unsigned long now=millis();
// Button debounce check + BUTTON CHECK//
int reading = digitalRead(BUTTON_PIN);
if(reading != lastButtonReading)
{
lastdebounce = now;
}
if(now-lastdebounce > debouncedelay)
{
if(reading != buttonstate)
{
buttonstate = reading;
if(buttonstate == LOW)
{
ledblinkstate = false;
digitalWrite(LED_PIN, LOW);
}
else
{
ledblinkstate = true;
}
}
}
lastButtonReading = reading;
//led on state se blinking state mein convert karna
if(ledblinkstate)
{
if((now-LastBlink) >= interval)
{
LastBlink = now;
lastState =! lastState;
digitalWrite(LED_PIN, lastState);
}
}
}