When I rotate the encoder clockwise to set hours or minutes or seconds I expect to get 1-2-3-4-5 and so but I get 0-0-1-3-2-3-4-5-4-6-8-7 and when I rotate it in the reverse direction e.g. starting from 10 I expect to get 10-9-8-7-6 and so but I get 10-10-9-7-8-9-6-4-5-5-4. My code is attached.
I tried changing the encoder and installing two 10nF-100nF capacitors between LK and GROUND and between DT and GROUND but no avail.
When I rotate the encoder clockwise to set hours or minutes or seconds I expect to get 1-2-3-4-5 and so but I get 0-0-1-3-2-3-4-5-4-6-8-7 and when I rotate it in the reverse direction e.g. starting from 10 I expect to get 10-9-8-7-6 and so but I get 10-10-9-7-8-9-6-4-5-5-4. My code is attached.
I tried changing the encoder and installing two 10nF-100nF capacitors between LK and GROUND and between DT and GROUND but no avail.
Can you please help me with my code?
Any help appreciated. Thanks.
#define ClockPin 2 // Must be pin 2
#define DataPin 3 // Must be pin 3
#define readA bitRead(PIND,2)//faster than digitalRead() (((value) >> (bit)) & 0x01)
#define readB bitRead(PIND,3)//faster than digitalRead() (((value) >> (bit)) & 0x01)
volatile long count = 0;
long lastCtr;
unsigned long SpareCycles;
void Encoder(bool A) {
(readB == A) ? count++ : count--;
}
void setup() {
Serial.begin(115200); //115200
pinMode(ClockPin, INPUT);
pinMode(DataPin, INPUT);
/* 1 Step */
attachInterrupt(digitalPinToInterrupt(ClockPin), [] {Encoder( true);}, RISING);
/**/
/* 2 step count
attachInterrupt(digitalPinToInterrupt(ClockPin), [] {Encoder( readB);}, CHANGE);
*/
/* Full 4 Step Count
attachInterrupt(digitalPinToInterrupt(ClockPin), [] {Encoder( readB);}, CHANGE);
attachInterrupt(digitalPinToInterrupt(DataPin ), [] {Encoder( !readA);}, CHANGE);
*/
}
void loop() {
long Counter;
SpareCycles++;
noInterrupts ();
Counter = count;
interrupts ();
if ((lastCtr != Counter) & (SpareCycles > 10)) {
Serial.println(Counter);
SpareCycles = 0;
}
lastCtr = Counter;
}
note that #define ClockPin 2 // Must be pin 2 #define DataPin 3 // Must be pin 3
PaulS and zhomeslice ... thank you very much for your quick response, help and time. I am sorry I forgot to mention that I am very new to Arduino and programming but I have some experience in electronics and plan to attach the timer to PCB UV box. In fact I do not know where I put the codes you suggested.
PaulS and zhomeslice ... thank you very much for your quick response, help and time. I am sorry I forgot to mention that I am very new to Arduino and programming but I have some experience in electronics and plan to attach the timer to PCB UV box. In fact I do not know where I put the codes you suggested.
Thank you again for your effort.
does my code by itself solve your stepping skipping problem
Sketch uses 2284 bytes (7%) of program storage space. Maximum is 30720 bytes.
Global variables use 204 bytes (9%) of dynamic memory, leaving 1844 bytes for local variables. Maximum is 2048 bytes.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0xa8
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0xa8
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00
Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.
This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.