Show number by LED - wrong behavior


#1

Hello,

Programming mBot to react to sensors I need to see sensor value (like sound, light sensor values). As there is no possibility to “write” - I decided to write program to “show” number by lighting numbers of LEDs. I use “mod” to get last digit of number and show it - this way 258 will light 8 LEDs, then 5 LEDs, then 2 LEDs, so I will be able to understand it’s 258 (in reverse order). Each 3rd lighted LED is blue, to be easier to count them.

I created program:

Variable “Number” is any number I’d like to show. I tried with smaller and bigger ones, like 28, 2468 and so on.
The problem is, that it works incorrectly:
for 28 it lights 4,8,3 LEDs (it’s count, not LED id).
for 2468 it lights 3,8,7,5,3 LEDs (it’s count, not LED id).

So first number is “ghost” (sometimes there is no ghost number), then second is last number, displayed correctly, then all remaining numbers are +1 from value needed. I expected it might be “real number” approximation problem (as I see it “double” in Arduino C code), so I used “round” everywhere, but problem is still there.

Please advice.

Technical data:
mBot Ranger 3-in-1 (SKU 90092)
Windows 7, 64bit
mBlock v.3.3.0 (beta), 3.2.2 have no my mBot board support
Auriga board selected


#2

@CaptAm,

Actually, the code is providing the correct answers for what is programmed. In this case, rounding up is carried to the higher order number (i.e. 28 will round up to 30). If you tested with 123, you will get 321 in response.

You can correct this by removing all of the rounding functions. Below is a stripped down version of the logic. I tested it directly in mBlock since I don’t have the Auriga.

If you want the numbers in forward order, (2468 to 2468 instead of 8642) that will take a bit more work, and may need to be done in the Arduino IDE.

Mike


#3

@mddickey, thanks for a help. “round(number/10)” is not “number div 10”. Shame on me. This was biggest mistake here.

I present “Show sound level by LED” program to anyone, who would need to see real values of Arduino sensors.
In example I use sound sensor, and “200” seams reasonable “silence threshold” for me. You can change 200 to 0, if You want Your mBot to continuously measure and display noise level.

As I mentioned before - numbers are shown in reverse order, so if seven, three and two LEDs are lighted - then sound sensor value is 237. All green LEDs is for program start, and one green LED is wait for “sound input” - clap Your hands, for example. Sometimes zero LEDs are lit and beep is heard - so zero is also a number.

Have fun. Comment or press “like” if it was useful for You, as it’s my first Arduino/mBot project :wink: