Cov txheej txheem:

Ua tiav Arduino Rotary Solution: 5 Kauj Ruam
Ua tiav Arduino Rotary Solution: 5 Kauj Ruam

Video: Ua tiav Arduino Rotary Solution: 5 Kauj Ruam

Video: Ua tiav Arduino Rotary Solution: 5 Kauj Ruam
Video: What He's Done (Nws Ua Tiav) feat. Kelley Chang 2024, Kaum ib hlis
Anonim
Ua tiav Arduino Rotary Solution
Ua tiav Arduino Rotary Solution

Cov encoders teb yog tig pob qhov rooj tswj rau cov phiaj xwm hluav taws xob, feem ntau siv nrog Arduino tsev neeg microcontrollers. Lawv tuaj yeem siv los kho qhov ntsuas tsis zoo, txheeb xyuas cov ntawv, txav cov khoom ntawm lub vijtsam, teeb tsa qhov tseem ceeb ntawm txhua yam. Lawv yog cov hloov pauv ntau rau cov potentiometers, vim tias lawv tuaj yeem hloov pauv tau ntau dua thiab tsis muaj qhov kawg, lawv nce lossis txo qis ib tus nqi sib txawv ntawm ib lub sijhawm, thiab feem ntau suav nrog kev hloov pushable rau xaiv cov haujlwm zoo. Lawv tuaj nyob hauv txhua daim duab thiab qhov ntau thiab tsawg, tab sis tus nqi qis tshaj yog qhov nyuaj rau cuam tshuam nrog raws li tau piav qhia hauv qab no.

Muaj ntau ntau cov ntawv hais txog kev ua haujlwm cov ntsiab lus thiab kev siv hom ntawm Rotary encoders, thiab ntau tus lej piv txwv thiab cov tsev qiv ntawv yuav siv li cas. Qhov teeb meem tsuas yog tias tsis muaj ib tus ntawm lawv ua haujlwm 100% raug nrog tus nqi qis tshaj suav suav teb cov qauv.

Kauj Ruam 1: Rotary Encoders Sab Hauv

Rotary Encoders Sab Hauv
Rotary Encoders Sab Hauv
Rotary Encoders Sab Hauv
Rotary Encoders Sab Hauv
Rotary Encoders Sab Hauv
Rotary Encoders Sab Hauv

Ib feem teb ntawm tus encoder muaj peb tus pin (thiab ob ntxiv rau qhov yeem hloov qhov). Ib qho yog ib qho av (dub GND), ob qho ntxiv yog rau kev txiav txim siab thaum lub pob qhov rooj tig (lawv feem ntau hu ua xiav CLK thiab liab DT). Ob qho no tau txuas nrog PULLUP tus lej nkag ntawm lub microcontroller, ua rau qib siab lawv qhov kev nyeem ntawv tsis raug. Thaum lub pob qhov rooj tig mus rau tom ntej (lossis raws lub moos), ua ntej xiav CLK poob rau qib LOW, tom qab ntawd liab DT ua raws. Tig ntxiv, xiav CLK nce rov qab mus rau HIGH, tom qab ntawd zoo li GND thaj ua rau ob daim pins txuas, liab DT kuj nce rov mus rau HIGH. Yog li ua kom tiav ib daim npav FWD tag nrho (lossis raws lub moos). Ib yam mus rau lwm qhov kev taw qhia BWD (lossis rov qab-clockwise), tab sis tam sim no liab liab ua ntej, thiab xiav nce rov qab kawg li tau qhia hauv ob qib duab ib feem.

Kauj Ruam 2: Kev nyuaj siab uas ua rau mob tiag rau ntau tus

Kev nyuaj siab uas ua rau mob tiag rau ntau leej
Kev nyuaj siab uas ua rau mob tiag rau ntau leej
Kev nyuaj siab uas ua rau mob tiag rau ntau leej
Kev nyuaj siab uas ua rau mob tiag rau ntau leej
Kev nyuaj siab uas ua rau mob tiag rau ntau leej
Kev nyuaj siab uas ua rau mob tiag rau ntau leej

Qhov teeb meem tshwm sim rau Arduino hobbyists, qhov pheej yig Rotary encoder modules thaws hloov pauv ntxiv hauv qib tso tawm, ua rau ntxiv thiab yuam kev suav suav nyeem. Qhov no tiv thaiv kev suav tsis tau zoo thiab ua rau nws tsis tuaj yeem ua ke cov qauv no rau hauv cov phiaj xwm raug. Cov kev tawm tsam ntxiv no yog tshwm sim los ntawm kev siv tshuab txav ntawm thaj ua rau thaj pins sib txuas, thiab txawm tias siv cov peev txheej ntxiv tsis tuaj yeem tshem tawm lawv tag. Bounces tuaj yeem tshwm sim nyob txhua qhov chaw hauv cov zuam puv, thiab tau qhia los ntawm cov xwm txheej hauv lub neej tiag tiag ntawm cov duab.

Kauj Ruam 3: Finite State Machine (FSM) Tshuaj

Finite State Machine (FSM) Tshuaj
Finite State Machine (FSM) Tshuaj

Cov duab qhia tag nrho lub xeev qhov chaw ntawm qhov hloov pauv tau rau ob tus pin (xiav CLK thiab liab DT), ob qho tib si rau qhov raug thiab tsis raug. Raws li lub xeev lub tshuab no ua tiav cov kev daws teeb meem tuaj yeem ua haujlwm uas ib txwm ua haujlwm 100% raug. Vim tias tsis muaj kev lim sijhawm qeeb yog qhov tsim nyog hauv qhov kev daws teeb meem no, nws tseem yog qhov ua tau sai tshaj plaws. Lwm qhov txiaj ntsig ntawm kev cais cov pins 'lub xeev qhov chaw los ntawm hom ua haujlwm yog tias ib tus tuaj yeem thov ob qho kev xaiv tsa lossis cuam tshuam cov qauv rau nws tus kheej nyiam. Kev xaiv tsa lossis cuam tshuam tuaj yeem txheeb xyuas qib kev hloov pauv ntawm tus pin thiab kev sib cais ib txwm yuav xam lub xeev tshiab raws li lub xeev tam sim no thiab cov xwm txheej tseeb ntawm qib hloov pauv.

Kauj ruam 4: Arduino Code

Arduino Txoj Cai
Arduino Txoj Cai

Cov cai hauv qab no suav tus FWD thiab BWD zuam ntawm tus lej saib thiab tseem suav nrog qhov kev xaiv hloov ua haujlwm.

// Peter Csurgay 2019-04-10

// Pins ntawm cov teb mapped rau Arduino chaw nres nkoj

#define SW 21 #define CLK 22 #define DT 23

// Tus nqi tam sim no thiab yav dhau los ntawm lub txee ntsuas los ntawm cov teb

int curVal = 0; int prevVal = 0;

// Xya lub xeev ntawm FSM (lub xeev muaj lub tshuab kawg)

#define IDLE_11 0 #define SCLK_01 1 #define SCLK_00 2 #define SCLK_10 3 #define SDT_10 4 #define SDT_00 5 #define SDT_01 6 int xeev = IDLE_11;

void teeb tsa () {

Serial.begin (250000); Serial.println ("Pib …"); // Qib HIGH yuav tsis raug rau txhua tus pin pinMode (SW, INPUT_PULLUP); pinMode (CLK, INPUT_PULLUP); pinMode (DT, INPUT_PULLUP); // Ob qho CLK thiab DT yuav ua rau cuam tshuam rau txhua qib hloov pauv attachInterrupt (digitalPinToInterrupt (CLK), rotaryCLK, CHANGE); attachInterrupt (digitalPinToInterrupt (DT), rotaryDT, CHANGE); }

void voj () {

// Kev ua haujlwm ntawm qhov kev xaiv hloov ua ke rau qee qhov kev hloov pauv hloov pauv yog (digitalRead (SW) == LOW) {Serial.println ("Nias"); thaum (! digitalRead (SW)); } // Txhua qhov kev hloov pauv hauv tus lej suav tau tshwm hauv Serial Monitor yog (curVal! = PrevVal) {Serial.println (curVal); prevVal = curVal; }}

// Lub Xeev Tshuab hloov pauv rau qib CLK hloov pauv

void rotaryCLK () {yog (digitalRead (CLK) == LOW) {yog (xeev == IDLE_11) xeev = SCLK_01; lwm tus yog (xeev == SCLK_10) xeev = SCLK_00; lwm tus yog (xeev == SDT_10) xeev = SDT_00; } lwm {yog (xeev == SCLK_01) xeev = IDLE_11; lwm tus yog (xeev == SCLK_00) xeev = SCLK_10; lwm yog (xeev == SDT_00) xeev = SDT_10; lwm tus yog (xeev == SDT_01) {xeev = IDLE_11; curVal--; }}}

// Lub Xeev Tshuab hloov pauv rau DT qib hloov pauv

void rotaryDT () {yog (digitalRead (DT) == LOW) {yog (xeev == IDLE_11) xeev = SDT_10; lwm tus yog (xeev == SDT_01) xeev = SDT_00; lwm tus yog (xeev == SCLK_01) xeev = SCLK_00; } lwm {yog (xeev == SDT_10) xeev = IDLE_11; lwm tus yog (xeev == SDT_00) xeev = SDT_01; lwm tus yog (xeev == SCLK_00) xeev = SCLK_01; lwm tus yog (xeev == SCLK_10) {xeev = IDLE_11; curVal ++; }}}

Kauj ruam 5: Flawless Integration

Koj tuaj yeem tshawb xyuas hauv cov vis dis aus txuas uas FSM daws tau ua haujlwm raug thiab nrawm txawm tias muaj qhov qis qis qis qis qis nrog ntau yam cuam tshuam cuam tshuam.

Pom zoo: