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:
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(PB_PIN, INPUT_PULLUP);
pinMode(PB_PIN2, INPUT_PULLUP);
pinMode(PB_PIN3, INPUT_PULLUP);
pinMode(PB_PIN4, INPUT_PULLUP);
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
else
{
// Resets the flags
transientPeriodStarted = false;
bPressAccepted = false;
}
}