Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
en:iot:examples:encoder [2018/12/27 12:53]
127.0.0.1 external edit
en:iot:examples:encoder [2019/11/08 14:54] (current)
timothy_r Parandatud Enkoodri kood
Line 12: Line 12:
  
 Needed libraries Needed libraries
-<​code>​lib_deps = ITTIoT, ​Encoder</​code>​+<​code>​lib_deps = ITTIoT@1.0.5ClickEncoder</​code>​
  
 The example code will print out encoder current position value The example code will print out encoder current position value
Line 21: Line 21:
 #include <​Ticker.h>​ #include <​Ticker.h>​
  
-#include <Encoder.h>+#include <ClickEncoder.h>
  
-#​define ​ENC_PIN_1 ​12 +#​define ​ENC_PINA ​12 
-#​define ​ENC_PIN_2 ​13+#​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);​
  
-Encoder myEnc(ENC_PIN_1,​ ENC_PIN_2); 
 Ticker encTicker; Ticker encTicker;
  
-long oldPosition ​ = -999; 
 bool encFlag; bool encFlag;
 +uint16_t button;
 void iot_connected() void iot_connected()
 { {
Line 49: Line 51:
   iot.printConfig();​ // print json config to serial //Peale Serial.begin ja enne iot.setup   iot.printConfig();​ // print json config to serial //Peale Serial.begin ja enne iot.setup
   iot.setup();​   iot.setup();​
 +
 +  encoder.setButtonHeldEnabled(true);​
 +  encoder.setDoubleClickEnabled(true);​
 +
  
   encTicker.attach(1,​ setEncFlag);​   encTicker.attach(1,​ setEncFlag);​
Line 57: Line 63:
   iot.handle();​   iot.handle();​
  
-  // Send encoder reading to the serial port, when it changes +  ​static uint32_t lastService = 0; 
-  ​long newPosition = myEnc.read()+  ​// Button 
-  if (newPosition != oldPosition)+  ​if(analogRead(A0< 100)
   {   {
-    ​oldPosition = newPosition;​ +    Serial.println( "button"); 
-    ​Serial.print("Encoder: ​"); +  } 
-    ​Serial.println(newPosition);+   
 +  if (lastService + 1000 < micros()) 
 +  { 
 +    ​lastService = micros(); 
 +    encoder.service();
   }   }
  
 +  static int16_t last, value;
 +  value += encoder.getValue();​
   // Send encoder reading to server   // Send encoder reading to server
 +
 +  if(value != last)
 +  {
 +    last = value;
 +    Serial.print("​Encoder Value: ");
 +    Serial.println(value);​
 +  }
   if(encFlag)   if(encFlag)
   {   {
       encFlag = false;       encFlag = false;
-      String msg = String(newPosition);+      String msg = String(value);
       iot.publishMsg("​enc",​ msg.c_str());​       iot.publishMsg("​enc",​ msg.c_str());​
   }   }
 } }
 +
  
  
 </​code>​ </​code>​
en/iot/examples/encoder.txt · Last modified: 2019/11/08 14:54 by timothy_r
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