I have Project to Measure Length textille product in Meters and i have Arduino UNO + hanyoung Rotary Encoder 500 Pulse
I have Sketch that use serial communication to show counter in pulse and i put the wheel with rubber with 6.5cm diameter
so 16.5 x 3.14 = 20.41cm -> (20.41/100) * 500Pulse =2449 pulse
It take 2449 Pulse for measure 1 Meter
so i draw 1 m line to measure my rotary.e
the problem Is!!
i have 2 sketch 1 use UDP ethernet and 2 use simple serial communication COM2
for sketch 1 i succes to measure 1m line
but if I use sketch 2 with com2 i measure lower result and not 1 meter
its seems Serial com have a limit to read my encoder or to slow for my encoder
i googling for answer maybe Direct port or using Interrupt maybe its a solve
but i have no experience yet, i learn arduino for 1 month...
but pin3 not work , i have no idea how to used for backward counter
You need to change the ISR sensor1 to be something like what you had when you were polling the pins. Your interrupt is triggered when pin 2 is RISING. If you look at the possible quadrature states of the encoder you will see that if pin3 is high when pin2 rises, you are going one direction, and if it is low when pin2 rises you are going in the other.
if(digitalRead(encoder1Pin)) //is pin 3 high when pin 2 triggered the interrupt?
{
count++;
}
else
{
count--;
}
There are several other problems with your sketch
Count is the variable that changes within the ISR, and it is the one which needs to be a volatile float
At fast measuring speed you may run into a problem with
Serial.flush();
This line tells the Arduino to print out everything in the buffer before it will process another ISR. If you are measuring the cloth fast you may miss counts while the serial print is happening. You want the ISR sensor1 , which increments or decrements count, to operate in between characters of the print out. It will do that because the external interrupt is a higher priority than the interrupt which controls the serial print.
Why is count a float? There is no need for that. Floats are only an approximation.
The problem with count being a multi byte number is that it could change when you are accessing it giving you a false value. You need to either use a byte for count in the interrupt and accumulate the running total in the loop function, or you need to disable the interrupts while you fetch count in your loop function.