I would be tempted to make a little function to set the RGB colors of your LED strip: void fillup(byte r, byte g, byte b)
. Then you would call it in two places: fillup(0,0,0)
and fillup(127,127,127)
(yeah, not 100% on).
Also, I'm confused why you have so many sensor()
calls in the first while
loop. Seems like you only need to call it once when you need updated values for distance
.
Also, the comments on the sensor
calls are confusing... after reading them I have no idea what sensor
is supposed to do. I tend to put a comment before functions that describes what they do, and put a stand-alone comment in the code to explain what the next "paragraph" does. And I avoid banners - they get in the way.
Do you want FastLED.show()
inside the loop that updates the colors? Or just after the loop, to update the hardware after the array is changed? IDK, just asking.
I usually do not mind globals, but in this case you would be better off letting sensor
return the distance. Then you could while ( sensor() <= 10 )
. Or use a do.. while
loop with one call to sensor
at the bottom if you want to keep the global.
I'd try to get rid of the floating point, too... just calculate what your thresholds are in the raw echo values, and use those. Do we really care what the internal units of measurement are?
Sorry for the long unload... There's nothing wrong with what you have.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…