Components and supplies
Arduino UNO
Breadboard (generic)
Resistor 1k ohm
Jumper wires (generic)
Pushbutton switch 12mm
LED (generic)
RobotGeek Buzzer
Single Turn Potentiometer- 10k ohms
Standard LCD - 16x2 White on Blue
Tools and machines
Soldering iron (generic)
Project description
Code
dollar_warrior
arduino
1#include <LiquidCrystal.h> 2 3 4const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; 5const int buttonPin1=1; 6const int buttonPin2=6; 7const int buzzer=7; 8unsigned long pts=0; 9//set buttonstates 10bool buttonState1=0; 11bool buttonState2=0; 12//random number for position of obstacles 13int randomNums[6]; 14//random number for number of obstacles 15int randomNum=0; 16//random number for position of pts 17int randomNums1[3]; 18//random number for number of pts 19int randomNum1=0; 20//start delay time, which decreases gradually 21unsigned int myDelay=500; 22//made this boolean to check if button2 is pressed because if it's pressed once in the first for loop i want obstacles not to be written until the end of it 23bool temp=0; 24//this variable stores the positions of the warrior while he shoots. there can be 16 positions because the warrior has 16 positions. 25int tempI[16]; 26//i use this boolean to check if the point is catched 27bool temp1=0; 28 29//use this variable to store the position of the catched point. it must be and array to store the position of all the points in one iteration of the first for loop. if it stored just one position, then the "old" point would come back in the new iteration 30int tempI1[3]; 31//use this variable to have a number of shots of the warrior, which is also the length of an array tempI 32int button2IsPressed=0; 33LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 34 35void setup() { 36 // set up the LCD's number of columns and rows: 37 lcd.begin(16, 2); 38 // set buttonpin mode 39 pinMode(buttonPin1,INPUT); 40 pinMode(buttonPin2,INPUT); 41 pinMode(buzzer,OUTPUT); 42 lcd.setCursor(4,0); 43 lcd.print("THE GAME"); 44 lcd.setCursor(2,1); 45 lcd.print("STARTS IN "); 46 lcd.print("5"); 47 delay(1000); 48 lcd.setCursor(12,1); 49 lcd.print("4"); 50 delay(1000); 51 lcd.setCursor(12,1); 52 lcd.print("3"); 53 delay(1000); 54 lcd.setCursor(12,1); 55 lcd.print("2"); 56 delay(1000); 57 lcd.setCursor(12,1); 58 lcd.print("1"); 59 delay(1000); 60 lcd.clear(); 61 62} 63 64void loop() { 65 here: 66 67 68 randomNum=random(5); 69 for(int i=0; i<randomNum; ++i){ 70 randomNums[i]=random(16);} 71 randomNum1=random(3); 72 for(int i=0; i<randomNum1; ++i){ 73 randomNums1[i]=random(16); 74 } 75 //i reset temp variable before every new for loop because the cycle of the moving warrior starts again 76 temp=0; 77 for(int j=0; j<3; ++j){ 78 tempI1[j]=0; 79 } 80 button2IsPressed=0; 81//loop that writes dollar(warrior) on the lcd which jumps everytime button is pressed 82 for(int i=0; i<16; ++i){ 83 84 85 //pts must be written here because of the lcd.clear() below (pts wouldn't be written down the whole time otherwise) 86 //i must check if pts is greater than 9 or 99 etc to know how many gaps i should leave for the number. the more points are collected, the smaller delay time is and the faster is dollar 87 if(pts>9 && pts<20) 88 {lcd.setCursor(14,0); 89 myDelay=400; 90 } 91 else if(pts>19 && pts<30) { 92 lcd.setCursor(14,0); 93 myDelay=300; 94 } 95 else if(pts>29 && pts<50) 96 { 97 myDelay=200; 98 lcd.setCursor(14,0); 99 } 100 101 else if(pts>=50){ 102 pts=0; 103 myDelay=500; 104 lcd.clear(); 105 lcd.setCursor(5,0); 106 lcd.print("VICTORY"); 107 tone(buzzer,262); 108 delay(200); 109 tone(buzzer,330); 110 delay(200); 111 tone(buzzer,392); 112 delay(100); 113 tone(buzzer,330); 114 delay(100); 115 tone(buzzer,392); 116 delay(100); 117 tone(buzzer,523); 118 delay(200); 119 noTone(buzzer); 120 delay(3000); 121 lcd.clear(); 122 goto here; 123 } 124 else lcd.setCursor(15,0); 125 lcd.print(pts); 126 127 buttonState1=digitalRead(buttonPin1); 128 buttonState2=digitalRead(buttonPin2); 129 //setting obstacles if the warrior didn't shoot or he shot but he also jumped 130 if(!temp){ 131 for(int j=0; j<randomNum; ++j){ 132 lcd.setCursor(randomNums[j],1); 133 lcd.print("#"); 134 } 135 } 136 //checking if the warrior had shot but there were obstacles before him, we want those obstacles to stay 137 else{ 138 for(int j=0; j<randomNum; ++j){ 139 //i check only the tempI[0] position because that's the when the warrior shot for the first time and he removed all the remaining obstacles 140 if(tempI[0]>randomNums[j]){ 141 lcd.setCursor(randomNums[j],1); 142 lcd.print("#"); 143 144 } 145 146 } 147 } 148 149 150//setting pts 151if(!temp1){ 152 for(int j=0; j<randomNum1; ++j){ 153 lcd.setCursor(randomNums1[j],0); 154 155 lcd.print("*"); 156 157 } 158} 159else{ 160 for(int j=0; j<randomNum1; ++j){ 161 162 if(randomNums1[j]!=tempI1[j]){ 163 lcd.setCursor(randomNums1[j],0); 164 lcd.print("*"); 165 } 166 } 167 } 168 169 170 171 //if the button is pressed we set the cursor up (so that our warrior jumps) 172 if(buttonState1==HIGH) 173 {lcd.setCursor(i,0); 174 tone(buzzer,131); 175 delay(200); 176 noTone(buzzer); 177 } 178 else lcd.setCursor(i,1); 179 180 lcd.print("$"); 181 182 //if button2 is pressed our warrior shoots. i had to put this loop here so that warrior shoots from his current spot and then continues to move. that's why delay time is 5 so that this loop finishes as soon as possible 183 if (buttonState2==HIGH) 184 { 185 tone(buzzer,175); 186 delay(100); 187 noTone(buzzer); 188 189 temp=1; 190 //if the button1 is low then we remove all the obstacles. that's why the state if temp variable is changed 191 if(buttonState1==LOW) 192 { 193 194 tempI[button2IsPressed]=i; 195 } 196 ++button2IsPressed; 197 for(int k=i+1; k<16; ++k){ 198 //if the buton1 is low then warrior shoots in the second row. otherwise it shoots in the first row 199 if(buttonState1==LOW) 200 lcd.setCursor(k,1); 201 else lcd.setCursor(k,0); 202 lcd.print("~"); 203 delay(5); 204 if(buttonState1==LOW) 205 lcd.setCursor(k-1,1); 206 else lcd.setCursor(k-1,0); 207 lcd.print(" "); 208 delay(5); 209 210 211 } 212 } 213 214 delay(myDelay); 215 lcd.clear(); 216 //checking if the positions of the pts and the warrior are the same and if the button is pressed because then they collide and we gain 5 extra points 217 for(int j=0; j<randomNum1; ++j){ 218 if(i==randomNums1[j] && buttonState1==HIGH){ 219 temp1=1; 220 tempI1[j]=i; 221 pts+=5; 222 } 223 } 224 225 226 227 228 //checking if the positions of the obstacle and the warrior are the same and if the button1 is not pressed, then they collide and it is the end of the game 229 for(int j=0; j<randomNum; ++j){ 230 if(i==randomNums[j] && buttonState1==LOW && temp==0) { 231 232 pts=0; 233 myDelay=500; 234 lcd.clear(); 235 lcd.setCursor(6,0); 236 lcd.print("GAME"); 237 lcd.setCursor(6,1); 238 lcd.print("OVER"); 239 tone(buzzer,349); 240 delay(200); 241 tone(buzzer, 277); 242 delay(200); 243 tone(buzzer,220); 244 delay(100); 245 tone(buzzer,277); 246 delay(100); 247 tone(buzzer,220); 248 delay(200); 249 noTone(buzzer); 250 delay(3000); 251 lcd.clear(); 252 //i must skip the for loop because otherwise the game would continue where it ended 253 goto here; 254 255 } 256 //counting number of skipped obstacles and that would be our points 257 else if(i==randomNums[j] && buttonState1==HIGH) ++pts; 258 } 259 260 261 262 } 263 264 265 266}
dollar_warrior
arduino
1#include <LiquidCrystal.h> 2 3 4const int rs = 12, en = 11, d4 = 5 5, d5 = 4, d6 = 3, d7 = 2; 6const int buttonPin1=1; 7const int buttonPin2=6; 8const 9 int buzzer=7; 10unsigned long pts=0; 11//set buttonstates 12bool buttonState1=0; 13bool 14 buttonState2=0; 15//random number for position of obstacles 16int randomNums[6]; 17//random 18 number for number of obstacles 19int randomNum=0; 20//random number for position 21 of pts 22int randomNums1[3]; 23//random number for number of pts 24int randomNum1=0; 25//start 26 delay time, which decreases gradually 27unsigned int myDelay=500; 28//made this 29 boolean to check if button2 is pressed because if it's pressed once in the first 30 for loop i want obstacles not to be written until the end of it 31bool temp=0; 32//this 33 variable stores the positions of the warrior while he shoots. there can be 16 positions 34 because the warrior has 16 positions. 35int tempI[16]; 36//i use this boolean 37 to check if the point is catched 38bool temp1=0; 39 40//use this variable to 41 store the position of the catched point. it must be and array to store the position 42 of all the points in one iteration of the first for loop. if it stored just one 43 position, then the "old" point would come back in the new iteration 44int tempI1[3]; 45 46//use this variable to have a number of shots of the warrior, which is also 47 the length of an array tempI 48int button2IsPressed=0; 49LiquidCrystal lcd(rs, 50 en, d4, d5, d6, d7); 51 52void setup() { 53 // set up the LCD's number of columns 54 and rows: 55 lcd.begin(16, 2); 56 // set buttonpin mode 57 pinMode(buttonPin1,INPUT); 58 59 pinMode(buttonPin2,INPUT); 60 pinMode(buzzer,OUTPUT); 61 lcd.setCursor(4,0); 62 63 lcd.print("THE GAME"); 64 lcd.setCursor(2,1); 65 lcd.print("STARTS IN "); 66 67 lcd.print("5"); 68 delay(1000); 69 lcd.setCursor(12,1); 70 lcd.print("4"); 71 72 delay(1000); 73 lcd.setCursor(12,1); 74 lcd.print("3"); 75 delay(1000); 76 77 lcd.setCursor(12,1); 78 lcd.print("2"); 79 delay(1000); 80 lcd.setCursor(12,1); 81 82 lcd.print("1"); 83 delay(1000); 84 lcd.clear(); 85 86} 87 88void loop() 89 { 90 here: 91 92 93 randomNum=random(5); 94 for(int i=0; i<randomNum; ++i){ 95 96 randomNums[i]=random(16);} 97 randomNum1=random(3); 98 for(int i=0; i<randomNum1; 99 ++i){ 100 randomNums1[i]=random(16); 101 } 102 //i reset temp variable before 103 every new for loop because the cycle of the moving warrior starts again 104 temp=0; 105 106 for(int j=0; j<3; ++j){ 107 tempI1[j]=0; 108 } 109 button2IsPressed=0; 110//loop 111 that writes dollar(warrior) on the lcd which jumps everytime button is pressed 112 113 for(int i=0; i<16; ++i){ 114 115 116 //pts must 117 be written here because of the lcd.clear() below (pts wouldn't be written down the 118 whole time otherwise) 119 //i must check if pts is greater than 9 or 99 120 etc to know how many gaps i should leave for the number. the more points are collected, 121 the smaller delay time is and the faster is dollar 122 if(pts>9 && pts<20) 123 124 {lcd.setCursor(14,0); 125 myDelay=400; 126 } 127 128 else if(pts>19 && pts<30) { 129 lcd.setCursor(14,0); 130 myDelay=300; 131 132 } 133 else if(pts>29 && pts<50) 134 { 135 myDelay=200; 136 137 lcd.setCursor(14,0); 138 } 139 140 else 141 if(pts>=50){ 142 pts=0; 143 myDelay=500; 144 lcd.clear(); 145 146 lcd.setCursor(5,0); 147 lcd.print("VICTORY"); 148 tone(buzzer,262); 149 150 delay(200); 151 tone(buzzer,330); 152 delay(200); 153 154 tone(buzzer,392); 155 delay(100); 156 tone(buzzer,330); 157 158 delay(100); 159 tone(buzzer,392); 160 delay(100); 161 162 tone(buzzer,523); 163 delay(200); 164 noTone(buzzer); 165 166 delay(3000); 167 lcd.clear(); 168 goto here; 169 170 } 171 else lcd.setCursor(15,0); 172 lcd.print(pts); 173 174 175 buttonState1=digitalRead(buttonPin1); 176 buttonState2=digitalRead(buttonPin2); 177 178 //setting obstacles if the warrior didn't shoot or he shot but he also jumped 179 180 if(!temp){ 181 for(int j=0; j<randomNum; ++j){ 182 lcd.setCursor(randomNums[j],1); 183 184 lcd.print("#"); 185 } 186 } 187 //checking if the warrior had 188 shot but there were obstacles before him, we want those obstacles to stay 189 else{ 190 191 for(int j=0; j<randomNum; ++j){ 192 //i check only the tempI[0] position because 193 that's the when the warrior shot for the first time and he removed all the remaining 194 obstacles 195 if(tempI[0]>randomNums[j]){ 196 lcd.setCursor(randomNums[j],1); 197 198 lcd.print("#"); 199 200 } 201 202 } 203 } 204 205 206//setting 207 pts 208if(!temp1){ 209 for(int j=0; j<randomNum1; ++j){ 210 lcd.setCursor(randomNums1[j],0); 211 212 213 lcd.print("*"); 214 215 } 216} 217else{ 218 219 for(int j=0; j<randomNum1; ++j){ 220 221 if(randomNums1[j]!=tempI1[j]){ 222 223 lcd.setCursor(randomNums1[j],0); 224 lcd.print("*"); 225 } 226 } 227 228 } 229 230 231 232 //if the button is pressed 233 we set the cursor up (so that our warrior jumps) 234 if(buttonState1==HIGH) 235 236 {lcd.setCursor(i,0); 237 tone(buzzer,131); 238 delay(200); 239 240 noTone(buzzer); 241 } 242 else lcd.setCursor(i,1); 243 244 245 lcd.print("$"); 246 247 //if button2 248 is pressed our warrior shoots. i had to put this loop here so that warrior shoots 249 from his current spot and then continues to move. that's why delay time is 5 so 250 that this loop finishes as soon as possible 251 if (buttonState2==HIGH) 252 253 { 254 tone(buzzer,175); 255 delay(100); 256 noTone(buzzer); 257 258 259 temp=1; 260 //if the button1 is low then we remove 261 all the obstacles. that's why the state if temp variable is changed 262 if(buttonState1==LOW) 263 264 { 265 266 tempI[button2IsPressed]=i; 267 } 268 269 ++button2IsPressed; 270 for(int k=i+1; k<16; ++k){ 271 //if 272 the buton1 is low then warrior shoots in the second row. otherwise it shoots in 273 the first row 274 if(buttonState1==LOW) 275 lcd.setCursor(k,1); 276 277 else lcd.setCursor(k,0); 278 lcd.print("~"); 279 delay(5); 280 281 if(buttonState1==LOW) 282 lcd.setCursor(k-1,1); 283 else 284 lcd.setCursor(k-1,0); 285 lcd.print(" "); 286 delay(5); 287 288 289 290 } 291 } 292 293 delay(myDelay); 294 295 lcd.clear(); 296 //checking if the positions of the pts and 297 the warrior are the same and if the button is pressed because then they collide 298 and we gain 5 extra points 299 for(int j=0; j<randomNum1; ++j){ 300 if(i==randomNums1[j] 301 && buttonState1==HIGH){ 302 temp1=1; 303 tempI1[j]=i; 304 pts+=5; 305 306 } 307 } 308 309 310 311 312 313 //checking if the positions of the obstacle and the warrior are the same 314 and if the button1 is not pressed, then they collide and it is the end of the game 315 316 for(int j=0; j<randomNum; ++j){ 317 if(i==randomNums[j] && buttonState1==LOW 318 && temp==0) { 319 320 pts=0; 321 myDelay=500; 322 lcd.clear(); 323 324 lcd.setCursor(6,0); 325 lcd.print("GAME"); 326 lcd.setCursor(6,1); 327 328 lcd.print("OVER"); 329 tone(buzzer,349); 330 delay(200); 331 332 tone(buzzer, 277); 333 delay(200); 334 tone(buzzer,220); 335 336 delay(100); 337 tone(buzzer,277); 338 delay(100); 339 tone(buzzer,220); 340 341 delay(200); 342 noTone(buzzer); 343 delay(3000); 344 lcd.clear(); 345 346 //i must skip the for loop because otherwise the game would continue where 347 it ended 348 goto here; 349 350 } 351 //counting 352 number of skipped obstacles and that would be our points 353 else if(i==randomNums[j] 354 && buttonState1==HIGH) ++pts; 355 } 356 357 358 359 360 } 361 362 363 364}
Downloadable files
dollar_warrior
dollar_warrior
dollar_warrior
dollar_warrior
dollar_warrior
dollar_warrior
Comments
Only logged in users can leave comments
anime
0 Followers
•0 Projects
Table of contents
Intro
11
0