Use the following code for Blinking Lights Game to program your Arduino and Learning Board.
The LED pin numbers are not complete. Each learning board is unique so you need to look to where the LEDs are hooked up.
The pre-Setup and Setup Code are in the correct order, but it is up to you to rearrange the void loop code to make the game function.
Good luck and don't let little failures stop you.
"Learning is doing something and then saying, 'I shouldn't do that again.'"
- Douglas Adams
/* This program is meant to be used with a learning board
* that snaps onto an Arduino UNO R3 with the following specs.
* 6 LEDs are connected to a series of pins.
* One pin is connected to a pullup resistor and a pushbutton
* One pin is hooked up to an always HIGH pin as a 5V jumper
*/
int LED_1 = ?;
int LED_2 = ?;
int LED_3 = ?;
int LED_4 = ?;
int LED_5 = ?;
//Use this section to define which pins your LEDs are hooked up to.
int winningLED = ?;
//This names the special winning LED
int 5V_PIN = ?;
//Look for a red wire on your board. Enter whichever pin it is on here
int pushButton = ?;
//This lets us use the variable pushButton
//through the code and makes switching which pin it is connected
//to easier
int ledCounter = LED_1;
//This counter will be used to determine which LED should be on at any point
int timer = 250;
//This variable will be used to quickly state how long to delay between each
//light flash
void setup() {
// put your setup code here, to run once:
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
pinMode(LED_4, OUTPUT);
pinMode(LED_5, OUTPUT);
pinMode(LED_6, OUTPUT);
pinMode(5V_PIN, OUTPUT);
pinMode(pushButton, INPUT);
//this code sets up the LEDs and Pushbutton
digitalWrite(5V_PIN, HIGH);
//This code sets your 5V pin to high(5V)
}
void loop() {
// put your main code here, to run repeatedly:
}
//A - This if statement checks if the correct LED is on and turns on the
//winningLED if it is
if (ledCounter == LED_5)
{
digitalWrite(winningLED, HIGH);
}
//B -This pauses so that we can enjoy the LED's glow
delay(timer);
//C - This while loop will stop the lights if the pushbutton is on
while (digitalRead(pushButton) == 1)
{
}
//D - This code turns all of the light off for the next time this loops
digitalWrite(winningLED, LOW);
digitalWrite(ledCounter, LOW);
//E - This increments the LED counter to turn on a new LED the next time it loops
ledCounter = ledCounter + 1;
//F - This resets the ledCounter if it goes past the last green LED
if (ledCounter == LED_5)
{
ledCounter = LED_1;
}
//G - This turns one LED on
digitalWrite(ledCounter, HIGH);
//Remember that if an open bracket is created ( { you must close that bracket ) } .
//A-G Are out of order. Fix them.
No comments:
Post a Comment