Skip to content

Instantly share code, notes, and snippets.

@jrmedd
Last active November 29, 2018 01:00
Show Gist options
  • Save jrmedd/5dae73c22907d28fdd56d447ce5339d4 to your computer and use it in GitHub Desktop.
Save jrmedd/5dae73c22907d28fdd56d447ce5339d4 to your computer and use it in GitHub Desktop.
Code for Awkward Arduino workshop – fastest finger.
/*
AWKWARD ARDUINO – FASTEST FINGER
*/
/*
We're going to connect LEDs to pins 3, 4, and 5.
These will act as winning lightts for the green
and blue team, and the red light that tells you
when to press!
*/
int greenTeamLED = 3;
int blueTeamLED = 4;
int pressLED = 5;
int greenTeamButton = 6;
int blueTeamButton = 7;
/*
The boolean variable that lets our program know
if we're waiting for the presses.
*/
boolean waitingForPress = false;
/*
Variable to track the time to wait before turning
on the press LED, and the the time at which we
start waiting.
*/
float pressTime;
float timeStart;
/*
The setup portion of our program runs once each
time the Arduino is turned on or reset. Each
instruction is run once, then we're done with
setup.
*/
void setup() {
/*
Make LED pins outputs
*/
pinMode(greenTeamLED, OUTPUT);
pinMode(blueTeamLED, OUTPUT);
pinMode(pressLED, OUTPUT);
/*
Make button pins inputs
*/
pinMode(greenTeamButton, INPUT);
pinMode(blueTeamButton, INPUT);
}
/*
The loop portion of our program runs after setup
is done, and keeps looping until we turn the
Arduino off. When the last instruction is
exectued, it starts again from the beginning.
*/
void loop() {
/*If we're waiting for a button press, run what's
inside this section*/
if (waitingForPress) {
/*
Is millis() is telling us we've waited less time
than the random value we've set, then run what's here.
*/
if (millis() - timeStart < pressTime) {
/*
digitalWrite() means that we're telling a pin to
do something. LOW means 0 volts, so we're telling
the LED to stay switched off here!
*/
digitalWrite(pressLED, LOW);
}
/*
If we have been waiting for longer than the time we set
earlier, then run what's inside here!
*/
else {
/*
We want to tell the players to press, so let's turn
the LED on! HIGH means 5 volts, which means we're going
to see the LED light up!
*/
digitalWrite(pressLED, HIGH);
/*
Digital read will see either HIGH or
LOW. If it's high, then it's being pressed!
First we want to see if the green team's button is
being pressed.
*/
if (digitalRead(greenTeamButton)) {
/*
Turn off the press LED now that someone's pressed!
*/
digitalWrite(pressLED, LOW);
/*
Turn on the green LED to let them know that they won
*/
digitalWrite(greenTeamLED, HIGH);
/*
Wait a second and half to let them bask in the glory
*/
delay(1500);
/*
Turn off their LED
*/
digitalWrite(greenTeamLED, LOW);
/*
Now we're not waiting for a press, tell the program.
This way, on the next loop, we'll set a new waiting
time for the next round!
*/
waitingForPress = false;
}
/*
If the green team isn't pressing, see if the blue team
is! All of this happens much more quickly than humans
can press buttons, so it doesn't make much difference
which team gets checked first!
*/
else if (digitalRead(blueTeamButton)) {
digitalWrite(pressLED, LOW);
digitalWrite(blueTeamLED, HIGH);
delay(1500);
digitalWrite(blueTeamLED, LOW);
waitingForPress = false;
}
}
}
/*
If we're not waiting for a button press, run
what's inside here.
*/
else {
/*
Pick and random number between 1000 and
10000. This is the number of milliseconds
you'll have to wait before the press LED
lights up!
*/
pressTime = random(1000, 10000);
/*
millis() tells the program how long it's
been running for. We're saving it to a
variale here so we see how long we've been
waiting later on.*/
timeStart = millis();
/*
Now we're going to wait for the button to be
pressed, and the program will run slightly
differently next time it loops around.
*/
waitingForPress = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment