This is an old revision of the document!


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. Do not use this example to configure the IoT module.

The jackets must be inserted to the right input. Four channel input to D jacket (right) and three channel input to the A jacket(left).

Needed libraries

lib_deps = ITTIoT@1.0.5, ClickEncoder

The example code will print out encoder current position value

#include <Arduino.h>
#include <ittiot.h>
#include <Ticker.h>
 
#include <ClickEncoder.h>
 
#define ENC_PINA 12
#define ENC_PINB 13
#define ENC_BTN   0
#define ENC_STEPS_PER_NOTCH 4
 
ClickEncoder encoder = ClickEncoder(ENC_PINA, ENC_PINB, ENC_BTN, ENC_STEPS_PER_NOTCH);
 
Ticker encTicker;
 
bool encFlag;
uint16_t button;
void iot_connected()
{
  Serial.println("MQTT connected callback");
  iot.log("IoT encoder example!");
}
 
void setEncFlag()
{
    encFlag=true;
}
 
void setup()
{
  Serial.begin(115200);
  Serial.println("Booting");
  iot.printConfig(); // print json config to serial //Peale Serial.begin ja enne iot.setup
  iot.setup();
 
  encoder.setButtonHeldEnabled(true);
  encoder.setDoubleClickEnabled(true);
 
 
  encTicker.attach(1, setEncFlag);
}
 
void loop()
{
  iot.handle();
  delay(10);
  static uint32_t lastService = 0;
  // Button
  if(analogRead(A0) < 100)
  {
    Serial.println( "button");
  }
 
  if (lastService + 1000 < micros())
  {
    lastService = micros();
    encoder.service();
  }
 
  static int16_t last, value;
  value += encoder.getValue();
  // Send encoder reading to server
 
  if(value != last)
  {
    last = value;
    Serial.print("Encoder Value: ");
    Serial.println(value);
  }
  if(encFlag)
  {
      encFlag = false;
      String msg = String(value);
      iot.publishMsg("enc", msg.c_str());
  }
}
en/iot/examples/encoder.1595244384.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0