Cov txheej txheem:

Arduino Touchscreen Calculator: 7 Kauj Ruam
Arduino Touchscreen Calculator: 7 Kauj Ruam

Video: Arduino Touchscreen Calculator: 7 Kauj Ruam

Video: Arduino Touchscreen Calculator: 7 Kauj Ruam
Video: Touchscreen Calculator using Arduino 2024, Lub Xya hli ntuj
Anonim
Arduino Touchscreen Calculator
Arduino Touchscreen Calculator

Nyob zoo! Nov yog txoj haujlwm los ua lub laij lej suav siv lub Arduino Uno thiab daim iav TFT LCD. Kuv tau los nrog lub tswv yim rau kuv chav kawm ua haujlwm hauv tsev, thiab kev paub hauv kev tsim lub phiaj xwm no tau nthuav heev. Lub tshuab xam zauv no tuaj yeem ua plaub qhov kev ua lej yooj yim (ntxiv, rho tawm, sib ntxiv, thiab faib tawm). Nws kuj tseem nthuav tawm txog ob lub ntsiab lus sib luag rau kev faib cov lus teb uas muaj lawv. Cia peb dhia dej hauv! Cov khoom siv rau txoj haujlwm no tau teev tseg hauv qab no.

Khoom siv

- Arduino Ib

-2.4 TFT LCD Shield (ntawm no yog qhov kuv yuav nws:

- USB A rau B cable (qaum txuas Arduino rau lub khoos phis tawj)

- Khoos phis tawj nrog Arduino IDE teeb tsa

- Koj tseem yuav tsum rub ob lub tsev qiv ntawv: MCUFRIEND_kbv thiab Touchscreen. Thawj qhov koj tuaj yeem pom ntawm github (txuas: https://github.com/prenticedavid/MCUFRIEND_kbv) lossis koj tuaj yeem siv lub tsev qiv ntawv zip file kuv tau suav nrog hauv qab no. Qhov thib ob yog nyob hauv Arduino Library tus thawj tswj rau kev teeb tsa.

Kauj Ruam 1: Kev Sib Txuas Kho Vajtse

Kho vajtse sib txuas
Kho vajtse sib txuas
Kho vajtse sib txuas
Kho vajtse sib txuas
Kho vajtse sib txuas
Kho vajtse sib txuas

Kev sib txuas ntawm daim npog npog rau Arduino Uno yog qhov yooj yim thiab nrawm. Txhua yam koj yuav tsum ua yog kab kab qis tshaj plaws ntawm daim ntaub thaiv nrog tus lej qis tshaj ntawm Arduino thiab thawb daim ntaub thaiv rau hauv tus pin. Sab saum toj 5V tus pin thiab tus lej cim npe ntawm sab zog yuav tsum tsis muaj tus pin los ntawm daim ntaub thaiv hauv lawv, nrog tib yam tsis siv rau tus pin cim SCL thiab SDA ntawm lwm sab ntawm pawg thawj coj saib. Tam sim no, peb tau npaj rau code!

Kauj Ruam 2: Txoj Cai: Cov Lus Txhais Ntiaj Teb & Teeb

#suav nrog

MCUFRIEND_kbv tft; // nyuaj-xaim rau UNO cov ntaub thaiv lawm

#suav nrog

#Txhais YP A3

#Txhais XM A2

#define YM 9

#Txhais qhov XP 8

TouchScreen ts = TouchScreen (XP, YP, XM, YM, 300);

#define MINPRESSURE 10

Nov yog qhov pib ntawm txoj cai, qhov uas peb suav nrog cov tsev qiv ntawv (MCUFRIEND_kbv & Touchscreen), txheeb xyuas X thiab Y pins, teeb tsa lub vijtsam kov tsis tau, thiab txiav txim siab yam tsawg kawg nkaus xav tau rau Arduino rau npe tus neeg siv xovxwm.

tus ID;

int neeg siv_selection;

ntab save_number = 0;

lub sij hawm ntab 1;

koj op_num;

ntab tshwm sim;

int cursorLocX = 5;

int cursorLocY = 20;

Txoj cai ua ntej teeb tsa, peb yuav tsum teeb tsa qee qhov kev hloov pauv thoob ntiaj teb. ID pab nrog ua kom lub vijtsam kov nce thiab khiav. user_selection tuav tus lej uas sib haum rau tus yuam sij uas tus neeg siv xaiv thaum nias lub touchscreen. saved_number yog qhov sib txawv uas peb luam tawm rau lub vijtsam tom qab tus neeg siv nkag (ntau ntxiv ntawm qhov no hauv lub voj). Nws yog ntab kom nws tuaj yeem tuav cov lej zauv ntxiv nrog rau cov lej. lub sij hawm 1 yog qhov sib txawv uas thawj tus lej ntawm qhov sib npaug raug khaws tseg tom qab kev xaiv ua haujlwm. op_num txuag cov haujlwm ua tus lej (1 ntxiv rau, 2 rau kev rho tawm, 3 rau kev sib npaug, thiab 4 rau kev faib). qhov tshwm sim yog qhov sib txawv uas tau luam tawm rau lub vijtsam tom qab tus neeg siv tau nias qhov kos npe sib npaug. Nws tseem yog ntab. cursorLocX thiab cursorLocY yog cov lus taw qhia ntawm lub vijtsam kov uas tus cursor tau teeb tsa ntau zaus (nws nyob hauv qhov chaw grey bar nyob rau sab saum toj, txwv tsis pub paub tias yog cov txiaj ntsig teb).

void teeb tsa () {

tft.reset ();

ID = tft.readID ();

tft.begin (ID);

tft.setRotation (0);

tft.fillScreen (TFT_DARKGREY);

plaub fab ();

cov zauv ();

tft.setTextSize (3);

tft.setTextColor (TFT_BLUE, TFT_DARKGREY);

}

Peb teeb tsa ua haujlwm thawj zaug muaj qhov pib ua rau daim iav npog (kab 1-3). Kev taw qhia ntawm daim ntaub thaiv tau teeb tsa siv tft.setRotation () hais kom ua, nrog 0 ua ncaj ncees. Tag nrho lub vijtsam yog xim tsaus nti nrog tft.fillScreen () hais kom ua, uas peb yuav sau saum toj kawg nkaus (tshwj tsis yog rau cov txiaj ntsig teb). Cov xwmfab () thiab cov lej () ua haujlwm kos cov plaub fab ntawm lub laij lej, xim cov plaub fab dub thiab dawb hauv daim phiaj ntsuas, thiab sau tus lej/ua haujlwm ntawm cov plaub fab hauv xiav. Peb yuav mus rau cov uas nyob hauv cov kauj ruam tom ntej. Tft.setTextSize () hais kom ua cov ntawv me me ntawm cov txiaj ntsig teb rau 3, uas yog tus ntawv nruab nrab. Qhov tft.setTextColor () hais kom ua cov ntawv xim ntawm cov txiaj ntsig teb rau xiav, uas tau sau dua qhov tsaus ntuj nti.

Kauj Ruam 3: Txoj Cai: Loop

tsis muaj dab tsi voj () {numberSelect ();

ncua (100);

yog (user_selection == 16) {

;

lwm yam {

yog (user_selection <10) {

saved_number = saved_number * 10 + user_selection;

tft.setCursor (cursorLocX, cursorLocY);

tft.print (saved_number);

} lwm tus yog (user_selection> 10) {

hloov (user_selection) {

rooj plaub 11:

op_num = 1;

tft.setCursor (cursorLocX, cursorLocY);

tft.print ("+");

term1 = saved_number;

save_number = 0;

tawg;

rooj plaub 12:

op_num = 2;

tft.setCursor (cursorLocX, cursorLocY);

tft.print ("-");

term1 = saved_number;

save_number = 0;

tawg;

rooj plaub 13:

op_num = 3;

tft.setCursor (cursorLocX, cursorLocY);

tft.print ("X");

term1 = saved_number;

save_number = 0;

tawg;

rooj plaub 14:

op_num = 4;

tft.setCursor (cursorLocX, cursorLocY);

tft.print ("/");

term1 = saved_number;

save_number = 0;

tawg;

rooj plaub 15:

save_number = 0;

term1 = 0;

op_num = 0;

tft.setCursor (cursorLocX, cursorLocY);

tft.print ("");

tawg;

}

tft.setCursor (cursorLocX, cursorLocY);

Qhov no ntau heev rau zom, yog li kuv yuav piav qhia dab tsi saum toj no. Peb pib los ntawm kev hu xov tooj xaiv () ua haujlwm, uas muab tus lej rau txhua lub xwmfab ntawm lub vijtsam. Thaum ib tus neeg siv nias ib qho ntawm cov xwm txheej no, cov haujlwm ua rau tus neeg siv kev xaiv sib txawv rau tus lej ntawm cov xwm txheej. Thawj yog cov lus hais tsuas yog siv los ntawm lub voj yog tias tau xaiv cov neeg siv raug. Yog tias yog, tom ntej yog nqe lus nug yog tias user_selection muaj tus lej tsawg dua 10 tau txais kev cawmdim hauv nws (tus lej 0-9). Yog tias nws ua tiav, tau txais kev cawm dim los ntawm 10 thiab tus lej hauv user_selection tau ntxiv rau save_number, uas tau luam tawm hauv cov txiaj ntsig teb ntawm lub vijtsam. Yog tias nws tsis yog, tom ntej yog nqe lus nug yog tias user_selection muaj tus lej ntau dua 10 tau txais kev cawmdim rau hauv nws (tus lej ua haujlwm: 11 rau +, 12 rau -, 13 rau X, 14 rau /, thiab 15 rau qhov pom tseeb square). Kev hloov pauv ua haujlwm saib xyuas txhua kis (txiav txim los ntawm user_selection). Qhov sib txawv op_num tau muab tus lej uas sib haum rau cov haujlwm uas tau xaiv (1 rau +, 2 rau -, 3 rau X, thiab 4 rau /). Tus nqi hauv saved_number tau txais kev cawmdim mus rau qhov sib txawv ntawm lub sijhawm 1 kom cov npe sib txawv tau txais kev cawmdim tuaj yeem siv rau qhov thib ib nrab ntawm qhov sib npaug. Lub cim ua haujlwm tau luam tawm ntawm lub vijtsam nrog rau kev tshem tawm txhua tus lej hauv cov txiaj ntsig txiaj ntsig. Tsuas yog qhov tshwj xeeb yog lub vijtsam pom tseeb square, uas rov ua dua txhua qhov kev suav hloov pauv thiab tshem tawm cov txiaj ntsig ntawm txhua yam ntawm nws.

lwm yam {

hloov (op_num) {

rooj plaub 1:

tshwm sim = term1 + saved_number;

tft.setCursor (cursorLocX, cursorLocY);

tft.print (ob npaug (qhov tshwm sim));

tawg;

rooj plaub 2:

result = term1 - saved_number;

tft.setCursor (cursorLocX, cursorLocY);

tft.print (ob npaug (qhov tshwm sim));

tawg;

rooj plaub 3:

tshwm sim = term1 * saved_number;

tft.setCursor (cursorLocX, cursorLocY);

tft.print (ob npaug (qhov tshwm sim));

tawg;

rooj plaub 4:

result = float (term1) / ntab (save_number);

tft.setCursor (cursorLocX, cursorLocY);

tft.print (tshwm sim);

tawg;

}

tft.setCursor (cursorLocX, cursorLocY);

save_number = tshwm sim;

term1 = 0;

op_num = 0;

ncua (1000);

}

}

}

Qhov kawg ntawm lub voj cuam tshuam nrog qhov xwm txheej ntawm tus neeg siv xaiv qhov kos npe sib npaug (user_selection == 10). Lwm txoj haujlwm hloov pauv ua haujlwm dhau los ntawm plaub qhov kev ua lej (txiav txim los ntawm op_num). Cov ntaub ntawv ntxiv (rooj plaub 1) ntxiv cov lus 1 thiab cov ntawv khaws tseg_number ua ke thiab txuag tus lej rau hauv qhov tshwm sim sib txawv. Cov txiaj ntsig tau luam tawm rau cov txiaj ntsig teb ua ob npaug. Qhov rho tawm (rooj plaub 2) rho tawm cov nyiaj khaws tseg los ntawm cov lus 1 thiab khaws tus lej rau hauv cov txiaj ntsig sib txawv. Cov txiaj ntsig tau luam tawm mus rau thaj chaw txiaj ntsig ua ob npaug. Cov ntaub ntawv sib npaug (rooj plaub 3) sib npaug lub sijhawm 1 los ntawm kev khaws tseg_number thiab txuag tus lej rau hauv qhov tshwm sim sib txawv. Cov txiaj ntsig tau luam tawm rau cov txiaj ntsig teb ua ob npaug. Qhov faib cov ntaub ntawv (rooj plaub 4) faib lub sij hawm 1 los ntawm kev cawm_number ua ke thiab txuag tus lej rau hauv qhov tshwm sim sib txawv. Cov txiaj ntsig tau luam tawm rau cov txiaj ntsig teb raws li ntab (vim hais tias faib cov lus teb tuaj yeem yog zauv zauv). Tom qab qhov xwm txheej ntawm ib tus lej, ua haujlwm, lossis cov txiaj ntsig tau luam tawm rau lub vijtsam, tus cursor tau rov pib dua, tau txais kev cawmdim_number tau teeb tsa rau qhov txiaj ntsig dhau los, thiab lub sijhawm 1 & op_num tau rov pib dua.

Ob peb qhov kev ceeb toom: tus neeg siv tsis tuaj yeem sau tus lej zauv rau hauv lub laij lej vim tsis muaj tus lej lej zauv. Tsis tas li, tus neeg siv tsuas tuaj yeem ua ib qho zauv ntawm ib lub sijhawm. Koj tsis tuaj yeem suav qhov txiaj ntsig thiab tom qab ntawd ntxiv/rho tawm/sib npaug/faib qhov txiaj ntsig. Hauv tus lej Select () muaj nuj nqi, muaj lub luag haujlwm tshem tawm lub vijtsam tom qab cov txiaj ntsig tau luam tawm yog tias tus neeg siv tau nias lwm lub xwmfab.

Kauj Ruam 4: Txoj Cai: Squares Function

tsis muaj cov plaub fab () {

// cov plaub dub thiab dawb hloov pauv ntawm txhua kab thiab kab thib ib thiab thib peb muaj cov qauv sib txawv dua li kab thib ob thiab plaub

tft.fillRect (0, 60, 60, 65, TFT_BLACK); // thawj kab ntawm plaub fab pib, dub rau dawb tft.fillRect (60, 60, 60, 65, TFT_WHITE);

tft.fillRect (120, 60, 60, 65, TFT_BLACK);

tft.fillRect (180, 60, 60, 65, TFT_WHITE); // thawj kab ntawm plaub fab xaus

tft.fillRect (0, 125, 60, 65, TFT_WHITE); // kab thib ob ntawm plaub fab pib, dawb rau dub tft.fillRect (60, 125, 60, 65, TFT_BLACK);

tft.fillRect (120, 125, 60, 65, TFT_WHITE);

tft.fillRect (180, 125, 60, 65, TFT_BLACK); // kab thib ob ntawm plaub fab xaus

tft.fillRect (0, 190, 60, 65, TFT_BLACK); // thib peb kab ntawm plaub fab pib, dub rau dawb tft.fillRect (60, 190, 60, 65, TFT_WHITE);

tft.fillRect (120, 190, 60, 65, TFT_BLACK);

tft.fillRect (180, 190, 60, 65, TFT_WHITE); // Peb kab ntawm plaub fab xaus

tft.fillRect (0, 255, 60, 65, TFT_WHITE); // Plaub kab ntawm plaub fab pib, dawb rau dub tft.fillRect (60, 255, 60, 65, TFT_BLACK);

tft.fillRect (120, 255, 60, 65, TFT_WHITE);

tft.fillRect (180, 255, 60, 65, TFT_BLACK); // Plaub kab ntawm plaub fab xaus

}

Cov plaub fab () kev ua haujlwm yog qhov yooj yim zoo nkauj. Tft.fillRect (X1, Y1, X2, Y2, TFT_COLOR) hais kom kos lub duab plaub raws li qhov ntsuas tsis dhau mus rau nws, uas yog x thiab y thawj txoj haujlwm, x thiab y txoj haujlwm thib ob, thiab xim uas lub kaum plaub tau ntim nrog. Cov haujlwm no thim tag nrho plaub kab ntawm cov xwm txheej (cov duab plaub sib dhos) thiab ua kom tiav txhua lub xwmfab nrog cov xim dhau mus rau nws.

Kauj Ruam 5: Txoj Cai: Numbers Function

cov lej tsis muaj dabtsis () {

tft.setTextColor (TFT_BLUE); // teev tus lej/cim xim rau xiav

tft.setTextSize (5); // teev tus lej/tus cim loj rau 5

tft.setCursor (18, 75); // teeb tus cursor rau thawj kab ntawm tus lej/cim

tft.print ("7 8 9 /"); // luam thawj kab ntawm tus lej/cim

tft.setCursor (18, 140); // teeb tus cursor rau kab thib ob ntawm cov lej/cim

tft.print ("4 5 6 X"); // luam kab thib ob ntawm cov lej/cim

tft.setCursor (18, 205); // teeb tus cursor rau kab thib peb ntawm cov lej/cim

tft.print ("1 2 3 -"); // luam kab thib peb ntawm cov lej/cim

tft.setCursor (18, 270); // teeb tus cursor rau kab thib plaub ntawm cov lej/cim

tft.print ("C 0 = +"); // luam kab thib plaub ntawm cov lej/cim

}

Cov lej () kev ua haujlwm kuj tseem yooj yim. Thawj ob kab teeb cov ntawv loj dua thiab xim rau xiav. Tft.setCursor () hais kom ua tus cursor rau txoj haujlwm ntawm txhua kab uas sau cov lej pib los ntawm. Tom qab ntawv tft.print () hais kom luam tus lej/cim hla lub xwmfab.

Kauj Ruam 6: Txoj Cai: NumberSelect Function

void numberSelect () {

TSPoint p = ts.getPoint ();

tus pinMode (XM, OUTPUT);

tus pinMode (YP, OUTPUT);

yog (p.z> MINPRESSURE) {

p.x = daim ntawv qhia (p.x, 250, 845, 0, 239);

p.y = daim ntawv qhia (p.y, 245, 860, 0, 319);

yog (qhov tshwm sim! = 0) {

tau = 0;

save_number = 0;

tft.print ("QHIA VALUES");

ncua (500);

tft.setCursor (cursorLocX, cursorLocY);

tft.print ("");

tft.setCursor (cursorLocX, cursorLocY);

}

Txhawm rau pib tawm tus lej Xaiv () ua haujlwm, peb thov kom cov neeg siv nkag los ntawm lub vijtsam kov nrog ts.getPoint () hais kom ua. Thaum cov ntaub ntawv ntawd tau sau tseg, peb tshawb xyuas seb qhov siab tshaj plaws tau dhau los (lossis, ua lwm yam lus, yog tias tus neeg siv tau nias qhov chaw ntawm lub vijtsam kov). Yog tias nws yog, x thiab y kev sib koom ua ke tau teeb tsa los ntawm Cartesian kev tswj hwm mus rau kev tshuaj xyuas tshwj xeeb ntawm lub vijtsam. (0, 0) yog kaum sab laug tes saum toj kawg nkaus ntawm lub vijtsam kov, nrog x axis hla thiab y axis nqis qis. Tshooj tom ntej tshuaj xyuas seb puas muaj tus lej khaws tseg hauv qhov tshwm sim. Yog tias muaj, cov txiaj ntsig thiab tau txais kev cawmdim tau rov pib dua rau 0. Cov lus "CLEAR VALUES" tau luam tawm ntawm thaj chaw txiaj ntsig, thiab lub vijtsam raug tshem nrog tus cursor rov qab rau nws txoj haujlwm pib.

yog (p.y 60) {// thawj kab ntawm plaub fab

yog (p.x <60)

user_selection = 7;

lwm yog (p.x <120)

user_selection = 8;

lwm yog (p.x <180)

user_selection = 9;

lwm tus user_selection = 14;

} lwm yog (p.y 125) {// kab thib ob ntawm cov xwm txheej

yog (p.x <60)

user_selection = 4;

lwm yog (p.x <120)

user_selection = 5;

lwm yog (p.x <180)

user_selection = 6;

lwm tus user_selection = 13;

} lwm yog (p.y 190) {// kab thib peb ntawm cov xwm txheej

yog (p.x <60)

user_selection = 1;

lwm yog (p.x <120)

user_selection = 2;

lwm yog (p.x <180)

user_selection = 3;

lwm tus user_selection = 12;

} lwm yog (p.y> 255) {// kab plaub ntawm plaub fab

yog (p.x <60)

user_selection = 15;

lwm yog (p.x <120)

user_selection = 0;

lwm yog (p.x <180)

user_selection = 10;

lwm tus user_selection = 11;

}

lwm yam {

user_selection = 16; // user_selection tau teeb tsa rau 16 (tsis muaj qhov sib txawv)

}

}

Qhov no yog ib feem uas txiav txim siab lub pob twg tau raug xaiv. Pib nrog cov kab saum toj kawg nkaus ntawm cov xwm txheej thiab xaus nrog kab hauv qab, Arduino saib rau qhov twg lub vijtsam tau raug nias ntawm. Tom qab ntawv nws muab tus lej ib tus lej thiab khaws tus lej ntawd rau hauv user_selection. Cov lej 0-9 sib npaug rau cov lej sib npaug, cov lej 11-15 sib raug rau cov haujlwm ua haujlwm thiab cov xwm txheej meej, thiab tus lej 10 sib raug rau qhov kos npe sib npaug. Yog tias tsis muaj cov xwm txheej tau raug xaiv, tom qab ntawd user_selection tau teeb tsa rau 16, uas yuav ua rau lub voj rov pib dua (saib lub voj muaj nuj nqi).

Kauj Ruam 7: Txaus Siab Rau Koj Txoj Haujlwm Tiav

Muaj koj muaj nws! Tam sim no koj muaj lub laij lej touchscreen uas tuaj yeem ua ntxiv, rho tawm, sib ntxiv, thiab faib tawm. Txoj haujlwm no hloov pauv tag nrho txoj kev kuv xav tias lub laij lej ua haujlwm. Raws li kuv tau ua haujlwm ntawm txoj haujlwm no, kuv nco qab qhia kuv tus kws qhia hauv chav kawm, "Kuv yuav tsis saib lub laij lej ib yam nkaus!" Cov haujlwm uas koj ua tus siv xav tias yooj yim yog qhov nyuaj me ntsis thaum koj nyob tom qab lub khoos phis tawj sim ua raws koj lub tswv yim. Kuv vam tias koj txaus siab rau txoj haujlwm, thiab kuv vam tias koj lub siab xav li cas lub tshuab xam zauv ua haujlwm tau hloov pauv ib yam nkaus!

Nov yog tag nrho cov cai rau koj yooj yim. Nws tau sau nrog cov lus pom yog li koj muaj teeb meem lawv yuav tsum qhia koj tias txhua kab ua dab tsi.

Pom zoo: