Afternoon all. I am very new to programming and struggling with a task. I have a rotary encoder and i want it to display information at set point. The rotary code i got off the internet. Between the readings:
-1, 0, 1 = countRA + countRM + countRF + countLA + countLM + countLF
2, 3=count RF
4, 5=count RM
6, 7=count RA
-2, -3=count LF
-4, -5=count LM
-6, -7=count LA
Any help would be appreciated.
Many thanks
Kieran
enum PinAssignments {
encoderPinA = 2, // right (labeled DT on our decoder, yellow wire)
encoderPinB = 3, // left (labeled CLK on our decoder, green wire)
clearButton = 8 // switch (labeled SW on our decoder, orange wire)
// connect the +5v and gnd appropriately
};
volatile unsigned int encoderPos = 0; // a counter for the dial
unsigned int lastReportedPos = 1; // change management
static boolean rotating=false; // debounce management
// interrupt service routine vars
boolean A_set = false;
boolean B_set = false;
int countRA = 0; //Right Aft fuel qty
int countRM = 11; //Right Main fuel qty
int countRF = 22; //Right Fwd fuel qty
int countLA = 33; //Left Aft fuel qty
int countLM = 44; //Left Main fuel qty
int countLF = 55; //Left Fwd fuel qty
void setup() {
pinMode(encoderPinA, INPUT_PULLUP); // new method of enabling pullups
pinMode(encoderPinB, INPUT_PULLUP);
pinMode(clearButton, INPUT_PULLUP);
// turn on pullup resistors (old method)
//digitalWrite(encoderPinA, HIGH);
// digitalWrite(encoderPinB, HIGH);
// digitalWrite(clearButton, HIGH);
// encoder pin on interrupt 0 (pin 2)
attachInterrupt(0, doEncoderA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, doEncoderB, CHANGE);
Serial.begin(9600); // output
}
// main loop, work is done by interrupt service routines, this one only prints stuff
void loop() {
rotating = true; // reset the debouncer
if (lastReportedPos != encoderPos) {
Serial.print("Index:");
if (encoderPos == 0, -1, 1);
Serial.println(countRA + countRM + countRF + countLA + countLM + countLF);
if (encoderPos == 2, 3);
Serial.println(countRF);
if (encoderPos == 4, 5);
Serial.println(countRM);
if (encoderPos == 6, 7);
Serial.println(countRA);
if (encoderPos == -2, -3);
Serial.println(countLF);
if (encoderPos == -4, -5);
Serial.println(countLM);
if (encoderPos == -6, -7);
Serial.println(countLA);
lastReportedPos = encoderPos;
}
if (digitalRead(clearButton) == LOW ) {
encoderPos = 0;
}
}
// Interrupt on A changing state
void doEncoderA(){
// debounce
if ( rotating ) delay (1); // wait a little until the bouncing is done
// Test transition, did things really change?
if( digitalRead(encoderPinA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set )
encoderPos += 1;
if(encoderPos > 19)
encoderPos = 19;
rotating = false; // no more debouncing until loop() hits again
}
}
// Interrupt on B changing state, same as A above
void doEncoderB(){
if ( rotating ) delay (1);
if( digitalRead(encoderPinB) != B_set ) {
B_set = !B_set;
// adjust counter - 1 if B leads A
if( B_set && !A_set )
encoderPos -= 1;
if(encoderPos > -19)
encoderPos = -19;
rotating = false;
}
}