Best way to trigger a function from running analog input?

I should start by saying I don't have the strongest programming background. My project involves a rotating wheel with an attached encoder. The goal is to trigger an event when the wheel reaches a certain angle.

Currently I have this set up with a simple if statement stating if (the wheel == angle) {trigger event}. I am hoping to find a more robust solution to this issue. Some potential sources of error could be, if the wheel stops at the exact angle and continuously triggers the event. Another issue could be if the encoder skips over 30 and misses the trigger.

I understand that I could patch these issues individually, but was wondering if there was a standard way of performing such a task.

Thank you

Posted Mar 12, 2018, 1:04 PM UTC

24
chrwei•

you have a good start. add a global variable, then change it to if (wheel >= angle && !isTriggered) {isTriggered=true; rest of trigger event} then add another condition to set isTriggered=false when the wheel is less than the angle.

3
Red_ChairfaceOP•

This is exactly what I was looking for! Thank you, it works great!

1
trumpke_dumpster•

"Hysteresis" is one way to deal with the analog world in digital programs.
https://courses.ideate.cmu.edu/16-223/f2015/guide/ex/Arduino/input-hysteresis/input-hysteresis.html

2
Red_ChairfaceOP•

I should have mentioned that the wheel will only be turning in one direction, otherwise this would have been more useful. Thanks

1