Devices & Components
Arduino Uno Rev3
LEDs
Analog joystick (Generic)
Jumper wires (generic)
Male/Female Jumper Wires
Resistor 221 ohm
Project description
Code
WhackALED
arduino
1// Whack a LED 2 3// Joystick variables 4int sX = A0; //joystick x axis, analog input 5int sY = A1; //joystick y axis, analog input 6int sSX; //state of x, reading from sX 7int sSY; //state of y, reading from sY 8int sS; //converted state (may not be most efficient) 9 10// Game variables 11int rNum; //random int choosing the random light to turn on 12int wins=0; //counting consecutive wins 13int highScore=0; //saving the highest score of consecutive wins 14 15// Difficulty constants, time to react 16const int easy=1000; 17const int medium=500; 18const int hard=325; 19const int stupid=250; 20 21 22int ledPins[]={5,2,4,3,6,7,8}; //initializing led's 23int pinCount=7; //number of led pins 24 25void setup() { 26 27Serial.begin(9600); 28pinMode(sX, INPUT); 29pinMode(sY, INPUT); 30 31for (int thisPin = 0; thisPin < pinCount; thisPin++) { //assigning all the pins as outputs in a for loop 32 pinMode(ledPins[thisPin], OUTPUT); 33 } 34} 35 36void loop() { 37 38 rNum=random(4); //generating random choice 39 delay(1000); 40 digitalWrite(ledPins[rNum], HIGH); //lighting the randomly chosen bulb 41 delay(stupid); //difficulty 42 43 //stick stuff 44 sSX = analogRead(sX); //reading x axis input 45 delay(100); 46 sSY = analogRead(sY); //reading y axis input 47 48 //converting y and x inputs to 4 possibilities. 0 and 1023 are the maximum values on each axis of the joystick, as measured. 49 sS=0; 50 switch (sSX) { 51 case 0: 52 sS=1; // Left 53 break; 54 case 1023: 55 sS=2; // Right 56 break; 57} 58switch (sSY) { 59 case 0: 60 sS=3; // Up 61 break; 62 case 1023: 63 sS=4; // Down 64 break; 65} 66 67 digitalWrite(ledPins[rNum], LOW); //turning off the randomly selected bulb, after the joystick choice was made 68 if (sS-1==rNum) //checking if the user input the correct direction of the lit bulb 69 { 70 wins++; 71 for (int k=0; k<=3; k++) { //blinking green light indicating correct choice 72 digitalWrite(ledPins[4], HIGH); 73 delay(50); 74 digitalWrite(ledPins[4], LOW); 75 delay(50); 76 } 77 } 78 else 79 { 80 if (wins>highScore) { //if the consecutive wins are more than the previous highscore, the new highscore is set. 81 highScore=wins; 82 wins=0; 83 } 84 for (int i=0; i<=3; i++) { //blinking red light indicating incorrect choice 85 digitalWrite(ledPins[5], HIGH); 86 delay(50); 87 digitalWrite(ledPins[5], LOW); 88 delay(50); 89 } 90 for (int w=0; w<highScore; w++) { //displaying via counting and blinking the high score on a yellow bulb. 91 digitalWrite(ledPins[6], HIGH); 92 delay(200); 93 digitalWrite(ledPins[6], LOW); 94 delay(200); 95 } 96 } 97 } 98 99 100
Downloadable files
LED Whack-a-mole schematic
Everything should match the real picture of the project except for the joystick. My joystick has 5 pins, 4 or which I have used: named from top to bottom "GND", "+5V", "VRx", and "VRy". "GND" and "+5V" are connected to GND and 5V on the arduino, "VRx" and "VRy" are connected to A0 and A1.
LED Whack-a-mole schematic
Whack-a-mole LED
Everything should match the real picture of the project except for the joystick. My joystick has 5 pins, 4 or which I have used: named from top to bottom "GND", "+5V", "VRx", and "VRy". "GND" and "+5V" are connected to GND and 5V on the arduino, "VRx" and "VRy" are connected to A0 and A1.
Whack-a-mole LED

Real Picture
Real Picture
Whack-a-mole LED
Everything should match the real picture of the project except for the joystick. My joystick has 5 pins, 4 or which I have used: named from top to bottom "GND", "+5V", "VRx", and "VRy". "GND" and "+5V" are connected to GND and 5V on the arduino, "VRx" and "VRy" are connected to A0 and A1.
Whack-a-mole LED

Real Picture
Real Picture
LED Whack-a-mole schematic
Everything should match the real picture of the project except for the joystick. My joystick has 5 pins, 4 or which I have used: named from top to bottom "GND", "+5V", "VRx", and "VRy". "GND" and "+5V" are connected to GND and 5V on the arduino, "VRx" and "VRy" are connected to A0 and A1.
LED Whack-a-mole schematic
Comments
Only logged in users can leave comments