Louie's Arduino Race Game
Play against a friend in this action packed Arduino game that is simple and easy to build!
Components and supplies
8
Resistor, 300 ohm
1
Solderless Breadboard Full Size
6
5 mm LED: Red
18
Wire, Hook Up
3
Through Hole Resistor, 200 ohm
1
Arduino UNO
2
5 mm LED: Yellow
3
Switch Actuator, Head for spring return push-button
Project description
Code
Arduino game
c_cpp
1//https://pastebin.com/HffCmD2V 2//https://hackaday.io/project/3477-race-to-the-led-arduino-game#menu-description 3//got code from this website and tweaked it with things I wanted and revisions 4 5const int button1 = 8; 6const int button2 = 4; 7const int reset = 13; 8 9const int lvl1led1 = 11; 10const int lvl2led1 = 10; 11const int lvl3led1 = 9; 12 13const int lvl1led2 = 5; 14const int lvl2led2 = 6; 15const int lvl3led2 = 7; 16 17const int counterled1 = 1; 18const int counterled2 = 0; 19 20 21int lastbuttonstate = 0; 22int lastbuttonstate2 = 0; 23 24 25 int goal = 60; 26 int state1 ; //didn't assign a value because will be assigned later 27 int state2 ; 28int stateReset ; 29 30 int counter1 = 0; //assigning 0 since the score would start at 0 31 int counter2 = 0; 32 33 34 void setup() { 35 36 pinMode(button1, INPUT); 37 pinMode(button2, INPUT); 38 pinMode(lvl1led1, OUTPUT); 39 pinMode(lvl1led2, OUTPUT); 40 pinMode(lvl2led1, OUTPUT); 41 pinMode(lvl2led2, OUTPUT); 42 pinMode(lvl3led1, OUTPUT); 43 pinMode(lvl3led2, OUTPUT); 44 pinMode(counterled1,OUTPUT); 45 pinMode(counterled2, OUTPUT); //declaring all outputs and inputs 46 pinMode(reset, INPUT); 47 48 } 49 50 void loop() { 51 52 state1 = digitalRead(button1); //set digitalread to state1 to check if button1 is pressed 53 state2 = digitalRead(button2); //set digitalread to state2 to check if button2 is pressed 54 stateReset = digitalRead(reset); //set digitalread to stateReset to check if reset is pressed 55 56// used nested if statements to prevent holding down button and getting clicks 57 58 if(state1 != lastbuttonstate) { //check if state1 is not equal to lastbuttonstate 59 60 if (state1 == HIGH) { //check if state1 is high 61 62 counter1 = counter1 + 1; // add 1 point to counter1 63 digitalWrite(counterled1, HIGH); 64 65 } else { //if state1 isn’t high run this code 66 digitalWrite(counterled1, LOW); 67 } 68 69 } 70 lastbuttonstate = state1; 71//this code prevents player 1 users from holding the button to get points 72 73 if(state2 != lastbuttonstate2) { //check if state2 is not equal to lastbuttonstate2 74 75 if(state2 == HIGH) { //check if state2 is high 76 77 counter2 = counter2 + 1; //add 1 point to counter2 78 digitalWrite(counterled2, HIGH); 79 } else{ // if state 2 is low run this code 80 81 digitalWrite(counterled2, LOW); 82 } 83 } 84 lastbuttonstate2 = state2; 85// this code prevents player 2 users from holding the button to get points 86 87 88//______________________________________ 89 90 if(counter1 >= 20) { //if counter1 reaches a score of 10 or more turn on first light 91 92 digitalWrite(lvl1led1, HIGH); 93 } 94 95 if(counter2 >= 20) { //if counter2 reaches a score of 10 or more turn on first light 96 97 digitalWrite(lvl1led2, HIGH); 98 99 } 100//_______________________________________ 101 102 if(counter1 >= 40) { //if counter1 reaches a score of 40 or more turn on second light 103 104 digitalWrite(lvl2led1, HIGH); 105 } 106 107 if(counter2 >= 40) { //if counter2 reaches a score of 40 or more turn on second light 108 109 digitalWrite(lvl2led2, HIGH); 110 } 111//________________________________________ 112 113 114 if (counter1 >= goal) { 115// if counter1 reaches 60 or more reset the opponent's lights and counter to indicate the win 116 117 digitalWrite(lvl3led1, HIGH); 118 119 counter2 = 0; 120 digitalWrite(lvl1led2,LOW); 121 digitalWrite(lvl2led2,LOW); 122 digitalWrite(lvl3led2,LOW); 123 } 124 125 126 127 128 129if (counter2 >= goal) { 130// if counter1 reaches 60 or more reset the opponent's lights and counter to indicate the win 131 132 digitalWrite(lvl3led2, HIGH); 133 counter1 = 0; 134 digitalWrite(lvl1led1,LOW); 135 digitalWrite(lvl2led1,LOW); 136 digitalWrite(lvl3led1,LOW); 137 } 138 139 if (stateReset == HIGH) { 140//if stateReset is equal to high reset all counters and turn off all lights 141 counter1 = 0; 142 counter2 = 0; 143 144 digitalWrite(lvl1led1,LOW); 145 digitalWrite(lvl2led1,LOW); 146 digitalWrite(lvl3led1,LOW); 147 digitalWrite(lvl1led2,LOW); 148 digitalWrite(lvl2led2,LOW); 149 digitalWrite(lvl3led2,LOW); 150 } // made own reset button so void setup wouldn’t have to run again. 151// resets the game instantly 152 153 } 154
Arduino game
c_cpp
1//https://pastebin.com/HffCmD2V 2//https://hackaday.io/project/3477-race-to-the-led-arduino-game#menu-description 3//got 4 code from this website and tweaked it with things I wanted and revisions 5 6const 7 int button1 = 8; 8const int button2 = 4; 9const int reset = 13; 10 11const 12 int lvl1led1 = 11; 13const int lvl2led1 = 10; 14const int lvl3led1 = 9; 15 16const 17 int lvl1led2 = 5; 18const int lvl2led2 = 6; 19const int lvl3led2 = 7; 20 21const 22 int counterled1 = 1; 23const int counterled2 = 0; 24 25 26int lastbuttonstate 27 = 0; 28int lastbuttonstate2 = 0; 29 30 31 int goal = 60; 32 int state1 ; //didn't 33 assign a value because will be assigned later 34 int state2 ; 35int stateReset 36 ; 37 38 int counter1 = 0; //assigning 0 since the score would start at 0 39 40 int counter2 = 0; 41 42 43 void setup() { 44 45 pinMode(button1, INPUT); 46 47 pinMode(button2, INPUT); 48 pinMode(lvl1led1, OUTPUT); 49 pinMode(lvl1led2, 50 OUTPUT); 51 pinMode(lvl2led1, OUTPUT); 52 pinMode(lvl2led2, OUTPUT); 53 pinMode(lvl3led1, 54 OUTPUT); 55 pinMode(lvl3led2, OUTPUT); 56 pinMode(counterled1,OUTPUT); 57 pinMode(counterled2, 58 OUTPUT); //declaring all outputs and inputs 59 pinMode(reset, INPUT); 60 61 62 } 63 64 void loop() { 65 66 state1 = digitalRead(button1); //set 67 digitalread to state1 to check if button1 is pressed 68 state2 = digitalRead(button2); 69 //set digitalread to state2 to check if button2 is pressed 70 stateReset = 71 digitalRead(reset); //set digitalread to stateReset to check if reset is pressed 72 73 74// used nested if statements to prevent holding down button and getting clicks 75 76 77 if(state1 != lastbuttonstate) { //check if state1 is not equal to lastbuttonstate 78 79 80 if (state1 == HIGH) { //check if state1 is high 81 82 counter1 83 = counter1 + 1; // add 1 point to counter1 84 digitalWrite(counterled1, HIGH); 85 86 87 } else { //if state1 isn’t high run this code 88 digitalWrite(counterled1, 89 LOW); 90 } 91 92 } 93 lastbuttonstate = state1; 94//this code 95 prevents player 1 users from holding the button to get points 96 97 if(state2 98 != lastbuttonstate2) { //check if state2 is not equal to lastbuttonstate2 99 100 101 if(state2 == HIGH) { //check if state2 is high 102 103 counter2 104 = counter2 + 1; //add 1 point to counter2 105 digitalWrite(counterled2, HIGH); 106 107 } else{ // if state 2 is low run this code 108 109 digitalWrite(counterled2, 110 LOW); 111 } 112 } 113 lastbuttonstate2 = state2; 114// this code prevents 115 player 2 users from holding the button to get points 116 117 118//______________________________________ 119 120 121 if(counter1 >= 20) { //if counter1 reaches a score of 10 or more 122 turn on first light 123 124 digitalWrite(lvl1led1, HIGH); 125 } 126 127 128 if(counter2 >= 20) { //if counter2 reaches a score of 10 or more turn 129 on first light 130 131 digitalWrite(lvl1led2, HIGH); 132 133 } 134//_______________________________________ 135 136 137 if(counter1 >= 40) { //if counter1 reaches a score of 40 or more turn on 138 second light 139 140 digitalWrite(lvl2led1, HIGH); 141 } 142 143 144 if(counter2 >= 40) { //if counter2 reaches a score of 40 or more turn on second 145 light 146 147 digitalWrite(lvl2led2, HIGH); 148 } 149//________________________________________ 150 151 152 153 if (counter1 >= goal) { 154// if counter1 reaches 60 or more 155 reset the opponent's lights and counter to indicate the win 156 157 digitalWrite(lvl3led1, 158 HIGH); 159 160 counter2 = 0; 161 digitalWrite(lvl1led2,LOW); 162 digitalWrite(lvl2led2,LOW); 163 164 digitalWrite(lvl3led2,LOW); 165 } 166 167 168 169 170 171if (counter2 172 >= goal) { 173// if counter1 reaches 60 or more reset the opponent's lights and 174 counter to indicate the win 175 176 digitalWrite(lvl3led2, HIGH); 177 counter1 178 = 0; 179 digitalWrite(lvl1led1,LOW); 180 digitalWrite(lvl2led1,LOW); 181 182 digitalWrite(lvl3led1,LOW); 183 } 184 185 if (stateReset == HIGH) { 186//if 187 stateReset is equal to high reset all counters and turn off all lights 188 counter1 189 = 0; 190 counter2 = 0; 191 192 digitalWrite(lvl1led1,LOW); 193 digitalWrite(lvl2led1,LOW); 194 195 digitalWrite(lvl3led1,LOW); 196 digitalWrite(lvl1led2,LOW); 197 digitalWrite(lvl2led2,LOW); 198 199 digitalWrite(lvl3led2,LOW); 200 } // made own reset button so void setup 201 wouldn’t have to run again. 202// resets the game instantly 203 204 } 205
Downloadable files
arduino_game_NV0Sb7OfEa.png
arduino_game_NV0Sb7OfEa.png

Comments
Only logged in users can leave comments