const int switchPin = 2;
const int ledPin = 13;
int buttonState;
int lightstate = 0;
int blinkstate = LOW;
long previousMillis = 0;
long interval = 1000;
unsigned long currentMillis = 0;
void setup() {
// put your setup code here, to run once:
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
buttonState = digitalRead(switchPin);
}
void loop() {
// put your main code here, to run repeatedly:
int val = digitalRead(switchPin);
delay(10);
int val2 = digitalRead(switchPin);
if(val == val2){
if (val != buttonState) { // change in buttonState
if (val == LOW) { // Check if button is released
if(lightstate == 0){
lightstate = 1;
digitalWrite(ledPin, HIGH);
Serial.println("State 1: LED is on");
//delay(1000);
currentMillis = 0;
previousMillis = 0;
blinkstate = LOW;
}
else {
lightstate = 0;
digitalWrite(ledPin, LOW);
blinkstate = LOW;
Serial.println("State 2: LED is off");
//delay(1000);
}
}
}
}
//////////////////////////Blink Section//////////////////////////////
if (lightstate = 1) {
currentMillis = millis();
if (currentMillis - previousMillis > interval){
previousMillis = currentMillis;
if (blinkstate = LOW)
blinkstate = HIGH;
else
blinkstate = LOW;
digitalWrite(ledPin, blinkstate);
}
}
///////////////////End of Blink section//////////////////////////////
buttonState = val;
}