Hi.
I need to read puls from rotary encoder in printer.
I have a problem.
With slowly turn by the hand my Arduino Uno R3 read corectly puls.
But when Encoder turn by printer (not very fast) I missed all puls( noread)
I agree with MarkT that the entire structure of your program which exchanges RISING and FALLING interrupts is problematic, and you are better restructuring around CHANGE and reading the state of both pins.
However, there are two basic errors I see that may be giving you your current program poor results at higher speeds.
pulses, A_SIG and B_SIG are all variables changed within the interrupts, and should be declared as a volatile int.
2)You are using Serial.print from within the ISR. All interrupts are disabled within an ISR and Serial print uses interrupts.
Thank you MarkT and catlledog!
I try to use your code.
By the way, I try to use this code and was ok, but I don't know, that some puls was missing or no.
New code>
int val;
int encoder0PinA = 2;
int encoder0PinB = 3;
int encoder0Pos = 0;
int encoder0PinALast = HIGH;
int n = HIGH;
void setup() {
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
digitalWrite(encoder0PinA, LOW);
digitalWrite(encoder0PinB, LOW);
Serial.begin (9600);
}
void loop() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == HIGH) && (n == LOW)) {
if (digitalRead(encoder0PinB) == HIGH) {
encoder0Pos++;
Serial.println ("Foward dir");
}
else {
encoder0Pos--;
Serial.println ("Rewers dir");
}
Serial.println (encoder0Pos);
}
encoder0PinALast = n;
}
Please learn to post your code between code tags. They are available on the tool bar icon which is a scroll with <>
You would do well to read the Arduino Playground rotary encoder tutorial athttp://playground.arduino.cc/Main/RotaryEncoders
Your code looks similar Example 1 in the tutorial. That example only increments or decrements the count on a falling edge of PinA. This only reads 1/4 of the encoder states available. Depending on the counts per revolution of your encoder, this may be adequate for your requirements. Your, previous code was attempting to read all four states.
To Cattledog.
you're right, this code from encoder tutorial. I'm not programmar, I must take same code, do studio of this and rewrite for my requirements.
I know, this code do not read full pulses, but for my requirements is ok, I only need to know DIR and duration, when encoder turns, that I send my correct pulses to stepper on this time, becouse encoder works with constant cycle ( turn-stop, turn-stop.......).
For later, I will lean to post code tags to forum.
thank you.
Hi,
Do you have a part number for the encoder?
It may be open collector output, you may need to fit 10K resistors from each of the encoder outputs to 5V supply.