This shows you the differences between two versions of the page.
| et:iot:examples:encoder [2023/01/28 13:38] – tekitatud karllall | et:iot:examples:encoder [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Enkoodri näidis ====== | ||
| + | Enkooder peab olema ühendatud anduri mooduliga. Enkoodril on kaks 3,5 mm pistikut, mis peavad olema ühendatud anduri mooduli 3,5 mm sisendpesadega. Kontrolleri ja anduri moodulid peavad olema omavahel ühendatud. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Pistikud peavad olema õigesti ühendatud: nelja kanaliga sisend (D) pesasse (parempoolne) ning kolme kanaliga sisend (A) pesasse (vasakpoolne). | ||
| + | |||
| + | {{: | ||
| + | {{: | ||
| + | |||
| + | |||
| + | Vajaminevad teegid: | ||
| + | < | ||
| + | |||
| + | Näidiskood prindib välja ja saadab serverile info enkoodri praeguse asendi väärtuse. Väärtus suureneb enkoodri ratast üht pidi pöörates ning väheneb teist pidi pöörates. Lisaks sellele on enkoodri rattal lüliti, mis aktiveerub alla vajutades. Lülitit vajutades saadetakse info lüliti vajutamise kohta jada- ehk serial porti. | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | // Defining pins as need for the encoder | ||
| + | #define ENC_PINA 12 | ||
| + | #define ENC_PINB 13 | ||
| + | #define ENC_BTN | ||
| + | #define ENC_STEPS_PER_NOTCH 4 | ||
| + | ClickEncoder encoder = ClickEncoder(ENC_PINA, | ||
| + | |||
| + | Ticker encTicker; | ||
| + | |||
| + | bool encFlag; | ||
| + | uint16_t button; | ||
| + | |||
| + | // Function started after the connection to the server is established. | ||
| + | void iot_connected() | ||
| + | { | ||
| + | Serial.println(" | ||
| + | iot.log(" | ||
| + | } | ||
| + | |||
| + | void setEncFlag() | ||
| + | { | ||
| + | encFlag=true; | ||
| + | } | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | Serial.begin(115200); | ||
| + | Serial.println(" | ||
| + | |||
| + | // | ||
| + | // | ||
| + | iot.printConfig(); | ||
| + | iot.setup(); | ||
| + | |||
| + | // Activating additional button functions | ||
| + | encoder.setButtonHeldEnabled(true); | ||
| + | encoder.setDoubleClickEnabled(true); | ||
| + | |||
| + | encTicker.attach(1, | ||
| + | } | ||
| + | |||
| + | //Main code, which runs in loop | ||
| + | void loop() | ||
| + | { | ||
| + | iot.handle(); | ||
| + | delay(10); // wait for 0.01 second | ||
| + | static uint32_t lastService = 0; | ||
| + | |||
| + | // Sending message “Button”, | ||
| + | if(analogRead(A0) < 100) | ||
| + | { | ||
| + | Serial.println( " | ||
| + | } | ||
| + | |||
| + | if (lastService + 1000 < micros()) | ||
| + | { | ||
| + | lastService = micros(); | ||
| + | encoder.service(); | ||
| + | } | ||
| + | |||
| + | static int16_t last, value; | ||
| + | value += encoder.getValue(); | ||
| + | |||
| + | // Send encoder reading to serial monitor, if it has changed | ||
| + | if(value != last) | ||
| + | { | ||
| + | last = value; | ||
| + | Serial.print(" | ||
| + | Serial.println(value); | ||
| + | } | ||
| + | // Publishing encoder value in MQTT broker | ||
| + | if(encFlag) | ||
| + | { | ||
| + | encFlag = false; | ||
| + | String msg = String(value); | ||
| + | iot.publishMsg(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||