Fun with Arduino
The Outcome
This simple eye-hand coordination game challenges you to press a button in concert with a flashing LED – any delay makes it more difficult for you to beat your opponent.
For a better understanding of how the game is played, a video of the final setup can be viewed on the Arduteka YouTube channel [4].
Listing 1
Code for Reflex Game
001 #include <Servo.h> 002 003 Servo servo1; 004 005 int degrees = 90; // Servo degrees 006 int servoPin = 9; // Pin for the Servo 007 008 boolean stateP1; // State of push button Player 1 009 boolean stateP2; // State of push button Player 1 010 011 int rep1 = 0; // Variable for first For loop 012 int rep2 = 0; // Variable for second For loop 013 014 int randNum = 0; // Random Number 015 int time = 0; // Running time 1 016 int time2 = 0; // Running time 2 017 int countP1; // Pulse counter P1 018 int countP2; // Pulse counter P2 019 int freqGame = 1000; // Time LEDs are off 020 int freqLed = 500; // Time LED are on 021 022 //Pins for the LEDs in the game 023 int pinLedGeneral = 3; 024 int pinLedErrorP1 = 4; 025 int pinLedErrorP2 = 5; 026 int pinLedCorrectP1 = 6; 027 int pinLedCorrectP2 = 7; 028 int pinLedP1 = 8; 029 int pinLedP2 = 10; 030 int buttonP1 = 11; 031 int buttonP2 = 12; 032 033 void setup() 034 { 035 servo1.attach(servoPin); // Make servoPin Servo 036 servo1.write(0); // Send it to 0 degrees 037 delay(800); // Give it time to get there 038 servo1.write(179); // Send it to degree 179 039 delay(800); 040 servo1.write(degrees); // Take it to original position 041 042 //Set Pin Configuration 043 pinMode(pinLedGeneral, OUTPUT); 044 pinMode(pinLedErrorP1, OUTPUT); 045 pinMode(pinLedErrorP2, OUTPUT); 046 pinMode(pinLedCorrectP1, OUTPUT); 047 pinMode(pinLedCorrectP2, OUTPUT); 048 pinMode(pinLedP1, OUTPUT); 049 pinMode(pinLedP2, OUTPUT); 050 pinMode(buttonP1, INPUT); 051 pinMode(buttonP2, INPUT); 052 } 053 054 void loop() 055 { 056 randNum = random(0, 99); // Generate random number 057 time = millis(); // Store time 058 countP1 = 0; 059 countP2 = 0; 060 061 // Make sure the speed of the game is not too high 062 if (freqGame < 50) 063 { 064 Winner(); 065 } 066 067 // Make times shorter 068 freqGame = (freqGame - 10); 069 freqLed = (freqLed - 4); 070 071 // Switch on LED and check to see if the 072 // correct player pushes his button while it's on 073 while ((time2 - time) <= freqLed) 074 { 075 time2 = millis(); 076 if ((randNum % 2) == 0) // Led Player 1 077 { 078 digitalWrite(pinLedP1, HIGH); 079 digitalWrite(pinLedP2, LOW); 080 081 stateP1 = digitalRead(buttonP1); 082 stateP2 = digitalRead(buttonP2); 083 084 if ((stateP1 == true) & (countP1 == 0)) 085 { 086 RightP1(); 087 } 088 089 if ((stateP2 == true) & (countP2 == 0)) 090 { 091 P2Error(); 092 } 093 } 094 095 else // Led Player 2 096 { 097 digitalWrite(pinLedP1, LOW); 098 digitalWrite(pinLedP2, HIGH); 099 100 stateP1 = digitalRead(buttonP1); 101 stateP2 = digitalRead(buttonP2); 102 103 if ((stateP2 == true) & (countP2 == 0)) 104 { 105 RightP2(); 106 } 107 108 if ((stateP1 == true) & (countP1 == 0)) 109 { 110 P1Error(); 111 } 112 } 113 } 114 115 // Switch LEDs off 116 digitalWrite(pinLedP1, LOW); 117 digitalWrite(pinLedP2, LOW); 118 119 //Check nobody presses while they're off 120 time = millis(); 121 while ((time2 - time) <= freqGame) 122 { 123 time2 = millis(); 124 stateP1 = digitalRead(buttonP1); 125 stateP2 = digitalRead(buttonP2); 126 if ((stateP1 == true) & (countP1 == 0)) 127 { 128 P1Error(); 129 } 130 131 if ((stateP2 == true) & (countP2 == 0)) 132 { 133 P2Error(); 134 } 135 } 136 } 137 138 // Check if P1 has one 139 void Player1Wins() 140 { 141 if (degrees == 170) 142 { 143 digitalWrite(pinLedCorrectP1, HIGH); 144 digitalWrite(pinLedCorrectP2, LOW); 145 digitalWrite(pinLedErrorP1, LOW); 146 digitalWrite(pinLedErrorP2, LOW); 147 Winner(); 148 } 149 } 150 151 void Player2Wins() 152 { 153 if (degrees == 10) 154 { 155 digitalWrite(pinLedCorrectP1, LOW); 156 digitalWrite(pinLedCorrectP2, HIGH); 157 digitalWrite(pinLedErrorP1, LOW); 158 digitalWrite(pinLedErrorP2, LOW); 159 Winner(); 160 } 161 } 162 163 // P1 presses correctly 164 void RightP1() 165 { 166 countP1++; 167 degrees = (degrees + 10); 168 servo1.write(degrees); 169 digitalWrite(pinLedErrorP1, LOW); 170 digitalWrite(pinLedCorrectP1, HIGH); 171 Player1Wins(); 172 } 173 174 void RightP2() 175 { 176 countP2++; 177 degrees = (degrees - 10); 178 servo1.write(degrees); 179 digitalWrite(pinLedErrorP2, LOW); 180 digitalWrite(pinLedCorrectP2, HIGH); 181 Player2Wins(); 182 } 183 184 // P1 presses incorrectly 185 void P1Error() 186 { 187 countP1++; 188 degrees = (degrees - 10); 189 servo1.write(degrees); 190 digitalWrite(pinLedCorrectP1, LOW); 191 digitalWrite(pinLedErrorP1, HIGH); 192 Player2Wins(); 193 } 194 195 void P2Error() 196 { 197 countP2++; 198 degrees = (degrees + 10); 199 servo1.write(degrees); 200 digitalWrite(pinLedCorrectP2, LOW); 201 digitalWrite(pinLedErrorP2, HIGH); 202 Player1Wins(); 203 } 204 205 // There's a winner! 206 void Winner() 207 { 208 209 for (rep1=0; rep1<3; rep1++) 210 { 211 servo1.write(0); 212 for (rep2=10; rep2<250; rep2++) 213 { 214 analogWrite(pinLedGeneral, rep2); 215 delay(3); 216 } 217 servo1.write(179); 218 for (rep2=250; rep2>10; rep2--) 219 { 220 analogWrite(pinLedGeneral, rep2); 221 delay(3); 222 } 223 } 224 digitalWrite(pinLedCorrectP1, LOW); 225 digitalWrite(pinLedCorrectP2, LOW); 226 digitalWrite(pinLedErrorP1, LOW); 227 digitalWrite(pinLedErrorP2, LOW); 228 digitalWrite(pinLedGeneral, LOW); 229 degrees = (90); 230 servo1.write(degrees); 231 delay(5000); 232 freqGame = 100; 233 freqLed = 500; 234 }
Infos
- Arduino: http://arduino.cc/
- PWM: http://en.wikipedia.org/wiki/Pulse-width_modulation
- Fritzing: http://fritzing.org/home/
- Video: http://www.youtube.com/watch?v=TEzYulNEzRw (Spanish notations)
« Previous 1 2 Next »
Buy this article as PDF
Express-Checkout as PDF
Pages: 4
Price $2.95
(incl. VAT)
(incl. VAT)