Bab 3
IMPLEMTASI
3.1 Pendahuluan
Dalam
pembuatan Retro Snake NokianGame wi-th Arduino, komponen yang paling dibutuhkan
sudah tentu Arduino itu sendiri. Selanjutnya ada beberapa komponen yang
nantinya akan dijelaskan sebagai beri-kut:
3.1.1 Komponen
dan Supplies
Arduino uno genuino uno
Mini Speaker - PC Mount 12mm 2.048kHz
Graphic LCD 84x48 - Nokia 5110
Lithium Ion Battery - 2200mAh 7.4v
PSP 2-Axis Analog Thumb Joystick
Momentary Pushbutton Switch - 12mm Square
3.1.2 Barang
Elektronik
Layar Nokia 5110
Arduino Uno
Piezo Speaker
Joystick
Push Button
Switch
Battre Lithium
Untuk perakitan listriknya cukup sederhana.
Dalam link circuito.io, kita sudah memilih semua bagian di-butuhkan untuk
proyek tersebut. anda tentu saja me-nambahkan komponen tambahan jika anda mau.
Se-telah anda memilih semua yang anda butuhkan, hanya tinggal klik on generate.
3.1.3 Aplikasi
dan Online Service
Circuit.io
Arduino
3.2 Tentang
Proyek
Super Mario, Tetris,
Pac-Man. Tidakkah kamu ber-harap bisa memainkan kembali game ini untuk
meng-habiskan waktu luang anda didepan layar hitam de-ngan resolusi minimal dan
memaikan game yang sa-ngat adiktif? Nah, setelah kami menambahkan layar Nokia
ke circuito.io, proyek ini takkan terelakkan. Ka-mi bangga mempersembahkan
kreasi kami yaitu game Nokia Snake yang dibungkus dalam kotak Arduino!
3.3 Circuito.IO
Reply
Circuito reply mempunyai 4 tahapan:
3.3.1 BoM
Daftar semua komponen
yang Anda perlukan un-tuk proyek ini. anda mungkin bertanya-tanya mengapa
komponen baru "muncul" pada daftar ini, walaupun anda belum
memilihnya. Nah, bagian ini diperlukan untuk proyek anda seperti bagian inti
yang anda pilih, dan otomatis ditambahkan ke daftar suku cadang anda setelah
dihitung oleh smart engine kami.
3.3.2 Step-by-step
wiring guide
Ini adalah bagian yang
menyenangkan. Setelah me-nempatkan semua komponen pada papan, anda bisa mulai
mengaitkannya satu sama lain dan ke Arduino. Ikuti saja wizard, langkah demi
langkah, semudah itu!
Pastikan untuk membaca komentar yang pop-up di
sisi kiri bar, karena mungkin sangat membantu.
3.3.3 Code
Pada bagian ini anda
memiliki kode contoh yang dibuat secara otomatis untuk mengintegrasikan anta-ra
semua komponen yang anda pilih. Kode ini akan membantu anda menguji bahwa anda
memasang sir-kuit dengan benar.
Ikuti langkah dibawah
ini, yang juga muncul pada circuito.io reply:
Download Firmware.zip
Extract semua le ke folder baru dengan nama Firmware
Buka Firmware.ino menggunakan Arduino IDE yang bisa didownload di
web resminya.
Pastikan board terhubung dengan computer anda
via USB.
Di Arduino IDE jangan lupa untuk select correct
Port dan Board di tool menu.
Upload Firmware.ino ke board.
3.3.4 Test
Setelah anda mengupload kode untuk boardnya,
ma-ka anda bisa mengujinya. Semua langkah untuk meng-uji secara spesi k komponen yang anda gunakan, diu-raikan di bagian "test" dari
reply circuito.io. Setelah semuanya diatur dengan benar, Anda bisa melambaik-an
selamat tinggal ke circuito.io reply, sampai ketemu dilain waktu.
3.4 Mengubah
Code
Jadi segala sesuatu di
sirkuit dan kode Anda ter-lihat hebat. Sudah waktunya untuk mengambil kode
kustom yang kami buat khusus untuk proyek Snake.
Buka le rmware.ino
yang mencakup semua li-braries dari circuito.io reply.
Pada tab rmware, simpan // Include Libraries dan // Pin De nitions.
Ganti sisa kode pada tab rmware, dengan kode di bawah ini:
#include <EEPROM.h>
// Global variables and de nes
//de ne Nokia LCD contrast and dimentions(in pi-xels)
#de ne LCD_CONTRAST 70 #de ne LCD_SIZE_COL 84 #de ne LCD_SIZE_ROW 48
bool dl = false, dr = false, du = false, dd = false;
// to check in which direction the snake is
currently moving
uint8_t x[200], y[200], i, slength, tempx = 10,
tem-py = 10, xx, yy;
unsigned int high;
uint8_t bh, bl;
uint8_t xegg, yegg;
int freq, tb;
bool l, r, u, d, p;
unsigned long time = 280, beeptime = 50; int
score = 0, ag = 0 #include <EEPROM.h>
// object initialization
Adafruit_PCD8544 nokiaLcd(NOKIALCD_PIN_DC,
NOKIALCD_PIN_CS, NOKIALCD_PIN_RST);
Button pushButton(PUSHBUTTON_PIN_1);
//
Setup the essentials for your circuit to work.
It runs rst every
time your circuit is powered with ele-ctricity.
void setup()
{
//Serial.begin(9600); pinMode(JOYSTICKPSP_PIN_X,
INPUT); pinMode(JOYSTICKPSP_PIN_Y, INPUT); //Initialize Nokia instance
nokiaLcd.begin(LCD_SIZE_COL, LCD_SIZE_ROW); nokiaLcd.clearDisplay();
nokiaLcd.setContrast(LCD_CONTRAST); //Adjust
display contrast pushButton.init();
slength = 8; //Start with snake length 8 xegg =
(nokiaLcd.width()) / 2;
yegg = (nokiaLcd.height()) / 2
nokiaLcd.setTextSize(2); //Initial nokiaLcd
nokiaLcd.setTextColor(BLACK);
nokiaLcd.setCursor(10, 15);
nokiaLcd.setCursor(10, 40);
nokiaLcd.display();
delay(4000);
nokiaLcd.clearDisplay();
for (i = 0; i <= slength; i++) //Set starting
coo-rdinates of snake
{
x[i] = 25 - 3 * i;
y[i] = 10;
}
for (i = 0; i < slength; i++) //Draw the snake
{
nokiaLcd.drawCircle(x[i], y[i], 1, BLACK);
}
nokiaLcd.display();
dr = true; //Going to move right initially
}
//
Main logic of your circuit. It de nes the inte-raction between the components you
selected. After setup, it runs over and over again, in an eternal loop.
void loop()
{
movesnake(); //This is called endlessly
}
void movesnake()
{
int X_val = analogRead(JOYSTICKPSP_PIN_X); int
Y_val = analogRead(JOYSTICKPSP_PIN_Y);
l = X_val < 400 ? 1 : 0;
d = X_val > 650 ? 1 : 0;
r = Y_val < 400 ? 1 : 0;;
u = Y_val >= 650 ? 1 : 0;
p = pushButton.onPress();
if (p == HIGH) //Pause game for 5 seconds
{
nokiaLcd.clearDisplay(); nokiaLcd.setTextColor(BLACK);
nokiaLcd.setCursor(25, 10); nokiaLcd.setTextSize(1);
nokiaLcd.print("Pause"); nokiaLcd.display(); delay(1000);
while (!pushButton.onPress()); nokiaLcd.clearDisplay();
redraw(); //Redraw the
snake and egg at the same position as it was
}
if (millis() % time ==
0) //this condition becomes true after every 'time' milliseconds...millis()
returns the time since launch of program
{
direct();
if (ag == 0) //ag 0 means no directional key has been pressed
in the last 'time' milliseconds
{
if (dr == true) {
tempx = x[0] + 3; // so
the snake moves one step in the direction it is moving currently
tempy = y[0];
}
if (dl == true) {
tempx = x[0] - 3; //The new coordinates of head
of snake goes in tempx,tempy
tempy = y[0];
}
if (du == true) {
tempy = y[0] - 3;
tempx = x[0];
}
if (dd == true) {
tempy = y[0] + 3;
tempx = x[0];
}
}
ag = 0;
checkgame(); //Check if snake has met egg or
co-incided with itself
checkegg();
if (tempx <= 0) {
tempx = 84 + tempx; //If the new coordinates are
out of screen, set them accordingly
}
else if (tempx >= 84) {
tempx = tempx - 84;
}
if (tempy <= 0) {
tempy = 48 + tempy;
}
else if (tempy >= 48) {
tempy = tempy - 48;
}
for (i = 0; i <= slength; i++) //Change the
coo-rdinates of all points of snake
{
xx
= x[i];
yy = y[i]; x[i] = tempx; y[i] = tempy; tempx = xx;
tempy = yy;
}
drawsnake(); //Draw the snake and egg at the new
coordinates
}
}
void checkgame() //Game over checker
{
for (i = 1; i <
slength; i++) //Checking if the co-ordinates of head have become equal to one
of the non head points of snake
{
if (x[i] == x[0] && y[i] == y[0])
{
bh = EEPROM.read(1);
bl = EEPROM.read(0);
high = (((0x00 + bh)
<< 8) + bl);
if (score > high)
{
high = score;
bh = (high >>
8);
bl = high & 0x;
EEPROM.write(1, bh);
EEPROM.write(0, bl);
}
nokiaLcd.clearDisplay();
nokiaLcd.setTextColor(BLACK);
nokiaLcd.setTextSize(1);
nokiaLcd.setCursor(20, 12);
nokiaLcd.print("Game Over");
nokiaLcd.setCursor(15, 30);
nokiaLcd.print("Score: ");
nokiaLcd.print(score);
nokiaLcd.setCursor(15, 40);
nokiaLcd.print("High: ");
nokiaLcd.print(high);
nokiaLcd.display();
beep(20, 5000);
nokiaLcd.clearDisplay();
slength = 8; //Resetting the values
score = 0; time = 280; redraw(); //Restart game
by drawing snake with the resetted length and score
}
}
}
void checkegg() //Snake meets egg
{
if (x[0] == xegg or x[0]
== (xegg + 1) or x[0] == (xegg + 2) or x[0] == (xegg - 1)) //Snake in close
vicinity of egg
{
if (y[0] == yegg or y[0]
== (yegg + 1) or y[0] == (yegg + 2) or y[0] == (yegg - 1))
{
score += 1; //Increase length,score and increase
movement speed by decreasing 'time'
slength += 1;
if (time >= 90)
{
time -= 5;
}
nokiaLcd. llRect(xegg, yegg, 3, 3, WHITE); //De-lete the consumed egg
nokiaLcd.display();
beep(35, beeptime); //Beep with a sound of 35Hz
for 'beeptime' ms
xegg = random(1, 80); //Create New egg randomly
yegg = random(1, 40);
}
}
}
void direct() //Check if user pressed any keys
and change direction if so
{
if (l == HIGH and dr == false) //when key LEFT
is pressed ,L will become low
{
dl = true; du = false; dd = false;
tempx = x[0] - 3; //Save the new coordinates of
head in tempx,tempy
tempy = y[0];
ag = 1; //Do not change direction any further
for the ongoing 'time' milliseconds
}
else if (r == HIGH and dl == false)
{
dr = true;
du = false;
dd
= false; tempx = x[0] + 3; tempy = y[0]; ag = 1;
}
else if (u == HIGH and dd == false)
{
du = true; dl = false; dr = false; tempy = y[0]
- 3; tempx = x[0]; ag = 1;
}
else if (d == HIGH and du == false)
{
dd = true; dl = false; dr = false; tempy = y[0]
+ 3;
tempx = x[0]; ag =
1;
}
else if (p == HIGH) //Pause game for 5 seconds
{
nokiaLcd.clearDisplay();
nokiaLcd.setTextColor(BLACK);
nokiaLcd.setCursor(25, 10); nokiaLcd.setTextSize(1);
nokiaLcd.print("Pause");
nokiaLcd.display();
delay(1000);
while (!pushButton.onPress()); nokiaLcd.clearDisplay();
redraw(); //Redraw the snake and egg at the same
position as it was
}
}
void drawsnake() //Draw snake and egg at newly
changed positions
{
nokiaLcd. llRect(xegg, yegg, 3, 3, BLACK); //Draw egg at
new pos
nokiaLcd.drawCircle(x[0], y[0], 1, BLACK);
//Draw new head of snake
nokiaLcd.drawCircle(x[slength], y[slength], 1,
WHI-TE); //Delete old tail of snake
nokiaLcd.display();
}
void redraw() //Redraw ALL POINTS of snake and
egg
{
nokiaLcd. llRect(xegg, yegg, 3, 3, BLACK); for (i = 0; i < slength; i++) {
nokiaLcd.drawCircle(x[i], y[i], 1, BLACK);
}
nokiaLcd.display();
}
void beep (int freq, long tb) //This function
creates a sound of frequency 'freq' Hz and for a duration of 'tb'
milliseconds
{
int x;
long delayAmount = (long)(500 / freq);
//Calcula-te time duration of half cycle
long loopTime = (long)(tb / (delayAmount * 2));
//Calculate no. of cycles
for (x = 0; x < loopTime; x++) // One
iteration produces sound for one cycle
{
digitalWrite(PIEZOSPEAKER_PIN_SIG, HIGH); //High
for half cycle
delay(delayAmount);
digitalWrite(PIEZOSPEAKER_PIN_SIG, LOW);
//Low for half cycle
delay(delayAmount);
} delay(2); //a little delay to make all notes
sound separate
}
===
Kode Program ===
0 komentar:
Posting Komentar