Buat Game LCD Ninja Dollar dengan Arduino UNO
Komponen dan Perlengkapan
 |
|
× |
1 |
|
 |
| LCD Standar Adafruit - 16x2 Putih pada Biru |
|
× |
1 |
|
 |
|
× |
2 |
|
 |
| Potensiometer Putaran Tunggal - 10k ohm |
|
× |
1 |
|
 |
|
× |
2 |
|
 |
|
× |
4 |
|
 |
| Switch Pushbutton SparkFun 12mm |
|
× |
2 |
|
 |
|
× |
1 |
|
 |
|
× |
1 |
|
Alat dan Mesin yang Diperlukan
Tentang Proyek Ini
Ini adalah game LCD sederhana. Inspirasi saya adalah game Dinosaur Google Chrome, tetapi saya menambahkan beberapa perubahan. Prajurit dolar bergerak terus-menerus dan ia dapat melompat untuk menghindari rintangan serta mengumpulkan poin "bintang". Jika ia melompat melewati rintangan, ia mengumpulkan satu poin dan jika menangkap bintang, ia mengumpulkan lima poin. Ia juga dapat menembak dan menghapus rintangan di depannya. Ada juga dioda LED yang menunjukkan apakah ia melompat atau menembak. Ketika ia mengumpulkan 10, 20, atau 30 poin, ia mulai bergerak lebih cepat dari sebelumnya. Ketika mengumpulkan 50 poin, Anda menang. Jika Anda mengenai rintangan, Anda kalah. Tentu saja, Anda dapat mengubah batasan-batasan ini dengan mudah dalam kode. Saya juga menambahkan buzzer pasif yang mengeluarkan suara ketika prajurit melompat atau menembak dan melodi khusus ketika Anda kalah atau menang. Ini adalah proyek pertama saya di sini dan saya berharap Anda menyukainya. Berikan komentar dan ulasan Anda.
Video
Koneksi
Modul LCD (Liquid Crystal Display) memiliki header 16 pin pria di bagian bawah. Colokkan ini ke breadboard seperti yang ditunjukkan dalam gambar. Semua sinyal elektronik yang menyediakan daya dan mengontrol LCD melalui header ini.
Pin-pin ini adalah (dari kiri ke kanan):
- VCC - sinyal daya positif
- V0 - penyesuaikan kontras
- R/W - pemilihan baca/tulis
- D0 - bit data 0 (tidak digunakan di sini)
- D1 - bit data 1 (tidak digunakan di sini)
- D2 - bit data 2 (tidak digunakan di sini)
- D3 - bit data 3 (tidak digunakan di sini)
- A - positif LED backlight
- K - negatif LED backlight
Gunakan kabel jumper untuk menghubungkan sinyal 5V pada Arduino dengan baris merah di bagian atas breadboard.
Gunakan kabel jumper untuk menghubungkan sinyal GND pada Arduino dengan baris biru di bagian atas breadboard.
- Hubungkan pin VSS ke ground
- Hubungkan pin VDD ke sinyal 5V
- Pin V0 ke pin output potensiometer
Hubungkan pin potensiometer yang tersisa ke sinyal 5V dan sinyal GND.
Jika Anda memiliki piezo buzzer 3 pin, hubungkan pin S ke pin Arduino 7, pin '-' ke GND, dan pin yang tersisa ke sinyal 5V. Jika Anda memiliki piezo buzzer 2 pin, hubungkan satu pin ke sinyal GND dan yang lain ke pin Arduino 7.
Hubungkan tombol switch sesuai skematik. Tombol pertama (tombol lompat) terhubung ke pin Arduino 1 dan tombol kedua (tembakan) terhubung ke pin Arduino 6. LED hanya dikontrol oleh tombol dan tidak oleh Arduino. Anoda terhubung ke pin tombol yang terhubung ke Arduino dan katoda terhubung ke ground.
Kode
dollar_warriorArduino
#include <LiquidCrystal.h> const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; const int buttonPin1=1; const int buttonPin2=6; const int buzzer=7; unsigned long pts=0; //set buttonstates bool buttonState1=0; bool buttonState2=0; //random number for position of obstacles int randomNums[6]; //random number for number of obstacles int randomNum=0; //random number for position of pts int randomNums1[3]; //random number for number of pts int randomNum1=0; //start delay time, which decreases gradually unsigned int myDelay=500; //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 bool temp=0; //this variable stores the positions of the warrior while he shoots. there can be 16 positions because the warrior has 16 positions. int tempI[16]; //i use this boolean to check if the point is catched bool temp1=0; //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 int tempI1[3]; //use this variable to have a number of shots of the warrior, which is also the length of an array tempI int button2IsPressed=0; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // set buttonpin mode pinMode(buttonPin1,INPUT); pinMode(buttonPin2,INPUT); pinMode(buzzer,OUTPUT); lcd.setCursor(4,0); lcd.print("THE GAME"); lcd.setCursor(2,1); lcd.print("STARTS IN "); lcd.print("5"); delay(1000); lcd.setCursor(12,1); lcd.print("4"); delay(1000); lcd.setCursor(12,1); lcd.print("3"); delay(1000); lcd.setCursor(12,1); lcd.print("2"); delay(1000); lcd.setCursor(12,1); lcd.print("1"); delay(1000); lcd.clear(); } void loop() { here: randomNum=random(5); for(int i=0; i<randomNum; ++i){ randomNums[i]=random(16);} randomNum1=random(3); for(int i=0; i<randomNum1; ++i){ randomNums1[i]=random(16); } //i reset temp variable before every new for loop because the cycle of the moving warrior starts again temp=0; for(int j=0; j<3; ++j){ tempI1[j]=0; } button2IsPressed=0; //loop that writes dollar(warrior) on the lcd which jumps everytime button is pressed for(int i=0; i<16; ++i){ //pts must be written here because of the lcd.clear() below (pts wouldn't be written down the whole time otherwise) //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 if(pts>9 && pts<20) {lcd.setCursor(14,0); myDelay=400; } else if(pts>19 && pts<30) { lcd.setCursor(14,0); myDelay=300; } else if(pts>29 && pts<50) { myDelay=200; lcd.setCursor(14,0); } else if(pts>=50){ pts=0; myDelay=500; lcd.clear(); lcd.setCursor(5,0); lcd.print("VICTORY"); tone(buzzer,262); delay(200); tone(buzzer,330); delay(200); tone(buzzer,392); delay(100); tone(buzzer,330); delay(100); tone(buzzer,392); delay(100); tone(buzzer,523); delay(200); noTone(buzzer); delay(3000); lcd.clear(); goto here; } else lcd.setCursor(15,0); lcd.print(pts); buttonState1=digitalRead(buttonPin1); buttonState2=digitalRead(buttonPin2); //setting obstacles if the warrior didn't shoot or he shot but he also jumped if(!temp){ for(int j=0; j<randomNum; ++j){ lcd.setCursor(randomNums[j],1); lcd.print("#"); } } //checking if the warrior had shot but there were obstacles before him, we want those obstacles to stay else{ for(int j=0; j<randomNum; ++j){ //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 if(tempI[0]>randomNums[j]){ lcd.setCursor(randomNums[j],1); lcd.print("#"); } } } //setting pts if(!temp1){ for(int j=0; j<randomNum1; ++j){ lcd.setCursor(randomNums1[j],0); lcd.print("*"); } } else{ for(int j=0; j<randomNum1; ++j){ if(randomNums1[j]!=tempI1[j]){ lcd.setCursor(randomNums1[j],0); lcd.print("*"); } } } //if the button is pressed we set the cursor up (so that our warrior jumps) if(buttonState1==HIGH) {lcd.setCursor(i,0); tone(buzzer,131); delay(200); noTone(buzzer); } else lcd.setCursor(i,1); lcd.print("$"); //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 if (buttonState2==HIGH) { tone(buzzer,175); delay(100); noTone(buzzer); temp=1; //if the button1 is low then we remove all the obstacles. that's why the state if temp variable is changed if(buttonState1==LOW) { tempI[button2IsPressed]=i; } ++button2IsPressed; for(int k=i+1; k<16; ++k){ //if the buton1 is low then warrior shoots in the second row. otherwise it shoots in the first row if(buttonState1==LOW) lcd.setCursor(k,1); else lcd.setCursor(k,0); lcd.print("~"); delay(5); if(buttonState1==LOW) lcd.setCursor(k-1,1); else lcd.setCursor(k-1,0); lcd.print(" "); delay(5); } } delay(myDelay); lcd.clear(); //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 for(int j=0; j<randomNum1; ++j){ if(i==randomNums1[j] && buttonState1==HIGH){ temp1=1; tempI1[j]=i; pts+=5; } } //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 for(int j=0; j<randomNum; ++j){ if(i==randomNums[j] && buttonState1==LOW && temp==0) { pts=0; myDelay=500; lcd.clear(); lcd.setCursor(6,0); lcd.print("GAME"); lcd.setCursor(6,1); lcd.print("OVER"); tone(buzzer,349); delay(200); tone(buzzer, 277); delay(200); tone(buzzer,220); delay(100); tone(buzzer,277); delay(100); tone(buzzer,220); delay(200); noTone(buzzer); delay(3000); lcd.clear(); //i must skip the for loop because otherwise the game would continue where it ended goto here; } //counting number of skipped obstacles and that would be our points else if(i==randomNums[j] && buttonState1==HIGH) ++pts; } } }
Skematik
game_aUIjA6VZxX.fzz