I'm following suggestions by cr0sh:
First - clean up your code and narrow the function of the code down to -just- showing a count based on the sensor input. Turn the wheel by hand or via a battery. Basically doing this will rule out anything else that might be causing issues.
I remove all other code from my sketch:
#include "GeduinoPINS.h"
unsigned volatile int guidoEncACount = 0;
unsigned volatile int guidoEncBCount = 0;
void setup() {
Serial.begin(9600);
attachInterrupt(GUIDO_ENCA, guidoEncA, RISING);
attachInterrupt(GUIDO_ENCB, guidoEncB, RISING);
}
void guidoEncA() {
guidoEncACount = guidoEncACount + 1;
}
void guidoEncB() {
guidoEncBCount = guidoEncBCount + 1;
}
void loop() {
Serial.print(guidoEncACount);
Serial.print(" ");
Serial.println(guidoEncBCount);
}
I'm surprised by results: it's not working yet as expected but is really more stable. For a complete wheel round I should have 6 'clicks' by the encoder. Using this code counters are increased sometimes by 1 or 2 per clicks (before sometimes it was incremented also by 100!). I moved the wheel by hands and it seems the behavior is not affected by the wheel speed.
I try also this code:
#include "GeduinoPINS.h"
unsigned int guidoEncACount = 0;
unsigned int guidoEncBCount = 0;
int guidoEncAPreviousState = LOW;
int guidoEncBPreviousState = LOW;
int count = 0;
void setup() {
Serial.begin(9600);
pinMode(GUIDO_ENCA, INPUT);
pinMode(GUIDO_ENCB, INPUT);
}
void loop() {
const int guidoEncAState = digitalRead(GUIDO_ENCA);
const int guidoEncBState = digitalRead(GUIDO_ENCB);
if (guidoEncAState == HIGH && guidoEncAPreviousState == LOW) {
guidoEncACount++;
}
if (guidoEncBState == HIGH && guidoEncBPreviousState == LOW) {
guidoEncBCount++;
}
guidoEncAPreviousState = guidoEncAState;
guidoEncBPreviousState = guidoEncBState;
if (count++ == 1000) {
Serial.print(guidoEncACount);
Serial.print(" ");
Serial.println(guidoEncBCount);
count = 0;
}
}
More surprised again: it works perfectly!
I add the if statement on the serial output in order to avoid a fix delay after every read.
You have what appears to be a simple encoder wheel printed on paper and either taped or glued to the encoder wheel supplied with the kit and motor; it doesn't appear like you have mounted that paper wheel perfectly flat on the supplied encoder wheel; it needs to be kept as perpendicular to your sensor as possible. In fact, printing it on heavier stock may be in order. Also, increase the darkness level of your printer as much as possible, and make sure you are printing with black ink only (not some fake combo like some printers use) - ideally, you should be using a black and white laser printer and toner, on the darkest and densest print setting. Even better would be to use flat (non-gloss) black and white paint (instead of printing).
I know this is not well build! BTW I use laser printer and I also tested the result with sensor before mounting and the output change from 0.3 to 3.2 passing from black to white, so I think they are ok. In future I want to improve it using white sticky paper for white bands black velvety sticky paper for the black ones (this should be best to avoid reflection at all). I will also build a 'case' to isolate encoders between them and the environment.
During design I first think about the sparkfun sensor you link but it was too wide and not so easy to integrate on my project. Of course, if I will fail with actual system, I will try it.
Thanks for all suggestion but I think I have some general problem with interrupts. I absolutely need to use it for my encoders but I really cannot understand the behavior of my code. I have a lot on experience on computer programming but not on microcontrollers and I'm learning is something complete differently.
Can anyone explain what happens?
Thanks for support
Ale