Hello, I've been struggling with the use of interrupts together with the wheel encoders. I wanted to count each time the pulse is Rising for both the left motor and right motor. I tried showing the data in the serial monitor by simply printing it in the loop but I encountered that the encoder values are generating too high from expected and increments randomly instead of just by one. However, I managed to fix the problem by adding the "cli()" at the start of loop and "sei()" before ending and got the values correct. Now I made a function making the Robot Drive forward making use of the data to make it straight by adjusting the voltage given to the motors. But again, random value increments in the function and I don't know what to do. I tried adding the "cli()" and "sei()" functions but failed to fix. Help Please!!!
LM393 is compare IC, not encoder so we are off to a confusing start?
Add in your first sentace where you describe you want to use interrupts, but your attempts to fix the errors is by disabling interrupts..
I suspect that the encoder transitions are not clean and sharp, so you get multiple rising edges for each opening of the encoder wheel.
Write a simple program that prints out left_ticks and right_ticks while rotating either wheel by hand, or check the signals with an oscilloscope.
If that is the case, don't use sei() and cli(), and instead learn how to enable/disable the external interrupts. The encoder ticks won't happen very frequently, so inserting a small delay (a few milliseconds) before re-enabling each external interrupts might help.
If all interrupts are disabled, the millis() function won't keep time and the delay() won't work. delay() will not work within an interrupt, either.
Better yet, don't use interrupts with the encoders. Use polling methods instead, possibly with one of the switch debounce libraries.
Okay so the sensor your using outputs 0 or 1 and the encoderdisk looks like it will generate 39 outputs a revolution. 19 “off” and 20 ”on” signals.
Did you try a simple program to set motor spinning and serial print the millis each time encoder changes state or did you just write whole program at once?