Toggling Leds Using Push Buttons


Jugaadu has an upcoming science exhibition is his school on this Sunday. Students are preparing some projects to be presented at the exhibition. Jugaadu decides to make an innovative game for exhibition teaming up with his friend. Can you give them an idea which can make them qualify for one of the best projects as well in the exhibition?

Bag Items:

  1. Foam sheet
  2. Arduino UNO
  3. Buzzer
  4. IR sensor
  5. Glue gun
  6. USB Cable
  7. Battery and DC Jack
  8. Cutter
  9. Jumper Wires
  10. Push Button
  11. Breadboard
We can think of making an innovative game which will glow the LED on pushing a particular pattern of push buttons and not just any pattern. Like out of 4 buttons, LED will glow by pressing only 1st and 4th push button otherwise not.

    int PB_PIN = 2;
int PB_PIN2 = 3;
int PB_PIN3 = 4;
int PB_PIN4 = 5;

int LED = 13;

// Duration of the transient period, in milliseconds
// Note: Giving a larger value won't hurt
const int TRANSIENT_PERIOD = 10;

// Flag to indicate the start of the transient period
boolean transientPeriodStarted = false;

// Flag to indicate the state of the LED
boolean ledOn = false;

// Flag to indicate if we have already accepted the
// button press and we've handled the event
boolean bPressAccepted = false;

// Represents a (reference) point in time
unsigned long timeRef = 0;

void setup()
// Sets the pin modes
pinMode(LED, OUTPUT);

void loop()
if (digitalRead(PB_PIN) == HIGH 
&& digitalRead(PB_PIN2) == HIGH 

// If transient response has just started
if (!transientPeriodStarted)
// Sets the flag for the start of the transient period
transientPeriodStarted = true;

// Updates the time reference
timeRef = millis();
// If the transient period has passed 
// and we haven't already accepted the button press 
else if (!bPressAccepted && (unsigned long)(millis() - timeRef) > TRANSIENT_PERIOD)
// Toggles the state of the LED
ledOn = !ledOn;
digitalWrite(LED, ledOn); 

// Updates the flag for the acceptance of the button press 
bPressAccepted = true;
// The button has been released
// Resets the flags
transientPeriodStarted = false;
bPressAccepted = false;