//Using an LED, a resistor, a pushbutton, arduino nano, in WokWi:
// 1. on one press of the button(Less than 2 seconds)
// turns the LED ON if it is already off or OFF if it is already on
// 2. For a holding press greater than of 2 seconds, let it blink on and off repeatedly
// until the botton is pressed again
//Tip:
//Utilise internal pullup resistor, and internal timer counter
//using pinMode(pin,INPUT_PULLUP), analogWrite(pin,VAL) and millis()
//Refer to arduino tutorial slides for assistance.
const int ledPin = 10; // Pin to the LED
const int buttonPin = 9; // Pin to the pushbutton
int t1=0;
int a=0;
int state=1;
int blink=0;
unsigned long millTime = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
unsigned long milsrtime = millis();
while(digitalRead(buttonPin) == 0)
{
millTime = millis() - milsrtime;
}
if ((millTime <2000 ) && (millTime>0)){// When the button is held down for less than two seconds
while(digitalRead(buttonPin)==1){
if( digitalRead((ledPin)==HIGH))
digitalWrite(ledPin,LOW);
else
digitalWrite(ledPin,HIGH);
millTime=0;
}
}
if (millTime>2000){
delay(100);
while(digitalRead(buttonPin)==1){
t1=millis();
while((millis()-t1<1000)&&(digitalRead(buttonPin)==1));
digitalWrite(ledPin,HIGH);
while((millis()-t1<2000)&&(digitalRead(buttonPin)==1));
digitalWrite(ledPin,LOW);
Serial.print(digitalRead(buttonPin));
Serial.print(digitalRead(ledPin));
}
millTime=0;
}
millTime=0;
}