- The full code is nearly 250 lines.
- 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
}