This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot:examples:encoder [2020/04/13 09:10] – heikopikner | en:iot:examples:encoder [2021/03/05 10:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Encoder example ====== | ====== Encoder example ====== | ||
| - | The encoder sensor device has to be connected to the sensor module. Encoder sensor device has a two 3,5 mm plugs what has to be connected to the sensor module 3,5 mm input jackets. The controller and sensor modules must be connected. | + | The encoder sensor device has to be connected to the sensor module. Encoder sensor device has a two 3,5 mm plugs what has to be connected to the sensor module 3,5 mm input jackets. The controller and sensor modules must be connected. |
| - | </fc> | + | |
| {{: | {{: | ||
| Line 13: | Line 12: | ||
| Needed libraries | Needed libraries | ||
| - | < | + | < |
| - | The example code will print out encoder current position value | + | The example code will print out and send to the broker |
| - | <code c> | + | |
| + | <code c> | ||
| #include < | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | |||
| #include < | #include < | ||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | // Defining pins as need for the encoder | ||
| #define ENC_PINA 12 | #define ENC_PINA 12 | ||
| #define ENC_PINB 13 | #define ENC_PINB 13 | ||
| #define ENC_BTN | #define ENC_BTN | ||
| #define ENC_STEPS_PER_NOTCH 4 | #define ENC_STEPS_PER_NOTCH 4 | ||
| - | |||
| ClickEncoder encoder = ClickEncoder(ENC_PINA, | ClickEncoder encoder = ClickEncoder(ENC_PINA, | ||
| Line 35: | Line 36: | ||
| bool encFlag; | bool encFlag; | ||
| uint16_t button; | uint16_t button; | ||
| + | |||
| + | // Function started after the connection to the server is established. | ||
| void iot_connected() | void iot_connected() | ||
| { | { | ||
| Line 48: | Line 51: | ||
| void setup() | void setup() | ||
| { | { | ||
| - | Serial.begin(115200); | + | Serial.begin(115200); |
| Serial.println(" | Serial.println(" | ||
| - | iot.printConfig(); | ||
| - | iot.setup(); | ||
| + | // | ||
| + | // | ||
| + | iot.printConfig(); | ||
| + | iot.setup(); | ||
| + | |||
| + | // Activating additional button functions | ||
| encoder.setButtonHeldEnabled(true); | encoder.setButtonHeldEnabled(true); | ||
| encoder.setDoubleClickEnabled(true); | encoder.setDoubleClickEnabled(true); | ||
| - | |||
| encTicker.attach(1, | encTicker.attach(1, | ||
| } | } | ||
| + | //Main code, which runs in loop | ||
| void loop() | void loop() | ||
| { | { | ||
| - | iot.handle(); | + | iot.handle(); |
| - | delay(10); | + | delay(10); |
| static uint32_t lastService = 0; | static uint32_t lastService = 0; | ||
| - | | + | |
| + | | ||
| if(analogRead(A0) < 100) | if(analogRead(A0) < 100) | ||
| { | { | ||
| Serial.println( " | Serial.println( " | ||
| } | } | ||
| - | | + | |
| if (lastService + 1000 < micros()) | if (lastService + 1000 < micros()) | ||
| { | { | ||
| Line 79: | Line 87: | ||
| static int16_t last, value; | static int16_t last, value; | ||
| value += encoder.getValue(); | value += encoder.getValue(); | ||
| - | // Send encoder reading to server | ||
| + | // Send encoder reading to serial monitor, if it has changed | ||
| if(value != last) | if(value != last) | ||
| { | { | ||
| Line 87: | Line 95: | ||
| Serial.println(value); | Serial.println(value); | ||
| } | } | ||
| + | // Publishing encoder value in MQTT broker | ||
| if(encFlag) | if(encFlag) | ||
| { | { | ||
| Line 94: | Line 103: | ||
| } | } | ||
| } | } | ||
| - | |||
| - | |||
| </ | </ | ||