Arduino ukwenza imisebenzi emininzi kunye nolawulo lwexesha

Uvavanyo lweArduino kwimultitask kunye ne-milis

Andililo ingcali yaseArduino, nangona ndinetafile ixesha elide khange ndiphande. Amaxesha endiwasebenzisileyo ibisisixhobo sokukopa kunye nokuncamathisela ikhowudi esele yenziwe kodwa ngaphandle komdla omkhulu ekufundeni ngenene ukuba isebenza njani, kodwa ndinenjongo yokwenza ukuba isebenze kwaye iluncedo kum. Le Krisimesi ndiyilungisile imeko yoKuzalwa kancinane nee-LEDs ezithile kunye ne-HC-SR04 ye-ultrasound sensor. Ndaye ndayeka ukujonga into ekufuneka yenziwe.

Ndifuna ukwenza izinto ezahlukeneyo ngee-LEDs ezimbini ezivela kumqondiso omnye. Yhoo. Ndikhawuleze ndakhubeka kwinto endicinga ukuba iyakuba yiyo enye yezithintelo zokuqala ohlangabezana nazo xa uqala ukungxola ngeArduino. Kwaye awudingi kuyenza ibe nzima kakhulu. Ndithetha nje ngezinye ze-LED, uyaqonda ukuba awukwazi ukwenza le nto uyifunayo ngokuchanekileyo.

Masikucacise kwasekuqaleni ukwenza imisebenzi emininzi akukho eArduino, imisebenzi emibini ayinakwenziwa ngokufana. Kodwa kukho iindlela zokwenza iifowuni ngokukhawuleza kangangokuba zibonakala zisebenza ngaxeshanye.

Ndixelela imeko ngokweenkcukacha. NgeKrisimesi ndimise uMdlalo wokuZalwa kukaYesu kwaye ndifuna ukuba izibane ezithile zoKuzalwa zize xa iintombi zam zisondela. Akukho nto inzima. Ndifuna nje amasebe amabini kwizibane ezikhokelwayo ukuba zisebenze ngokwahlukileyo kumaxabiso enzwa yokusondela.

Ndifuna xa umntu esondela ngaphezu kwe-10 cm

  • Elinye lamasebe ezibane anokuya kwiinkwenkwezi liya kuhlala kwimizuzwana eli-10
  • Elinye elingena ngaphakathi ezindlwini liza kuhlala imizuzwana eli-10 kodwa okoko bahlukanayo nomfanekiso wokuzalwa.

Elula akunjalo? njengoko oku kunokubangela iingxaki ezinkulu. Ngenxa yokuba iArduino ayinakho ukwenza imisebenzi emininzi, yenza umthetho emva komnye.

 

Ukujikeleza kweesekethe ii-LED ezidanyazayo kunye ne-ultrasonic multitasking

Ndenze i Ukunyuka kunye ne-HC-SR04 ye-ultrasonic sensor kunye nee-2 ze-LED, nganye inokuba ilingana nesebe laseBhetelehem. Inxalenye yokuqala ayihlekisi, kuba kukumisela ukuseta kunye nokusebenza kwe-ultrasound sensor kwi-LOOP, kodwa ungayifumana kumawakawaka eendawo. Njengolunye usuku ukuba ndibuza ngaphezulu ndenza okhethekileyo, ngoku nantsi (uxolo ukuba ungabeki iikhredithi, kodwa andikhumbuli ukuba ndiyifumene phi)

Ikhowudi ye-HC-SR04 kwiArduino

int ledPin1 = 8; int ledPin2 = 7; umgama omde; ixesha elide; ixesha langoku elide; ixesha elide; ukuseta okungasebenziyo () {// qala iphini yedijithali i-LED_BUILTIN njengesiphumo. i-pinMode (ledPin1, I-OUTPUT); i-pinMode (ledPin2, OUTPUT); Uthotho. Ukuqala (9600); i-pinMode (3, ISIPHUMO); / * Ukusebenza kwepini 9 njengemveliso: ye-ultrasonic pulse * / pinMode (2, INPUT); / * Ukwenza kusebenze ipini 8 njengegalelo: ixesha lokubhabha kwe-ultrasound * /} // umsebenzi we-loop uqhubeka uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde uphinde ungasebenzi () / * Ngenxa yozinzo lwenzwa * / ukulibazisekaMicroseconds (3); digitalWrite (5, Phezulu); / * ukuthumela ukubetha kwe-ultrasonic * / delayMicroseconds (3); ixesha = ukubethaIn (10, Phezulu); / Umsebenzi wokulinganisa ubude be-pulse engenayo. Imilinganiselo yexesha elidlulileyo phakathi kokuthumela ukubetha kwe-ultrasonic kwaye xa inzwa ifumana i-bounce, oko kukuthi: ukusuka xa i-pin 2 iqala ukufumana i-bounce, HIGH, ide iyeke ukwenza njalo, LOW, ubude be-pulse engenayo * / umgama = int (12 * ixesha); / * ifomula yokubala umgama wokufumana ixabiso elipheleleyo Uthotho.println (umgama); Uthotho.println ("cm"); ukulibaziseka (0.017);

Ngale nto, umgama olinganiswe yi-ultrasound sensor uya kujongwa kwaye agcinwe

Isisombululo sokusebenza kwe-LEDS

Into yokuqala ethi qatha engqondweni ukuqala ukubeka ulibaziseko. Andazi ukuba kutheni, kodwa bonke abaqalayo bacinga ukulibaziseka () kwaye oko kuthintela kakhulu ukhetho kuba ngelixa usebenzisa delay () ibhodi ayiqhubeki isebenza kwaye ngenxa yoko awunakwenza enye into ngelo xesha lokumisa. Isisombululo kukusebenzisa millis ()

Apha ndifumene isisombululo esilula esisekwe kwiifayile zokubala. Njengotitshala wam wayedla ngokutsho, nantoni na inokucwangciswa ezininzi ukuba zilandelelana. Kodwa inyani ayisiyontle kakhulu.

// ukuba umgama ungaphantsi kwe-10, sivula ii-LEDs okanye amasebe amabini kwaye siqale ukubala ixesha ngeemillis () ukuba (umgama <10) {digitalWrite (ledPin1, HIGH); digitalWrite (ledPin2, Phezulu); Ixesha langoku = millis (); } // ukuba umgama mkhulu kuno-10 siza kujonga ixesha elidlulileyo ukusukela oko lavulwa kwaye ukuba likhulu kuneli libonisiweyo siza kucima i-LED1 ukuba (umgama> 10) {timepast = millis () - ixesha langoku ; idijithaliWrite (ledPin1, LOW); // ukuba umgama mkhulu kuno-10000 siya kucima i-LED2 ukuba (ixesha lokuhamba> 10000) {digitalWrite (ledPin2, LOW); }}}

Le khowudi ijonge ukuba iArduino ihlala iqhuba ezi zinto zintathu ukuba izakuhamba ngokukhawuleza kangangokuba kubonakala ngathi yenza izinto ezininzi ngaxeshanye. Kodwa njengoko sele sisazi, iyaqhubeka nokwenza izigwebo nganye nganye.

Nje ukuba umgama ulinganiswe kwaye ugcinwe kuguquguquko umgama, ukuba ii-ifs ziya kuvavanywa:

  • Iitsheki zokuqala ukuba umgama ungaphantsi kwe-10 cm oyifunayo. Ukuba kunjalo, sivula ii-LEDs ezimbini kwaye siqala ukubala ixesha ngeemillis ()
  • kwaye siya kwesesibini ukuba ngaba umgama omkhulu kune-10 cm. Ukuba iyahambelana, sibala ixesha elidlulileyo kwaye singasebenzi i-1 ekhokelweyo exhomekeke kuphela kumgama.
  • Singaya kweyesithathu ukuba apho sijonga khona ukuba ngaphezulu kwemizuzwana eli-10 idlulile okoko kwenziwa i-counter, ukuba kunjalo, cima i-pin 2
  • Kwaye ke iluphu iyaqhubeka. Kwaye kwakhona.

Ekugqibeleni, kwaye nangona isisombululo sisebenza, kwacaca kum ukuba le ngxaki inabantu abaninzi kwaye kufuneka kubekho isisombululo esisesikweni. Ndiqale ukujonga kwaye ndafumana elungileyo (ngokuqinisekileyo ayisiyiyo yodwa) kubafana be I-Adafruit kunye nenkqubo ejolise kwinto. Inezinto zokwenza izinto ezineeklasi ezichaziweyo zokwenza iifowuni ezikhawulezayo kwaye akufuneki ziphindaphinde ikhowudi amaxesha amaninzi kwinkqubo yethu.

Yeyiphi ekugqibeleni "efanayo" njengesisombululo sam ukuba if + zokubala kodwa zinobuhle ngakumbi kwaye zibonelela ngekhowudi efundeka ngakumbi nangakumbi.

Ukulawulwa kwemisebenzi emininzi ngokungathandabuzekiyo kukhokelela kulawulo lwexesha eArduino. Ekuqaleni oku bekungafakwanga kwinqaku kodwa ndicinga ukuba kunomdla kakhulu.

Imisebenzi yeArduino kunye nexesha

Njengoko sele ndiphawule Ndicinga ukuba kukho ukuxhomekeka okukhulu kulibaziseko () , mhlawumbi ngenxa yokuba abantu abaqala ukubona lo msebenzi kuyo yonke imizekelo edla ngokubekwa ukusuka kwibinki kuye nakuphi na ukukhanya kwetrafikhi okanye nakuphi na ukuphathwa kwe-LEDS nokucima.

Ukulibazisa kunengxaki enkulu kakhulu, kwaye oko kukuba xa sibiza ukulibaziseka () ixesha elithile, yonke into iyama. Ibhodi ayifundi nayiphi na inzwa, kwaye ayiqhubeki nokwenza izivakalisi, kwaye ayenzi nto konke konke, linda nje ixesha esikuxelele ukuba lidlule kwaye ngokuqinisekileyo ukuba sifuna ukusebenzisa ibhodi ngaphezu kwento enye. kwangaxeshanye, oku akunakwenzeka.

Kuya kufuneka sijonge iimilisisi () nokulibazisaMicrosecond() Kwaye sikwanazo iimicro() ezibuyisela inani leemicroseconds ukusukela oko yaqala ukwenzeka inkqubo.

Qhwanyaza ngaphandle kokulibazisa

Umzekelo opholileyo wokubona ukuba usebenza njani sebenzisa i-Blink yasentsomini kodwa ngaphandle kokulibazisa. Umahluko ophambili kukuba siyalibala malunga nokulibaziseka () ukusebenza kwaye sisebenzise millis () ukuze sikwazi ukubala ixesha phakathi kokusebenza.

Ukuba ungumntu ongaphumliyo njengathi kwaye ufuna ukusebenzisana ekugcinweni nasekuphuculweni kweprojekthi, unokwenza umnikelo. Yonke imali iya kuthenga iincwadi kunye nezixhobo zokuvavanya kunye nokwenza izifundo

3 izimvo "Arduino ukwenza imisebenzi emininzi kunye nolawulo lwexesha"

Shiya amazwana