EasyManua.ls Logo

SunFounder 3in1 Kit - Page 217

Default Icon
351 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
SunFounder 3in1 Kit
How it works?
1. Initialize the buttons and LEDs, 2 interrupt are used here to read the button status.
void setup()
{
...
attachInterrupt(digitalPinToInterrupt(buttonPin1), pressed1, FALLING);
attachInterrupt(digitalPinToInterrupt(buttonPin2), pressed2, FALLING);
...
}
2. If the rstBtn button is pressed, the game starts again. At a random time between 2 and 5ms, make one of the
LEDs light up.
void loop()
{
if (flag == -1 && digitalRead(rstBtn) == LOW) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
Serial.println("Waiting...");
int randomTime = random(2000, 5000);
delay(randomTime);
timer = millis();
flag = randomTime % 2;
Serial.println("Light!");
if (flag == 0) {
digitalWrite(ledPin1, HIGH);
} else if (flag == 1) {
digitalWrite(ledPin2, HIGH);
}
}
delay(200);
}
When flag is -1 and rstBtn button is pressed, use random() function to generate a random time
of 2-5s.
This time is then used to control the lighting of the LEDs.
Also the lighting of 2 LEDs is randomly generated by randomTime % 2 with 0 and 1. If flag is
0, then LED1 is lit; if 1, then LED2 is lit.
3. About pressed1() function
void pressed1() {
if (flag == -1) {
return;
}
if (flag == 0) {
int currentTime = millis();
Serial.print("Correct! You reaction time is : ");
(continues on next page)
4.6. 6. Funny Project 213

Related product manuals