I am just getting back into dabbling with my hc-sr04...so i decide to use it and some neopixels to make a stop meter for my mom sho she doesn't wreck her car pulling into the garage. my issue is simple...if i get too close or too far with from the sensor, it stops working...if you reset the arduino it works fine or if you just pull the sensor and put it back in it will reset and work fine until you are too close or too far away again. here is the code, let me know if i am doing anything wrong...but it works as planned ortherwise:
according to how new ping is supposed to work, if the ping is greater than the MAX_DISTANCE then it will return 0...in that case, it already takes into consideration that case on the if statement.
so dis should only be between 0 and 120...this will ensure that code is never negative.
codeFact is the increment in cm between the lights moving to the next step...this will be dynamic so if i decide to use a larger distance or more leds, that it will auto adapt.
I added LEDNUM later on to keep with the convention of having all my changeable variables at the top of the code...I'm just weird like that.
the ranges for each color will very depending on the MAX_DISTANCE and LEDNUM...so that is why I make codeFact...ex:// if i have 5 leds...there will be LEDNUM * 3 different light stages (sPhase) that will be shown...the codeFact is the range of a particular stage...in the previous example, assume I have the MAX_DISTANCE as 150...then the first led will turn on when distance is between 150 - 141
the section of the loop that displays the colors for the distance works fine...my issue is when the distance is 0 OR is greater than MAX_DISTANCE, the sensor stops click and sending data on the ping...when one of these occur, then the sensor fails until the arduino is reset (by reset button or pulling the power) OR but removing the power from the sensor and restoring it