i have a rotary encoder to set a value for a setpoint(calibration).
Everything works fine as it should ! But when the device reboots it adds one increment to the stored value Ex: if the value was 7.0 before reboot, after reboot the value will be 7.1 and subsequent reboots will keep on adding 0.1 to the previous value !!
Without having some details of your rotary encoder it’s difficult to know for sure, but I think the code in your void loop may be incrementing the value of setpoint the first time it runs.
You’ll be able to check this by commenting-out everything in your void loop after timer.run(); and rebooting your device.
Some well placed and informative Serial.print statements would also allow you to track the value of the various variables involved in the nested if statements to work-out exactly what’s happening.
Well i took it in a different way !
The rotary encoder has a inbuilt push button.
I am reading the push button, and when its high i will set the flag == 0 and call the encoder function encoder() under if condition
at the same time rotate the encoder to change the the value. Once the button is released the flag is set to 1 under else condiion and nothing can happen further !!
Now reboot does not effect the set value !!
This is crude\bad method, but works well !! (Really bad at coding)
If anyone can guide me with a right way that will be really appreciated…
Now when you understand rotary encoder better, there is some code with sufficient comment for you to adapt it for your needs. The code is written to allow Rotary Encoder Type to be defined later as a configuration option. Original code workts on MicroChip PIC processor but had proven to work for arduino as well.
If you know the type you can just pick the code for your type. Now put both inputs in a "byte now.
So you need to assign:
In your code
now = digitalRead(encoder0PinA) * 2 ; // Read pin A and put it in bit 1
now += digitalRead(encoder0PinB); // Add bit B to now bit 0