Hello everyone,
sorry for the late reply it was nighttime.
I have tried to fix the issue with the software and different code, I am still using interrupts but this time in kind of a different way. I thought about this after reading @kellygray post number19, searched on the internet and found a code which I slightly modified. this isn't exactly the same as @kellygray , being a beginner i was not able to understand properly what he meant but did understand to some extent.
So i used the following code to generate outputs each time the switch was rotated.
volatile boolean interruptdetected;
int pinA=2; //pin CLK
int pinB=3; //pin DT
void isr ()
{interruptdetected = true;}
void setup () {
Serial.begin(74880);
attachInterrupt (digitalPinToInterrupt(pinA), isr, CHANGE);
}
void loop () {
if (interruptdetected) {
interruptdetected=false;
Serial.print ("CLK, pinA: ");
Serial.println (digitalRead(pinA));
Serial.print ("DT, pinB: ");
Serial.println (digitalRead(pinB));
Serial.println();
delay (5);
}
}
After this i turned the rotary encoder both clockwise and counterclockwise and noted down the outputs on a paper. Then i got the following code from Brainy-Bits, and modified it slightly to work with ISR.
const int PinSW=8; // Switch
const int PinCLK=2; // PinA
const int PinDT=3; // PinB
// Variables to debounce Rotary Encoder
long TimeOfLastDebounce = 0;
int DelayofDebounce = 0.01;
//variables for previous Pins state
int PreviousCLK;
int PreviousDATA;
int displaycounter=0; // Store current counter value
volatile bool state=false;
//ISR function
void isr (){state = true;}
void setup() {Serial.begin(74880);
// Read current states
PreviousCLK=digitalRead(PinCLK);
PreviousDATA=digitalRead(PinDT);
pinMode(PinSW, INPUT_PULLUP);
Serial.print(displaycounter);
attachInterrupt (digitalPinToInterrupt(PinCLK), isr, CHANGE);
attachInterrupt (digitalPinToInterrupt(PinDT), isr, CHANGE);
}
void loop() {
// If enough time has passed check the rotary encoder
if (state) {
if ((millis() - TimeOfLastDebounce) > DelayofDebounce) {
check_rotary(); // Rotary Encoder check routine below
PreviousCLK=digitalRead(PinCLK);
PreviousDATA=digitalRead(PinDT);
TimeOfLastDebounce=millis(); // Set variable to current millis() timer
}
// CReset counter if switched is pressed
if (digitalRead(PinSW) == LOW) {
displaycounter=0; // Reset counter to zero
Serial.print(displaycounter);
while (!digitalRead(PinSW)){}
}
}
}
////////////////////////////////////
// Check if Rotary Encoder was moved
////////////////////////////////////
void check_rotary() {
state=false;
if ((PreviousCLK == 0) && (PreviousDATA == 1)) {
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
displaycounter++;
Serial.println(displaycounter);
}
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
displaycounter--;
Serial.println(displaycounter);
}
}
if ((PreviousCLK == 1) && (PreviousDATA == 0)) {
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
displaycounter++;
Serial.println(displaycounter);
}
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
displaycounter--;
Serial.println(displaycounter);
}
}
if ((PreviousCLK == 1) && (PreviousDATA == 1)) {
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
displaycounter++;
Serial.println(displaycounter);
}
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
displaycounter--;
Serial.println(displaycounter);
}
}
if ((PreviousCLK == 0) && (PreviousDATA == 0)) {
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
displaycounter++;
Serial.println(displaycounter);
}
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
displaycounter--;
Serial.println(displaycounter);
}
}
}
The output was pretty good compared to the previous one, and best of all I was able to use interrupts.
const int PinSW=8; // Switch
const int PinCLK=2; // PinA
const int PinDT=3; // PinB
// Variables to debounce Rotary Encoder
long TimeOfLastDebounce = 0;
int DelayofDebounce = 0.01;
//variables for previous Pins state
int PreviousCLK;
int PreviousDATA;
int displaycounter=50; // Store current counter value
volatile bool state=false;
//ISR function
void isr (){state = true;}
void setup() {Serial.begin(74880);
// Read current states
PreviousCLK=digitalRead(PinCLK);
PreviousDATA=digitalRead(PinDT);
pinMode(PinSW, INPUT_PULLUP);
Serial.println(displaycounter);
attachInterrupt (digitalPinToInterrupt(PinCLK), isr, CHANGE);
attachInterrupt (digitalPinToInterrupt(PinDT), isr, CHANGE);
}
void loop() {
// If enough time has passed check the rotary encoder
if (state) {
if ((millis() - TimeOfLastDebounce) > DelayofDebounce) {
check_rotary(); // Rotary Encoder check routine below
PreviousCLK=digitalRead(PinCLK);
PreviousDATA=digitalRead(PinDT);
TimeOfLastDebounce=millis(); // Set variable to current millis() timer
}
}
}
////////////////////////////////////
// Check if Rotary Encoder was moved
////////////////////////////////////
void check_rotary() {
state=false;
if ((PreviousCLK == 0) && (PreviousDATA == 1)) {
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
displaycounter++;
if (displaycounter>56){displaycounter=50;}
else if (displaycounter<50){displaycounter=56;}
Serial.print("Count : ");
Serial.println(displaycounter);
}
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
displaycounter--;
if (displaycounter>56){displaycounter=50;}
else if (displaycounter<50){displaycounter=56;}
Serial.print("Count : ");
Serial.println(displaycounter);
}
}
if ((PreviousCLK == 1) && (PreviousDATA == 0)) {
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
displaycounter++;
if (displaycounter>56){displaycounter=50;}
else if (displaycounter<50){displaycounter=56;}
Serial.print("Count : ");
Serial.println(displaycounter);
}
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
displaycounter--;
if (displaycounter>56){displaycounter=50;}
else if (displaycounter<50){displaycounter=56;}
Serial.print("Count : ");
Serial.println(displaycounter);
}
}
if ((PreviousCLK == 1) && (PreviousDATA == 1)) {
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
displaycounter++;
if (displaycounter>56){displaycounter=50;}
else if (displaycounter<50){displaycounter=56;}
Serial.print("Count : ");
Serial.println(displaycounter);
}
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
displaycounter--;
if (displaycounter>56){displaycounter=50;}
else if (displaycounter<50){displaycounter=56;}
Serial.print("Count : ");
Serial.println(displaycounter);
}
}
if ((PreviousCLK == 0) && (PreviousDATA == 0)) {
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
displaycounter++;
if (displaycounter>56){displaycounter=50;}
else if (displaycounter<50){displaycounter=56;}
Serial.print("Count : ");
Serial.println(displaycounter);
}
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
displaycounter--;
if (displaycounter>56){displaycounter=50;}
else if (displaycounter<50){displaycounter=56;}
Serial.print("Count : ");
Serial.println(displaycounter);
}
}
}
And here is the output:
Count : 50
Count : 51
Count : 52
Count : 53
Count : 54
Count : 55
Count : 56
Count : 50
Count : 51
Count : 52
Count : 53
Count : 54
Count : 55
Count : 56
Count : 50
Count : 51
There are some bounces sometimes, rarely, at least for my project it's not giving any currently, it has bounced two times, however.
I'm pretty sure that the code could be shortened using arrays or in other ways I might not know.
But according to my limited but increasing knowledge this is working for now.
Thank you everyone for taking your precious time.
Any further advice is always appreciated.