KY40 Encoder Reads

A part of my KY40 SW and encoder read sketch,, I'm unable to use the KY40 encoder reads outside void loop.
I think it's due to the volatile byte type assigned to encoderPos that is used as part of the KY40 Interrupt routine.

Code: [Select]

volatile byte encoderPos = 0

If I try to use encoderPos (after the KY40 knob is turned) in a function outside the void loop, encoderPos is always !, e.g. .....

Code: [Select]

void INCHCENTIMETER () {
Serial.print("This is INCHCENTIMETER)");
delay(5000);
a=encoderPos;  
b=encoderPos*2.54; 
c=encoderPos/2.54;

After a KY40 encoderPos read in void loop, I tried this (still in void loop) VAL=encoderPos, where VAL type is INT or else FLOAT.
Either type assignment works OK within void loop, but when accessed in functions outside void loop (e.g. INCHCENTIMETER) , VAL always = 1.

Code: [Select]

void INCHCENTIMETER () {
Serial.print("This is INCHCENTIMETER)");
delay(5000);
a=VAL;  
b=VAL*2.54; 
c=VAL/2.54;

Is there any way to maintain the actual encoder read outside void loop ???

Help is always appreciated.

Hello, two tips:

  1. Why are you showing only small fragments of your code?
  2. A "delay (5000)" within an interrupt function is a very bad idea.
  1. The full code is nearly 250 lines.
  2. The 5000 delay is for debug only so I can see results on the serial monitor.

My sketch converts many units (e.g. feet to meters) and is menu driven. I'm using a KY40 encoder to enter quantities (val), together with the encoder's switch to perform the 'ENTER' function after each encoder position is selected.

I think the problem is as a C++ newbie, that you can't do math when the variable types are different e.g. BYTE and Float. I just don't know how to solve that problem.

Dunno if the Forum will allow it, but my code follows below.

/*******Interrupt-based Rotary Encoder Sketch*******
  by Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt, Steve Spence
*/
int SW=4;;
int KYSW;
//int val;//quantity to convert
byte CONGROUP;//Conversion Group Select
byte UNITS;//Length conversion units

float val;//the quantity to be converted
float a;//a, b, c are dummy VARIABLES between PRNT calls and PRNT function.
float b;
float c;
String x;//x and y are dummy STRINGS between PRNT calls and PRNT function. 
String y;
//LENGTH
String d = " ft";
String e = " in";
String f = " cm";
String g = " mm";
String h = " yds";

//AREA
String l = " sqft";
String m = " acre";
String n = " sqmtr";
String o = " sqyd";
String p = " sqkmtr";
String q = " sqmi";
String ah= " sqmm";
String ai= " sqcm";

//VOLUME
String i = " oz";
String j = " pints";
String k = " qts";
String r = " ltr";
String t = " cuft";String s = " gall";
String u = " cuyds";
String v = " cumtr";

//WEIGHT
String w = " lb";
String z = " ton";
String aa = " gm";
String ab = " kgm";

 //SPEED
String ac = "  MpH";
String ad = "  KmtrpHr";
String ae = "  FtpSec";
String af = "  YdpSec";
String ag = "  MtrpSec";
static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
static int pinB = 3; // Our second hardware interrupt pin is digital pin 3

volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile byte encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent

void setup() {
  pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(SW, INPUT_PULLUP); // set pinSW as an input,pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  attachInterrupt(0, PinA, RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  attachInterrupt(1, PinB, RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  Serial.begin(115200); // start the serial monitor link
}

void PinA() {
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0b00001100; // read all eight pin values then strip away all but pinA and pinB's values
  if (reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos --; //decrement the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

void PinB() {
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0b00001100; //read all eight pin values then strip away all but pinA and pinB's values
  if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos ++; //increment the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

void loop() {
  if (oldEncPos != encoderPos) {
    oldEncPos = encoderPos;
  }
  delay (100);//debug delay
  Serial.println();
  Serial.println F(("ENTER QUANTITY TO CONVERT?"));
  delay(100);//debug delay......

KYSW=HIGH;//initialize KYSW
while(KYSW == HIGH){
KYSW=digitalRead (SW); 
delay(50);//debounce interval
//Serial.print (KYSW);
//Serial.print("quantity = ");
Serial.print(encoderPos);
Serial.println();
val = encoderPos; //= the quantity to convert
}


Serial.println  F(("Select Conversion Group:  1=LENGTH 2=AREA 3=VOLUME 4=WEIGHT 5=SPEED"));
delay(50);
KYSW=HIGH;//initialize KYSW
while(KYSW == HIGH){
KYSW=digitalRead (SW); 
delay(50);//debounce interval
//Serial.print (KYSW);
//SErial.print ("Conversion Group:);
Serial.print(encoderPos);
Serial.println();
CONGROUP = encoderPos;
}
  switch (CONGROUP) {
    case 1:
      LENGTH();
      break;
    case 2:
      AREA();
      break;
    case 3:
      VOLUME();
      break;
    case 4:
      WEIGHT();
      break;
    case 5:
      SPEED();
      break;
  }
}// end mainloop here.

void LENGTH() { //begin LENGTH Function
Serial.println F(("Select Length Conversion Units:  1=in-cm 2=in-mm 3=in-ft 4=in-yd"));
delay(50);
KYSW=HIGH;//initialize KYSW
while(KYSW == HIGH){
KYSW=digitalRead (SW); 
delay(50);//debounce interval
//Serial.print (KYSW);
//SErial.print ("Conversion Group:);
Serial.print(encoderPos);
Serial.println();
UNITS = encoderPos;
}


switch(UNITS){ // Select Length Conversion Units
    case 1:
      INCHCENTIMETER();
      break;
    case 2:
      INCHMILLIMETER();
      break;
    case 3:
      INCHFEET();
      break;
    case 4:
      INCHYARD();
      break;
}
}     

void INCHCENTIMETER () {
Serial.print("This is INCHCENTIMETER)");
delay(5000);
a=encoderPos;  
b=encoderPos*2.54; 
c=encoderPos/2.54;
x=e;//capture inches
y=f;//capture centietera
PRNT (a,b,c,x,y);
}
void INCHMILLIMETER () {
a=val;
b=val*25.4; 
c=val/25.4;
x=e;
y=g;
//PRNT (a,b,c,x,y);
}
void INCHFEET (){
a=val;
b=val/12;
c=val*12;
x=e;//capture inches
y=d;//capture feet
//PRNT (a,b,c,x,y);
}
void INCHYARD (){
a=val;
b=val/36;
c=val*36;
x=e;//capture inches
y=h;//capture yards
//PRNT (a,b,c,x,y);
}

void AREA() {
  Serial.print ("This is the AREA function");
}

void VOLUME() {
  Serial.print ("This is the VOLUME function");
}

void WEIGHT() {
  Serial.print ("This is the WEIGHT function");
}

void SPEED() {
  Serial.print ("This is the SPEED function");
}


//COMMON PRINT FUNCTION. The commas after the variables a,b and c declare the number of print decimal places e.g. a.ddd
void PRNT (float a,float b,float c, String x , String y){
delay(500);
Serial.print (a,3);
Serial.print (x);
Serial.print F((" = "));
Serial.print (b,3);
Serial.println (y);
Serial.print (a,3);
Serial.print (y);
Serial.print F((" = "));
Serial.print (c,3);
Serial.println (x);
delay(5000);//remove this later
}

user gfvalvo has written a library that is very easy to use and the detection-principle this code uses is the most reliable one. KY40 encoders have mechanical switches which do bouncing. But this is handled absolute reliably by this library
called NewEconder

take a look into this demo-code to see how easy it is to use.
If you have any questions about the demo-code just ask them
I will create a more commented version of this demo-code

/* SingleEncoder.cpp
   Created on: Jul 25, 2018
   Author: GFV  see https://github.com/gfvalvo/NewEncoder
*/
#include "Arduino.h"
#include "NewEncoder.h"

// Pins 2 and 3 should work for many processors, including Uno. See README for meaning of constructor arguments.
// Use FULL_PULSE for encoders that produce one complete quadrature pulse per detnet, such as: https://www.adafruit.com/product/377
// Use HALF_PULSE for endoders that produce one complete quadrature pulse for every two detents, such as: https://www.adafruit.com/product/377
// NewEncoder encoder(IO_Pin_A, IO_Pin_B, MinValue, MaxValue, InitialValue, HALF_PULSE); //HALF_PULSE FULL_PULSE

NewEncoder encoder(2, 3, -32000, 32000, 0, FULL_PULSE);

int16_t prevEncoderValue;

void setup() {
  int16_t value;

  Serial.begin(115200);
  //delay(2000);
  Serial.println(F("Starting"));
  if (!encoder.begin()) {

    Serial.println(F("Encoder Failed to Start. Check pin assignments and available interrupts. Aborting."));
    while (1) {
    }
  } else {
    value = encoder;
    Serial.print(F("Encoder Successfully Started at value = "));
    Serial.println(value);
  }
}

void loop() {
  int16_t currentValue;
  bool up, down;

  up = encoder.upClick();
  down = encoder.downClick();
  if (up || down) {
    currentValue = encoder;
    if (currentValue != prevEncoderValue) {
      Serial.print(F("Encoder: "));
      Serial.println(currentValue);
      prevEncoderValue = currentValue;
    } else if (up) {
      Serial.println(F("Encoder at upper limit."));
    } else {
      Serial.println(F("Encoder at lower limit."));
    }
  }
}

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.