Question: Open the Arduino IDE and enter the following code: const int switchPin = 2; // named constant for the switch pin unsigned long previousTime =
Open the Arduino IDE and enter the following code: const int switchPin = 2; // named constant for the switch pin unsigned long previousTime = 0; // store the last time an LED was updated int switchState = 0; // the current switch state int prevSwitchState = 0; // the previous switch state int led = 3; // a variable to refer to the LEDs int interval = 5000; // interval at which to light the next LED (5 s) void setup() { // set the LED pins as outputs and turn LEDs off for(int i = 3; i < 6; i++){ pinMode(i, OUTPUT); digitalWrite(i, LOW); } // set the switch pin as input pinMode(switchPin, INPUT); }
void loop(){ // store the time since the Arduino started running in a variable unsigned long currentTime = millis(); // compare the current time to the previous time an LED turned on // if it is greater than your interval, turn next LED on if(currentTime - previousTime > interval && led < 6) { // save the current time as the last time you changed an LED previousTime = currentTime; // Turn the LED on digitalWrite(led, HIGH); // increment the led variable // in 5 s the next LED will light up led++; } // read the switch value switchState = digitalRead(switchPin); // if the switch has changed if(switchState != prevSwitchState){ // turn all the LEDs low for(int i = 3; i < 6; i++){ digitalWrite(i, LOW); } // reset the LED variable to the first one led = 3; //reset the timer previousTime = currentTime; } // set the previous switch state to the current state prevSwitchState = switchState; }
Task 5 Add the second S2 switch to your project such that the hourglass will only operate if S2 is pressed, i.e. the LEDs should be off if S2 is not pressed. Note: take care to ensure that the green LED does not turn on straight away when S2 is pressed, i.e. if the interval is set to 5 s, it should take a minimum of 5 s after S2 is pressed before an LED turns on.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
