Cov txheej txheem:

Arduino - Piano Vuas: 16 Kauj Ruam (nrog Duab)
Arduino - Piano Vuas: 16 Kauj Ruam (nrog Duab)

Video: Arduino - Piano Vuas: 16 Kauj Ruam (nrog Duab)

Video: Arduino - Piano Vuas: 16 Kauj Ruam (nrog Duab)
Video: ANDROID CAMERA vs. IPHONE CAMERA 2024, Hlis ntuj nqeg
Anonim
Arduino - Piano Pobzeb
Arduino - Piano Pobzeb

Nyob zoo cov neeg siv internet, Qhov no yuav yog yuav ua li cas thiaj li ua tau DEFINITELY tsis yog tshem tawm ntawm kev ua si mobile ntawm arduino uno r3.

yog li pib, koj yuav xav tau txhua ntu, uas yog raws li hauv qab no! 1x Arduino Uno r3 ($ 42)

2x LCD Keypad Shield ($ 19 txhua)

5 x nub

5 x 220Ω resistors

28x cev

Zoo, thaum koj tau txais tag nrho cov ntu nws yog lub sijhawm kom pib!

Kauj ruam 1: Kev thaiv kab

Thaiv
Thaiv
Thaiv
Thaiv
Thaiv
Thaiv

Pib los ntawm kev txuas koj li arduino thiab cov phooj ywg raws li qhia hauv daim duab, Nco ntsoov tias cov nyees khawm tau txuas rau txoj hauv kev ib puag ncig, nrog A0-4 qhov qhib rau hauv av hauv qab ntawm lub khawm kab, lossis cov arduino yuav hloov qhov xav tias cov nyees khawm raug tuav tas li tsis txhob siv xovxwm nkaus xwb.

Kauj Ruam 2: Cov Lus Tshaj Tawm

Txhua tus lej ntawm no yuav tsum mus ua ntej koj qhov kev teeb tsa tsis muaj dab tsi thiab lub voj void, qhov no yog vim tias txhua qhov kev hloov pauv thiab cov khoom siv tau siv ntau qhov haujlwm peb yuav teeb tsa.

Pib los ntawm kev muab:

#suav nrog

nyob rau sab saum toj ntawm koj cov cai, qhov no yog qhia rau arduino siv lub tsev qiv ntawv "LiquidCrystal.h" thiab cov haujlwm uas yog ib feem ntawm nws.

Cov kauj ruam tom ntej yog txhawm rau txheeb cov pins peb tab tom siv rau peb cov nyees khawm los ntawm kev tso cov cai no hauv qab peb #include:

#define btnEnter A0 #txhais btn1 15 #define btn2 16 #define btn3 17 #define btn4 18

Peb txhais cov ntsiab lus btnEnter thiab btn1 rau btn 4 kom cov cai yooj yim rau peb nyeem, lossis hloov yog tsim nyog. Nws txhais tau tias thaum peb ntaus btn1 lub arduino yuav paub peb qhov tseeb txhais tau tias khawm 15. Txawm hais tias peb tab tom hu xov tooj rau chaw nres nkoj 15, 16, 17 thiab 18, lawv tau sau rau ntawm arduino li A1 A2 A3 thiab A4, qhov no yog vim lawv yog cov chaw nres nkoj siv tshwj xeeb rau cov tswv yim analog, txawm hais tias peb tsuas yog siv lawv rau cov tswv yim digital.

Tom ntej no peb yuav tsim cov khoom uas yuav tswj hwm Cov Khoom Siv Crystal Crystal Displays. Txhawm rau ua qhov no muab cov cai hauv qab no peb txhais

LiquidCrystal lcdLeft (8, 9, 12, 10, 11, 13); LiquidCrystal lcdRight (2, 3, 4, 5, 6, 7);

Qhov no ua dab tsi yog qhia rau arduino tias thaum peb hu lcdLeft lossis lcdRight peb tab tom hais txog cov khoom siv LiquidCrystal. Cov lej hauv cov kab zauv txuas qhia rau arduino qhov chaw nres nkoj cov khoom yuav tsum siv los xa xov mus rau LCD thaum peb siv lawv cov haujlwm.

Tam sim no peb yuav tsum tshaj tawm cov kev hloov pauv los ntawm kev tso me ntsis ntxiv ntawm cov cai hauv qab cov lus tshaj tawm:

// cov kev hloov pauv no yog cov kev xaiv uas koj tuaj yeem hloov pauv - tus lej ntau dua = kev ua si nrawm dua nrawm intGameSpeedEasy = 10; int intGameSpeedMedium = 25; int intGameSpeedHard = 80;

// teeb tsa qhov hloov pauv rau gameboolean bolPlay; // taug qab yog tias tus neeg uas ua ntawv int intScore; // taug qab cov neeg ua si cov qhab nia int intDiff; // tsuas yog ib qho estetic rau qhia dab tsi nyuaj qhov kev ua si yog ntawm // teeb tsa qhov txawv rau kev nkag mus rau intEnter; // taug qab yog tias tus neeg siv nias lub pob nkag mus int intInput; // taug qab cov nyees khawm uas tus neeg siv nias boolean bolTilePressed; // xyuas kom cov neeg uas ua ntawv tsis txhob nias lub pob 5x thiab poob // teeb tsa qhov sib txawv rau tig int intTick; // suav txog millies (ib lub voj) kom txog thaum intDelay int intDelay; // lub sijhawm qhov kev zov me nyuam tos txog lub sijhawm tom ntej hauv millis int intGameSpeed; // abit ntawm debug xaiv boolean bolSerialBoard; // thaum muaj tseeb yuav luam tawm lub rooj tsavxwm hauv cov saib

Peb tshaj tawm qhov sib txawv los ntawm kev txheeb xyuas hom ntaub ntawv thiab tom qab ntawd hloov pauv lub npe, ex. int qhov noIsAnInteger

Boolean hloov pauv, xws li bolSerialBoard thiab bolPlay tuaj yeem tsuas muaj ib ntawm ob qhov txiaj ntsig, muaj tseeb lossis tsis tseeb.

Integer variable (int) xws li intScore thiab intInput tuaj yeem suav tus lej raws li qhov muaj txiaj ntsig, xws li 1, 5 lossis 100.

Qee qhov lwm cov ntaub ntawv tseem ceeb uas peb tsis siv ntawm no yog ib txoj hlua, uas yog cov ntawv me me, thiab ntab, uas yog tus lej lej.

Txhua qhov kev hloov pauv ntawm no tau siv nyob rau ntau qhov chaw sib txawv los ntawm txoj haujlwm, ntawm no yog cov ntsiab lus ntawm txhua tus ua dab tsi

bolPlay qhia qhov program yog tias cov ntawv qhia zaub mov yuav tsum tshwm lossis yog tias qhov kev ua si tiag tiag yuav tsum tau khiav.

intScore taug qab cov neeg ua si cov qhab nia thaum lawv tsoo cov vuas, intDiff tau siv ntawm cov ntawv qhia zaub mov tseem ceeb los qhia rau cov haujlwm li cas cov ntawv me me los luam tawm ntawm LCD, intEnter yog siv los qhia qhov kev zov me nyuam yog tias khawm nkag (sab laug mus deb tshaj) raug nias, intInput tau siv los qhia qhov haujlwm uas ntawm lwm 4 khawm raug nias.

bolTilePressed tau siv los ua kom ntseeg tau tias txoj haujlwm tsuas yog nyeem thaum lub pob raug nias thiab tsis yog thaum nws tuav.

intGameSpeed, intGameSpeedEasy, intGameSpeedMedium thiab intGameSpeedHard tau siv los tswj qhov kev ua si sai npaum li cas yuav tsum nrawm dua raws qhov nyuaj uas tau xaiv.

intTick thiab intDelay tau siv los txwv txoj haujlwm los ntawm kev txav lub rooj tsavxwm txhua lub sijhawm nws loops.

bolSerialBoard tau siv los tso cai rau koj los ua qhov haujlwm xa lub rooj tsav xwm mus rau arduino's serial monitor raws li cov lej ntawm cov phiaj xwm ntsuas.

Thaum kawg nws yog lub sijhawm los tshaj tawm peb lub rooj tsavxwm raws li kev siv cov cai no:

// teeb tsa game arrayint arrGame [16] [4] = {{0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0, 0}};

Ib qho array yog cov lej uas txhua lub ntsiab lus uas tuaj yeem hu ua lej lossis hloov pauv.

Koj tus lej yuav tsum zoo li tam sim no;

// suav nrog cov tsev qiv ntawv#suav nrog

// cov kev hloov pauv no yog cov kev xaiv uas koj tuaj yeem hloov pauv - cov lej ntau dua = kev ua si nrawm dua

int intGameSpeedEasy = 10; int intGameSpeedMedium = 25; int intGameSpeedHard = 80;

// Txhais pins

#define btnEnter A0 #define btn1 15 #define btn2 16 #define btn3 17 #define btn4 18

// tsim cov khoom LCD (n, ~, n, ~, ~, n)

LiquidCrystal lcdLeft (8, 9, 12, 10, 11, 13); LiquidCrystal lcdRight (2, 3, 4, 5, 6, 7);

// teeb tsa game array

int arrGame [16] [4] = {{0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};

// teeb tsa qhov sib txawv rau qhov kev ua si

boolean bolPlay; // taug qab yog tias tus neeg uas ua ntawv int intScore; // taug qab cov neeg ua si cov qhab nia int intDiff; // tsuas yog ib qho tseem ceeb tshaj rau qhia qhov nyuaj qhov kev ua si tau ua

// teeb tsa cov kev hloov pauv rau cov tswv yim

koj intEnter; // taug qab yog tias tus neeg siv nias lub pob nkag mus int intInput; // taug qab cov nyees khawm uas tus neeg siv nias boolean bolTilePressed; // xyuas kom cov neeg uas ua ntawv tsis txhob nias lub pob 5x thiab poob

// teeb tsa qhov sib txawv rau tig

koj intTick; // suav txog millies (ib lub voj) kom txog thaum intDelay int intDelay; // lub sijhawm qhov kev zov me nyuam tos txog lub sijhawm tom ntej hauv millis int intGameSpeed;

// abit ntawm debug xaiv

boolean bolSerialBoard; // thaum muaj tseeb yuav luam tawm lub rooj tsavxwm hauv cov saib

Kauj Ruam 3: Teeb Ua Haujlwm

Teeb lub voj yog txoj haujlwm nyeem los ntawm arduino tsuas yog ib zaug thaum nws pib pib.

Hauv kev teeb tsa lub voj peb tsuas yog teeb tsa qhov tseem ceeb ntawm ob peb ntawm peb qhov kev hloov pauv vim tias tsis yog teeb tsa lawv tus nqi thaum tshaj tawm lawv peb ua nws ntawm no.

Pib los ntawm kev muab cov cai no tso rau hauv koj qhov teeb tsa tsis raug.

bolPlay = tsis tseeb; intScore = 0; intTick = 0; intDelay = 1000; intDiff = 1; intGameSpeed = intGameSpeedMedium;

Txhua kab tsuas yog teeb tsa qhov sib txawv rau tus nqi.

bolPlay tau teeb tsa cuav kom qhov kev ua si tsis pib ua si.

intScore tau teeb tsa rau 0, vim ib txwm koj cov qhab nia pib ntawm 0.

intTick pib ntawm 0 vim tias txoj haujlwm tam sim no tsis suav txhua yam.

intDelay tau teeb tsa rau 1000 vim tias yog tus nqi vuas pib ntawm.

intDiff tsuas yog ib qho ascetic thiaj li hais tias txoj haujlwm paub sau dab tsi rau qhov kev ua si nyuaj.

intGameSpeed tau teeb tsa rau yam twg intGameSpeedMedium yog, lub ntsiab lus nws tau teeb tsa ntawm qhov nyuaj nruab nrab.

Tom ntej no muab cov cai no tso rau hauv Void Setup hauv qab cov cai koj nyuam qhuav tso rau hauv.

lcdLeft.begin (16, 2); lcdRight.begin (16, 2);

Serial.begin (9600);

Qhov no qhia rau arduino kom pib sib txuas lus nrog lub khoos phis tawj los ntawm kev tshuaj xyuas ntu (pom los ntawm nyem lub pob nyob rau sab xis saum toj ntawm arduino IDE).

Koj Lub Void Setup yuav tsum zoo li tam sim no!

teeb tsa tsis muaj dab tsi () {Serial.begin (9600); // pib qhov ntsuas saib // teeb tsa qhov sib txawv bolPlay = tsis tseeb; intScore = 0; intTick = 0; intDelay = 1000; intDiff = 1; intGameSpeed = intGameSpeedMedium; // pib lcd's lcdLeft.begin (16, 2); lcdRight.begin (16, 2); }

Kauj Ruam 4: Lub Loop Function

Lub voj ua haujlwm yog ua los ntawm arduino txhua qhov rov ua dua ntawm arduino.

Luam cov cai hauv qab no rau hauv koj lub Void Loop.

lub voj void () {input (); // kos rau kev ua si yog tias (bolPlay == muaj tseeb) {yog (intTick> = intDelay) {// txheeb xyuas seb qhov kev ua si yuav tsum tau tig lossis txuas ntxiv mus tos Serial.println ("~~~~~~~ ~~ "); // luam tawm lub ntsiab lus ntawm pawg thawj coj saib ntawm // writeSerial (); // yog tias qhov kev xaiv tau qhib sau lub rooj tsavxwm rau hauv cov lej ua siGame (); // txheeb xyuas cov neeg siv khoom nkag playBoard (); // txav lub rooj tsavxwm thiab ntxiv cov nplais tshiab clearLcd (); // ntxuav lub LCDs ua ntej kos drawBoard (); // kos lub rooj tsavxwm mus rau lub lcd's bottomCheck (); intTick = 0; // pib dua intTick} lwm tus {buttonsGame (); // txheeb xyuas cov neeg ua si inputs clearLcd (); // ntxuav lub LCDs ua ntej kos drawBoard (); // kos lub rooj tsavxwm mus rau lcd's intTick = intTick + intGameSpeed; // ntxiv rau zuam}} lwm qhov {clearLcd (); // ntxuav lub LCDs ua ntej kos npe (); // tso tawm lub npe thiab cov ntsiab lus cov ntsiab lus khawmMenu (); // nyeem cov neeg siv tswv yim clearBoard (); // xyuas kom tag nrho pawg thawj coj = 0} ncua (10); // ncua lub arduino los ntawm lub sijhawm luv luv}

thaum bolPlay sib npaug rau qhov tseeb nws txhais tau tias qhov kev ua si tau ua si, thiab txhua txoj cai rau thaum twg qhov kev ua si yuav tsum tau ua, tab sis peb tsuas xav kom pawg thawj coj saib ntxiv cov pobzeb tshiab thiab txav mus thaum intTick ntau dua li peb intDelay, txwv tsis pub peb tseem xav tso cai rau tus neeg siv nias lub pob kom ntaus pobzeb thiab rau intTick kom nce nrawm dua.

Feem ntau ntawm cov cai no siv cov haujlwm peb tseem tsis tau ua, thiab peb yuav ua rau lawv hauv cov kauj ruam tom ntej. Lub hom phiaj ntawm cov haujlwm no yog raws li hauv qab no.

Cov tswv yim nyeem cov nyees khawm uas tus neeg siv tau nias.

buttonGame tswj cov nyees khawm ua thaum nyob hauv qhov kev ua si, thiab tsis yog hauv cov ntawv qhia zaub mov

playBoard ntxiv cov nplais tshiab rau lub rooj tsavxwm, thiab tom qab ntawd txav txhua yam hauv pawg thawj coj saib mus rau ib qho chaw

clearLCD ntxuav lub LCD kom paub tseeb tias tsis muaj dab nyob hauv qab cov vuas

drawBoard hla dhau arrGame thiab luam tawm nws mus rau LCDs

clearBoard tshem tawm tag nrho ntawm arrGame thaum qhov kev ua si tsis nyob hauv kev ua si

bottomCheck txheeb xyuas hauv qab ntawm arrGame rau qhov ua tsis tiav

lub npe qhia cov npe game thiab cov ntaub ntawv qhab nia thaum nyob hauv cov ntawv qhia zaub mov

cov ntawv qhia zaub mov tswj hwm tus neeg siv cov khoom siv ua dab tsi thaum nyob hauv cov ntawv qhia zaub mov.

gameOver yog lwm txoj haujlwm, txawm hais tias tsis tau hu ntawm no vim nws tau hloov pauv hu ua hauv qab Kev Txheeb Xyuas thiab cov khawm kev ua si.

Kauj Ruam 5: Txoj Haujlwm ClearLCD

los tsim txoj haujlwm peb pib los ntawm kev ntxiv qhov no rau cov cai

tsis muaj nuj nqi functionName () {

}

lub "functionName" tuaj yeem yog ib qho, tsuav nws tsis muaj nyob.

Luam cov cai no rau hauv koj qhov haujlwm:

void clearLcd () {rau (int i = 0; i <= 15; i ++) {rau (int ii = 0; ii <= 1; ii ++) {lcdLeft.setCursor (i, ii); lcdLeft.write (""); lcdRight.setCursor (kuv, ii); lcdRight.write (""); }}}

qhov no hla dhau tag nrho cov kab ke los ntawm kev siv 2 suav cov loops mus dhau txhua kis ntawm LCDs thiab sau qhov chaw.

Tsis tau rov pib dua tsis muaj dab tsi LCD's yuav tswj hwm txhua yam uas tau sau yav tas los

Kauj Ruam 6: DrawBoard Function

theej cov cai no rau hauv koj qhov program

void drawBoard () {rau (int i = 1; i <= 15; i ++) {// kos collums 1 thiab 2 ntawm sab laug LCD // yog lub pobzeb = 0 tsis sau dab tsi, = 1 sau "#", = 2 sau "@" lcdLeft.setCursor (i, 1); // teeb tsa thawj kab ntawv (sab laug deb tshaj) yog (arrGame [0] == 1) {lcdLeft.write ("#");} yog (arrGame [0] == 2) {lcdLeft.write ("@");} lcdLeft.setCursor (kuv, 0); // teeb tsa rau ntu thib ob (nruab nrab sab laug) yog (arrGame [1] == 1) {lcdLeft.write ("#");} yog (arrGame [1] == 2) {lcdLeft.write ("@");} lcdRight.setCursor (kuv, 1); // teeb tsa rau ntu thib peb (nruab nrab txoj cai) yog (arrGame [2] == 1) {lcdRight.write ("#");} yog (arrGame [2] == 2) {lcdRight.write ("@");} lcdRight.setCursor (kuv, 0); // teeb tsa mus rau ntu plaub ntu (deb tshaj txoj cai) yog (arrGame [3] == 1) {lcdRight.write ("#");} yog (arrGame [3] == 2) {lcdRight.write ("@");}}}

qhov no siv lub voj los hla txhua kab ntawm pawg thawj coj, nws tom qab ntawd tshuaj xyuas seb puas muaj ib kab hauv kab sib npaug 1 lossis 2, raws li qhov ntawd nws yuav luam tawm ntawm LCD ib qho hashtag, rau cov nplais tseem tsis tau ntaus, lossis @ rau ntaus pobzeb.

Kauj Ruam 7: PlayBoard Function

theej cov cai no rau hauv koj qhov program.

void playBoard () {rau (int i = 0; i <= 3; i ++) {arrGame [0] = 0;} // tshem tawm cov kab saum toj kawg nkaus arrGame [0] [random (0, 4)] = 1; // teeb tsa lub ntsiab lus tsis sib xws ntawm kab saum toj kawg nkaus los ua pobzeb rau (int i = 15; i> = 1; i-) {// ua haujlwm los ntawm hauv qab ntawm pawg thawj coj saib rau sab saum toj rau (int ii = 0; ii <= 3; ii ++) {// rau txhua qhov kev sib tw arrGame [ii] = arrGame [i - 1] [ii]; }}}

txoj cai no pib los ntawm kev tshem tag nrho cov kab saum toj kawg nkaus rau 0, lossis tsis muaj pobzeb, thiab tom qab ntawd teeb tsa ib lub pobzeb tsis sib xws los ua 1, thiab tshem tawm cov nplais.

Nws tom qab ntawd dhau los ntawm suav lub voj nyob rau hauv thim rov qab, los ntawm 15 txog 1, teeb tsa kab kom sib npaug li cas ntawm kab saum toj no nws yog sib npaug rau, ua rau pawg thawj coj txav hauv LCD

Kauj Ruam 8: Txoj Haujlwm ClearBoard

theej cov cai no rau hauv koj qhov program.

void clearBoard () {// rov pib zuam thiab ncua qhov txiaj ntsig intTick = 0; intDelay = 1000; // hla dhau lub rooj tsavxwm thiab teeb txhua yam rau 0 rau (int i = 0; i <= 15; i ++) {rau (int ii = 0; ii <= 3; ii ++) {arrGame [ii] = 0; }}}

Txoj cai no tau khiav thaum qhov kev ua si tsis ua si kom ntseeg tau tias tag nrho ntawm arrGame tau teeb tsa rau 0, lossis tsis muaj cov vuas, los ntawm kev siv suav loops kom dhau los ntawm kab.

Txoj cai kuj rov pib dua qhov tseem ceeb ntawm intDelay thiab intTick.

Kauj Ruam 9: Lub Npe Ua Haujlwm

theej cov cai hauv qab no rau hauv koj qhov program

void title () {// sau lub npe rau LCD thiab qhov chaw kom tau qhab nia lcdRight.setCursor (0, 0); lcdRight.write ("Pobzeb Piano"); lcdRight.setCursor (0, 1); lcdRight.write ("Qeb:"); // hloov cov qhab nia rau hauv txoj hlua char strScore [3]; sprintf (strScore, "%d", intScore); // tso cov qhab nia mus rau LCD lcdRight.write (strScore); // ntxiv qhov sib txawv lcdRight.setCursor (10, 1); yog (intDiff == 0) {lcdRight.write ("Yooj Yim"); } yog (intDiff == 1) {lcdRight.write ("Nruab Nrab"); } yog (intDiff == 2) {lcdRight.write ("Nyuaj"); } // Nias qhov qhia me ntsis lcdLeft.setCursor (0, 0); lcdLeft.write ("Nias Sau"); lcdLeft.setCursor (0, 1); lcdLeft.write ("pib!"); }

Txoj cai no sau lub npe ntawm qhov kev ua si thiab tus qhab nia mus rau LCDs, nws ua qhov no los ntawm kev qhia LCD uas yuav pib sau siv LCD.setCursor thiab tom qab ntawv sau txoj hlua hauv LCD.write.

Qhov hloov pauv tshiab kuj tseem tsim ntawm no, strScore, nws tau siv los hloov intScore rau hauv txoj hlua lossis cov ntaub ntawv hom siv cov haujlwm sprintf.

intDiff kuj tseem siv ntawm no, raws li nws qhov txiaj ntsig nws luam tawm qhov kev xaiv nyuaj sib txawv.

Kauj Ruam 10: Lub Ntej Cov Menyuam Ua Haujlwm

ntxig cov cai hauv qab no rau hauv koj qhov program

void buttonsMenu () {// thaum nkag tau nias pib qhov game thiab rov pib dua tus nqi yog (intEnter == 1) {bolPlay = tseeb; intScore = 0; playBoard (); drawBoard (); } // thaum khawm 3 raug nias tig rau qhov kev xaiv debug ntawm luam tawm lub rooj tsavxwm hauv cov ntawv yog (intInput == 3) {yog (bolSerialBoard == tsis tseeb) {Serial.println ("Serial Board Active"); bolSerialBoard = muaj tseeb; } lwm {Serial.println ("Pawg Neeg Saib Xyuas Xiam Oob Qhab"); bolSerialBoard = tsis tseeb; }} // teeb tsa kev ua si kom yooj yim yog tias (intInput == 0) {Serial.print ("Game teeb kom yooj yim ("); Serial.print (intGameSpeedEasy); Serial.println ("ms acceleration)"); intDiff = 0; intGameSpeed = intGameSpeedEasy; } // teeb tsa kev ua si nrawm rau qhov nyuaj nruab nrab yog (intInput == 1) {Serial.print ("Game set to medium ("); Serial.print (intGameSpeedMedium); Serial.println ("ms acceleration)"); intDiff = 1; intGameSpeed = intGameSpeedMedium; } // teeb tsa kev ua si nrawm rau qhov nyuaj yog tias (intInput == 2) {Serial.print ("Game set to hard ("); Serial.print (intGameSpeedHard); Serial.println ("ms acceleration)"); intDiff = 2; intGameSpeed = intGameSpeedHard; }}

txoj cai no tsuas yog siv thaum bolPlay sib npaug rau qhov tsis raug nyob hauv qhov khoob khoob

yog tias intEnter tau teeb tsa 1 nws txhais tau tias khawm nkag tau raug thawb, yog tias nws tau raug nias qhov program teeb tsa bolPlay kom raug thiab qhov kev ua si pib.

Qhov kev zov me nyuam mam li nyeem dab tsi intInput yog sib npaug rau. yog tias nws sib npaug 0 thawj lub khawm los ntawm sab laug raug nias, nce mus rau sab xis txog 3. Yog tias intInput sib npaug rau 4 tsis muaj khawm raug nias.

yog tias cov nyees khawm 0-2 raug nias qhov kev ua si hloov pauv qhov nyuaj, tseem kho tus nqi kev ua si nrawm, lub ntsiab lus nws yuav ua kom nrawm dua.

yog tias khawm 3 tau nias qhov kev ua si yuav qhib lossis ua kom tsis muaj kev debug qhov twg tag nrho lub rooj tsav xwm tau luam tawm hauv cov ntawv saib xyuas kom pab nrhiav teeb meem hauv qhov program.

Kauj Ruam 11: Txoj Haujlwm Ua Ntej

theej cov cai hauv qab no rau hauv koj qhov program

void buttonsGame () {yog (intInput! = 4) {// yog tias lub pob raug nias yog (bolTilePressed == cuav) {// tsuas yog bolTilePressed yog qhov ua tsis raug rau kev txheeb xyuas lub pob nyem bolTilePressed = tseeb; // tom qab teeb tsa bolTilePressed rau qhov tseeb kom paub tseeb tias nws tsis tshwm sim tshwm sim dua int intLowestTile = 0; // kom tau teem rau cov nplais nrog cov pobzeb qis tshaj int intCheckedTile = 15; // kom taug qab cov vuas twg tau raug tshuaj xyuas thaum (intLowestTile == 0) {// tsuav nws tsis tau teeb tsa ib yam los txheeb cov vuas rau (int i = 0; i 100) {// tsuav yog int ncua tsis qis dua 100 intDelay = intDelay - 20; // coj tus nqi los ntawm nws}} lwm {Serial.println ("Tsis raug khawm nias"); gameOver (); // lwm yam kev ua si dhau}}}}}

Txoj cai tsuas yog siv thaum bolPlay sib npaug rau qhov tseeb hauv qhov khoob khoob.

Zoo li cov khawmMenu raws li tus nqi ntawm kev nkag nkag nws kuaj xyuas seb tus neeg uas ua ntawv tau tsoo pobzeb lossis plam ib qho.

Nws ua qhov no los ntawm kev mus los ntawm arrGame los ntawm hauv qab mus rau sab saum toj siv lub sijhawm voj los saib seb kab twg qis tshaj nrog cov nplais uas tsis tau ua. Nws mam li tshuaj xyuas seb qhov chaw nyob hauv kab ntawd sib thooj rau lub pob thawb yog cov pobzeb tsis sib dhos lossis tsis yog, yog tias nws yog unhit nws teeb nws kom sib npaug 2 hloov ntawm 1, lub ntsiab lus nws yuav tso tawm ua tus @, txwv tsis pub nws ua rau gameOver muaj nuj nqi uas peb tseem tsis tau tsim.

Txoj haujlwm no tseem siv qhov sib txawv bolTilePressed los ntawm kev teeb tsa nws kom raug thaum lub pob raug nias thiab tsis raug thaum tsis muaj lub pob nyem. Qhov no yog txhawm rau xyuas kom tus neeg siv tsis txhob poob qhov kev ua si vim qhov program xav tias lawv nias lub pob ntau zaug thaum lawv tuav nws.

Kauj Ruam 12: GameOver Function

Luam cov cai hauv qab no rau hauv koj qhov program

tsis muaj gameOver () {Serial.println ("Game Over!"); Serial.print ("Koj qhov qhab nia yog:"); Serial.println (intScore); Serial.print ("Koj qhov nrawm yog:"); Serial.println (intDelay); bolPlay = tsis tseeb; }

Qhov no tau tshwm sim los ntawm ob lub checkBottom lossis buttonGame kev ua haujlwm thiab ua rau qhov kawg ntawm nws qhov kev ua si los ntawm kev teeb tsa bolPlay kom tsis raug.

Nws kuj tseem luam tawm cov lus rau hauv kab ntawv saib rau cov neeg siv cov qhab nia thiab cov vuas nrawm tau ntxiv hauv milliseconds.

Kauj Ruam 13: Lub Tswv Yim Ua Haujlwm

Luam cov cai hauv qab no rau hauv koj qhov program.

void input () {intEnter = digitalRead (btnEnter); // nyeem nkag // nyeem qhov twg ntawm lwm qhov kev nkag mus, lossis yog tias tsis tau teeb tsa 4 yog (digitalRead (btn1) == HIGH) {intInput = 0;} ntxiv {yog (digitalRead (btn2) == HIGH) {intInput = 1;} lwm yam {yog (digitalRead (btn3) == HIGH) {intInput = 2;} lwm yam {yog (digitalRead (btn4) == HIGH) {intInput = 3;} lwm yam {intInput = 4; }}}} // serial luam tawm cov ntawv nkag yog (intEnter == 1) {Serial.println ("Enter Pressed!");} if (intInput! = 4) {Serial.print ("Button Press:"); Serial.println (intInput); } lwm tus {// yog tias tsis muaj khawm raug nias rov pib dua bolTilePressed bolTilePressed = cuav; }}

Txoj cai no tau siv nrog cov khawm Cov Kev Ua Si thiab cov khawm Menu cov haujlwm. raws li cov nyees khawm cov neeg siv tau nias nws teev tus nqi ntawm intInput, lossis yog tias tsis muaj khawm raug nias nws teeb tsa intInput kom sib npaug 4.

Yog tias tsis muaj khawm tau nyem qhov no yog qhov twg bolTilePressed tau rov pib dua rau cov khawm ua si.

Nws kuj tseem luam tawm cov lus mus rau tus lej saib xyuas uas lub khawm raug nias.

Kauj Ruam 14: Kev Tshawb Fawb Hauv Qab

theej cov cai hauv qab no rau koj qhov program.

void bottomCheck () {rau (int i = 0; i <= 3; i ++) {// rau 4 kab ntawv yog (arrGame [15] == 1) {// yog tias cov nplais nyob hauv qab Serial.println ("Pobzeb hauv qab"); arrGame [15] = 2; drawBoard (); ncua (400); arrGame [15] = 1; drawBoard (); ncua (400); arrGame [15] = 2; drawBoard (); ncua (400); arrGame [15] = 1; drawBoard (); ncua (400); gameOver (); }}}

siv lub voj no txoj cai tshuaj xyuas kab hauv qab ntawm arrGame rau ib qho vuas tsis sib xws (vuas sib npaug rau 1), yog tias muaj cov nplais uas tsis tau muab tso rau hauv qab ntawm lub vijtsam nws yuav nyem lub pobzeb thiab tom qab ntawd ua rau muaj kev ua si ntau dua.

Kauj Ruam 15: Kev Sau Ua Haujlwm

theej cov cai hauv qab no rau hauv koj qhov program

void writeSerial () {yog (bolSerialBoard == tseeb) {rau (int i = 0; i <= 15; i ++) {rau (int ii = 0; ii <= 3; ii ++) {Serial.print (arrGame [ii] [ii]); Serial.print (","); } Serial.println (""); }}}

Nov yog txoj haujlwm ua raws los ntawm qhov kev xaiv debug uas tuaj yeem ua haujlwm hauv cov khawm Menu muaj nuj nqi. Yog tias bolSerialBoard tau teeb tsa kom muaj tseeb hauv txoj haujlwm ntawd nws yuav dhau los ntawm arrGame thiab luam tawm tag nrho lub rooj tsavxwm mus rau hauv lub vijtsam saib rau kev ntsuas lub hom phiaj los ntawm kev siv ib qho array.

Kauj ruam 16: Ua tiav

Ua tiav!
Ua tiav!

Txhua yam ntawm koj cov cai yuav tsum tsis ua tiav thiab saib qee yam zoo li no!

/ * * Lub Npe - Pobzeb Piano; Arduino * Los Ntawm - Domenic Marulli * Hnub - 11/ *

/ suav nrog cov tsev qiv ntawv

#suav nrog

// cov kev hloov pauv no yog cov kev xaiv uas koj tuaj yeem hloov pauv - cov lej ntau dua = kev ua si nrawm dua

int intGameSpeedEasy = 10; int intGameSpeedMedium = 25; int intGameSpeedHard = 80;

// Txhais pins

#define btnEnter A0 #define btn1 15 #define btn2 16 #define btn3 17 #define btn4 18

// tsim cov khoom LCD (n, ~, n, ~, ~, n)

LiquidCrystal lcdLeft (8, 9, 12, 10, 11, 13); LiquidCrystal lcdRight (2, 3, 4, 5, 6, 7);

// teeb tsa game array

int arrGame [16] [4] = {{0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};

// teeb tsa qhov sib txawv rau qhov kev ua si

boolean bolPlay; // taug qab yog tias tus neeg uas ua ntawv int intScore; // taug qab cov neeg ua si cov qhab nia int intDiff; // tsuas yog ib qho tseem ceeb tshaj rau qhia qhov nyuaj qhov kev ua si tau ua

// teeb tsa cov kev hloov pauv rau cov tswv yim

koj intEnter; // taug qab yog tias tus neeg siv nias lub pob nkag mus int intInput; // taug qab cov nyees khawm uas tus neeg siv nias boolean bolTilePressed; // xyuas kom cov neeg uas ua ntawv tsis txhob nias lub pob 5x thiab poob

// teeb tsa qhov sib txawv rau tig

koj intTick; // suav txog millies (ib lub voj) kom txog thaum intDelay int intDelay; // lub sijhawm qhov kev zov me nyuam tos txog lub sijhawm tom ntej hauv millis int intGameSpeed;

// abit ntawm debug xaiv

boolean bolSerialBoard; // thaum muaj tseeb yuav luam tawm lub rooj tsavxwm hauv cov saib

// teeb tsa uas yuav ua haujlwm ib zaug

teeb tsa tsis muaj dab tsi () {Serial.begin (9600); // pib qhov ntsuas saib // teeb tsa qhov sib txawv bolPlay = tsis tseeb; intScore = 0; intTick = 0; intDelay = 1000; intDiff = 1; intGameSpeed = intGameSpeedMedium; // pib lcd's lcdLeft.begin (16, 2); lcdRight.begin (16, 2); }

// lub voj uas yuav khiav txhua 10 millisecons

lub voj void () {input (); // kos rau kev ua si yog tias (bolPlay == muaj tseeb) {yog (intTick> = intDelay) {// txheeb xyuas seb qhov kev ua si yuav tsum tau tig lossis txuas ntxiv mus tos Serial.println ("~~~~~~~ ~~ "); // luam tawm lub ntsiab lus ntawm pawg thawj coj saib ntawm // writeSerial (); // yog tias qhov kev xaiv tau qhib sau lub rooj tsavxwm rau hauv cov lej ua siGame (); // txheeb xyuas cov neeg siv khoom nkag playBoard (); // txav lub rooj tsavxwm thiab ntxiv cov nplais tshiab clearLcd (); // ntxuav lub LCDs ua ntej kos drawBoard (); // kos lub rooj tsavxwm mus rau lub lcd's bottomCheck (); intTick = 0; // pib dua intTick} lwm tus {buttonsGame (); // txheeb xyuas cov neeg ua si inputs clearLcd (); // ntxuav lub LCDs ua ntej kos drawBoard (); // kos lub rooj tsavxwm mus rau lcd's intTick = intTick + intGameSpeed; // ntxiv rau zuam}} lwm qhov {clearLcd (); // ntxuav lub LCDs ua ntej kos npe (); // tso tawm lub npe thiab cov ntsiab lus cov ntsiab lus khawmMenu (); // nyeem cov neeg siv tswv yim clearBoard (); // xyuas kom tag nrho pawg thawj coj = 0} ncua (10); // ncua lub arduino los ntawm lub sijhawm luv luv}

// ntxuav lub lcd, yog li txhua lub cell uas tsis tau sau tseg tsis nyob ntawd

void clearLcd () {rau (int i = 0; i <= 15; i ++) {rau (int ii = 0; ii <= 1; ii ++) {lcdLeft.setCursor (i, ii); lcdLeft.write (""); lcdRight.setCursor (kuv, ii); lcdRight.write (""); }}}

// kos lub rooj tsavxwm rau ntawm lub lcd

void drawBoard () {rau (int i = 1; i <= 15; i ++) {// kos collums 1 thiab 2 ntawm sab laug LCD // yog lub pobzeb = 0 tsis sau dab tsi, = 1 sau "#", = 2 sau "@" lcdLeft.setCursor (i, 1); // teeb tsa thawj kab ntawv (sab laug deb tshaj) yog (arrGame [0] == 1) {lcdLeft.write ("#");} yog (arrGame [0] == 2) {lcdLeft.write ("@");} lcdLeft.setCursor (kuv, 0); // teeb tsa rau ntu thib ob (nruab nrab sab laug) yog (arrGame [1] == 1) {lcdLeft.write ("#");} yog (arrGame [1] == 2) {lcdLeft.write ("@");} lcdRight.setCursor (kuv, 1); // teeb tsa rau ntu thib peb (nruab nrab txoj cai) yog (arrGame [2] == 1) {lcdRight.write ("#");} yog (arrGame [2] == 2) {lcdRight.write ("@");} lcdRight.setCursor (kuv, 0); // teeb tsa mus rau ntu plaub ntu (deb tshaj txoj cai) yog (arrGame [3] == 1) {lcdRight.write ("#");} yog (arrGame [3] == 2) {lcdRight.write ("@");}}}

// txav lub rooj tsavxwm nqes thiab tso tus lej tsis raug los ua cov pobzeb

void playBoard () {rau (int i = 0; i <= 3; i ++) {arrGame [0] = 0;} // tshem tawm cov kab saum toj kawg nkaus arrGame [0] [random (0, 4)] = 1; // teeb tsa lub ntsiab lus tsis sib xws ntawm kab saum toj kawg nkaus los ua pobzeb rau (int i = 15; i> = 1; i-) {// ua haujlwm los ntawm hauv qab ntawm pawg thawj coj saib rau sab saum toj rau (int ii = 0; ii <= 3; ii ++) {// rau txhua qhov kev sib tw arrGame [ii] = arrGame [i - 1] [ii]; }}}

// teeb tsa tag nrho lub rooj tsavxwm rau 0 thiab rov pib dua cov kev hloov pauv rau pregame

void clearBoard () {// rov pib zuam thiab ncua qhov txiaj ntsig intTick = 0; intDelay = 1000; // hla dhau lub rooj tsavxwm thiab teeb txhua yam rau 0 rau (int i = 0; i <= 15; i ++) {rau (int ii = 0; ii <= 3; ii ++) {arrGame [ii] = 0; }}}

// nthuav qhia Cov Ntawv Qhia Tseem Ceeb ntawm LCD

void title () {// sau lub npe rau LCD thiab qhov chaw kom tau qhab nia lcdRight.setCursor (0, 0); lcdRight.write ("Pobzeb Piano"); lcdRight.setCursor (0, 1); lcdRight.write ("Qeb:"); // hloov cov qhab nia rau hauv txoj hlua char strScore [3]; sprintf (strScore, "%d", intScore); // tso cov qhab nia mus rau LCD lcdRight.write (strScore); // ntxiv qhov sib txawv lcdRight.setCursor (10, 1); yog (intDiff == 0) {lcdRight.write ("Yooj Yim"); } yog (intDiff == 1) {lcdRight.write ("Nruab Nrab"); } yog (intDiff == 2) {lcdRight.write ("Nyuaj"); } // Nias qhov qhia me ntsis lcdLeft.setCursor (0, 0); lcdLeft.write ("Nias Sau"); lcdLeft.setCursor (0, 1); lcdLeft.write ("pib!"); }

// txheeb xyuas cov nyees khawm thiab yuav ua dab tsi rau lawv thaum tsis ua si

void buttonsMenu () {// thaum nkag tau nias pib qhov game thiab rov pib dua tus nqi yog (intEnter == 1) {bolPlay = tseeb; intScore = 0; playBoard (); drawBoard (); } // thaum khawm 3 raug nias tig rau qhov kev xaiv debug ntawm luam tawm lub rooj tsavxwm hauv cov ntawv yog (intInput == 3) {yog (bolSerialBoard == tsis tseeb) {Serial.println ("Serial Board Active"); bolSerialBoard = muaj tseeb; } lwm {Serial.println ("Pawg Neeg Saib Xyuas Xiam Oob Qhab"); bolSerialBoard = tsis tseeb; }} // teeb tsa kev ua si kom yooj yim yog tias (intInput == 0) {Serial.print ("Game teeb kom yooj yim ("); Serial.print (intGameSpeedEasy); Serial.println ("ms acceleration)"); intDiff = 0; intGameSpeed = intGameSpeedEasy; } // teeb tsa kev ua si nrawm rau qhov nyuaj nruab nrab yog (intInput == 1) {Serial.print ("Game set to medium ("); Serial.print (intGameSpeedMedium); Serial.println ("ms acceleration)"); intDiff = 1; intGameSpeed = intGameSpeedMedium; } // teeb tsa kev ua si nrawm rau qhov nyuaj yog tias (intInput == 2) {Serial.print ("Game set to hard ("); Serial.print (intGameSpeedHard); Serial.println ("ms acceleration)"); intDiff = 2; intGameSpeed = intGameSpeedHard; }}

// txheeb xyuas cov nyees khawm thiab yuav ua dab tsi rau lawv thaum ua si

void buttonsGame () {if (intInput! = 4) {// yog tias lub pob raug nias yog (bolTilePressed == cuav) {// tsuas yog bolTilePressed yog qhov ua tsis raug rau kev txheeb xyuas lub pob nyem bolTilePressed = tseeb; // tom qab teeb tsa bolTilePressed rau qhov tseeb kom paub tseeb tias nws tsis tshwm sim tshwm sim dua int intLowestTile = 0; // kom tau teem rau cov nplais nrog cov pobzeb qis tshaj int intCheckedTile = 15; // kom taug qab cov vuas twg tau raug tshuaj xyuas thaum (intLowestTile == 0) {// tsuav nws tsis tau teeb tsa ib yam los txheeb cov vuas rau (int i = 0; i 100) {// tsuav yog int ncua tsis qis dua 100 intDelay = intDelay - 20; // coj tus nqi los ntawm nws}} lwm {Serial.println ("Tsis raug khawm nias"); gameOver (); // lwm yam kev ua si dhau}}}}}

tsis muaj gameOver () {

Serial.println ("Game Over!"); Serial.print ("Koj qhov qhab nia yog:"); Serial.println (intScore); Serial.print ("Koj qhov nrawm yog:"); Serial.println (intDelay); bolPlay = tsis tseeb; }

// txheeb xyuas cov neeg siv khoom nkag

void input () {intEnter = digitalRead (btnEnter); // nyeem nkag // nyeem qhov twg ntawm lwm qhov kev nkag mus, lossis yog tias tsis tau teeb tsa 4 yog (digitalRead (btn1) == HIGH) {intInput = 0;} ntxiv {yog (digitalRead (btn2) == HIGH) {intInput = 1;} lwm yam {yog (digitalRead (btn3) == HIGH) {intInput = 2;} lwm yam {yog (digitalRead (btn4) == HIGH) {intInput = 3;} lwm yam {intInput = 4; }}}} // serial luam tawm cov ntawv nkag yog (intEnter == 1) {Serial.println ("Enter Pressed!");} if (intInput! = 4) {Serial.print ("Button Press:"); Serial.println (intInput); } lwm tus {// yog tias tsis muaj khawm raug nias pib dua bolTilePressed bolTilePressed = cuav; }}

// tshawb xyuas hauv qab ntawm lub rooj tsavxwm kom ua tsis tiav

void bottomCheck () {rau (int i = 0; i <= 3; i ++) {// rau 4 kab ntawv yog (arrGame [15] == 1) {// yog tias cov nplais nyob hauv qab Serial.println ("Pobzeb hauv qab"); arrGame [15] = 2; drawBoard (); ncua (400); arrGame [15] = 1; drawBoard (); ncua (400); arrGame [15] = 2; drawBoard (); ncua (400); arrGame [15] = 1; drawBoard (); ncua (400); gameOver (); }}}

// luam tawm lub rooj tsavxwm rau hauv tus saib xyuas yog tias bolSerialBoard muaj tseeb

void writeSerial () {yog (bolSerialBoard == tseeb) {rau (int i = 0; i <= 15; i ++) {rau (int ii = 0; ii <= 3; ii ++) {Serial.print (arrGame [ii] [ii]); Serial.print (","); } Serial.println (""); }}}

Thaum tag nrho cov cai nkag mus, upload rau koj arduino thiab txaus siab!

Pom zoo: