This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot:examples:encoder [2017/09/29 12:18] – Somepub | en:iot:examples:encoder [2021/03/05 10:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Encoder ====== | + | ====== Encoder |
- | ==== Theory ==== | + | 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. |
+ | {{: | ||
- | ==== Practical ==== | + | The jackets must be inserted |
- | The encoder sensor device has to be connected | + | |
- | {{: | + | {{: |
+ | {{: | ||
- | The jackets must be inserted to the right input. Shorter input head plug to smaller jack(left) and Longer input head to the larger jack(right). | ||
- | |||
- | {{: | ||
+ | Needed libraries | ||
+ | < | ||
+ | The example code will print out and send to the broker encoder’s current position value. The value increases when rotating in one direction and decreases when rotating in another direction. Additionally, | ||
<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_BTN | ||
+ | #define ENC_STEPS_PER_NOTCH 4 | ||
+ | ClickEncoder encoder = ClickEncoder(ENC_PINA, | ||
- | Encoder myEnc(ENC_PIN_1, | ||
Ticker encTicker; | Ticker encTicker; | ||
- | long oldPosition | ||
bool encFlag; | bool encFlag; | ||
+ | uint16_t button; | ||
+ | // Function started after the connection to the server is established. | ||
void iot_connected() | void iot_connected() | ||
{ | { | ||
Line 45: | Line 51: | ||
void setup() | void setup() | ||
{ | { | ||
- | Serial.begin(115200); | + | Serial.begin(115200); |
Serial.println(" | Serial.println(" | ||
- | | + | |
- | | + | // |
+ | // | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | encoder.setButtonHeldEnabled(true); | ||
+ | encoder.setDoubleClickEnabled(true); | ||
encTicker.attach(1, | encTicker.attach(1, | ||
} | } | ||
+ | //Main code, which runs in loop | ||
void loop() | void loop() | ||
{ | { | ||
- | iot.handle(); | + | iot.handle(); // IoT behind the plan work, it should be periodically called |
+ | delay(10); // wait for 0.01 second | ||
+ | static uint32_t lastService = 0; | ||
- | // Send encoder reading to the serial port, when it changes | + | // Sending message “Button”, when the analogue value is bigger less then 100 |
- | | + | |
- | if (newPosition != oldPosition) | + | |
{ | { | ||
- | | + | Serial.println( "button"); |
- | | + | |
- | Serial.println(newPosition); | + | |
} | } | ||
- | // Send encoder reading to server | + | |
+ | { | ||
+ | lastService = micros(); | ||
+ | encoder.service(); | ||
+ | } | ||
+ | |||
+ | static int16_t last, value; | ||
+ | value += encoder.getValue(); | ||
+ | |||
+ | | ||
+ | if(value != last) | ||
+ | { | ||
+ | last = value; | ||
+ | Serial.print(" | ||
+ | Serial.println(value); | ||
+ | } | ||
+ | // Publishing encoder value in MQTT broker | ||
if(encFlag) | if(encFlag) | ||
{ | { | ||
encFlag = false; | encFlag = false; | ||
- | String msg = String(newPosition); | + | String msg = String(value); |
iot.publishMsg(" | iot.publishMsg(" | ||
} | } | ||
} | } | ||
- | |||
</ | </ |