Hi I want to make an Odometer to read in the range 0000.00 Miles to 9999.99 Miles

I have a hall switch and magnet on a wheel that gives a pulse for each revolution From that I can calculate the distance travelled My plan is to have a variable hold the current distance and display it on an LCD

I also want to save the variables value to eeprom when I switch off and recall it so it can continue from where it left off on start up next time

I know how to get it to save when it detects the power being switched off and I think I understand how to save /recall from eeprom Where I am stuck is ,if the mileage was 17.34 Miles it will display as 17.34 but a real odometer would pad it out to 0017.34 How do I get it to add as many leading zero's as is required to fill all 6 spaces

Cheers Don

I think, the simplest way is to use sprintf resp. sprintf_P to prepare string in temporary variable and then send it to the LCD. Function is part of standard C (stdio.h).

“0000.00 Miles to 9999.99”

Hint – all printed output is text. You can print text to suit.

if ( value < 1000 ) // leading zeros
{
write( ‘0’ );

if ( value < 100 )
{
write( ‘0’ );

if ( value < 10 )
{
write( ‘0’ );

// I can’t test right now but IF Arduino prints .123 as “0.123” then this step is not needed.
if ( value < 1 )
{
write( ‘0’ );
}
}
}
}

print( value );

Budvar10: I think, the simplest way is to use sprintf resp. sprintf_P to prepare string in temporary variable and then send it to the LCD. Function is part of standard C (stdio.h).

sprintf() adds much to the size of the code.

I like it very much but for formatting bigger things than leading zeros. Simple code for simple things!

Yes, those functions for formatted data take about 1k of size.

@GoForSmoke ... simple for the user :) But, I'd want to let the chance for others to contribute in topic.

Leading zeros is too quirky. Should go for leading blanks.....except for 0.00

Budvar10: Yes, those functions for formatted data take about 1k of size.

@GoForSmoke ... simple for the user :) But, I'd want to let the chance for others to contribute in topic.

Please do! I am not trying to stop that! Only making comment and my name is not Trump!

Also, I am not so sure it makes the new users task simpler in that I sent a good deal of time learning all the ins and outs of sprintf() once I found out it was there.

It's as much as strtok() which generally I code around most every time, but that is part of my process-as-it-arrives--buffering-wastes-time approach. If the data is already in string(s) then I strtok(), otherwise not.

When the CPU has these commands in microcode, I'll lean towards them. 10 more years?

Thanks GoForSmoke

I tried your code and I have it working great now
You were right about it adding its own zero when the number is less
than one so I missed that bit out

thanks for the help

Don

GoForSmoke:
“0000.00 Miles to 9999.99”

Hint – all printed output is text. You can print text to suit.

if ( value < 1000 ) // leading zeros
{
write( ‘0’ );

if ( value < 100 )
{
write( ‘0’ );

if ( value < 10 )
{
write( ‘0’ );

// I can’t test right now but IF Arduino prints .123 as “0.123” then this step is not needed.
if ( value < 1 )
{
write( ‘0’ );
}
}
}
}

print( value );

I dont know why you say quirky every vehical I ever drove with an odometer had leading zeros not blanks

Southpark: Leading zeros is too quirky. Should go for leading blanks.....except for 0.00

DonRecardo: I dont know why you say quirky every vehical I ever drove with an odometer had leading zeros not blanks

They really couldn't do it any other way with the mechanics of odometer wheels. Each wheel from right to left turns from 1 to 9 then 0 once to make the next.

If you replaced the zeros with blanks then 10203 would appear as 1 2 3.

With car odometers the decimal point is fixed in place. The numbers on the wheels are an integer scaled in 1/10th mile (or Km) with a painted-in decimal point to look like floating point.

How many revolutions per 1/100th mile? Per mile? Per 100 miles? Cars used to use gear teeth to get ratios right or closely so to turn the 1/10th mile (or Km) wheel that turned the next-left wheel that.. etc up the line. Could your code do that with integers?

DonRecardo: every vehical I ever drove with an odometer had leading zeros not blanks

That would be because of the cost and complication of making a mechanical 11 character display. That would not be a result of rigorous user interface testing.

I dont know why you say quirky

(My current vehicle does not display leading zeros on the odometer.)

[quote author=Coding Badly link=msg=3339585 date=1500064715] That would be because of the cost and complication of making a mechanical 11 character display. That would not be a result of rigorous user interface testing. [/quote]

Don't the zeroes also serve a hidden purpose of showing exactly how many digits there actually are? When rollover will happen/ did happen?

[/quote]

Every industrial NC/CNC machine I programmed used leading zeros, no decimals or trailing zeros.

You can put a display between the human and machine but the underlying system uses leading zeros, it is easier.

If my computer doesn't show me bits does that mean that bits are no longer needed, there's some other way?

GoForSmoke: Every industrial NC/CNC machine I programmed used leading zeros, no decimals or trailing zeros.

For cases where there are decimal points ..... having leading zeroes to the left of the whole number (integer) part can be a distraction, or even confusing. And for those that reckon they don't know when the roll-over occurs..... then just put a marker on the front face, or some instructions that tells the user how many significant digits there are all-up in the display.

Somebody also mentioned that leading zeros can indicate when roll-over occurs. But normally, roll-over for an odometer is probably not even on somebody's mind when driving.

But if somebody wants leading zeros for their display ..... then that's ok too, as that's the way they want it.

DonRecardo: Hi I want to make an Odometer to read in the range 0000.00 Miles to 9999.99 Miles

I have a hall switch and magnet on a wheel that gives a pulse for each revolution From that I can calculate the distance travelled

What exactly is the size of your wheel?

My plan is to have a variable hold the current distance and display it on an LCD

I also want to save the variables value to eeprom when I switch off and recall it so it can continue from where it left off on start up next time

I know how to get it to save when it detects the power being switched off and I think I understand how to save /recall from eeprom Where I am stuck is ,if the mileage was 17.34 Miles it will display as 17.34 but a real odometer would pad it out to 0017.34 How do I get it to add as many leading zero's as is required to fill all 6 spaces

Cheers Don

``````long rawDistance = 1734; // in hundredths of a mile

int milesWhole, milesHundredths;
char milesBuffer[10];

milesWhole = (rawDistance / 100) % 10000;
milesHundredths = rawDistance % 100;

sprintf(milesBuffer, "%04d.%02d", milesWhole, milesHundredths);
lcd.print(milesBuffer);
``````

Southpark: For cases where there are decimal points ..... having leading zeroes to the left of the whole number (integer) part can be a distraction, or even confusing. And for those that reckon they don't know when the roll-over occurs..... then just put a marker on the front face, or some instructions that tells the user how many significant digits there are all-up in the display.

Somebody also mentioned that leading zeros can indicate when roll-over occurs. But normally, roll-over for an odometer is probably not even on somebody's mind when driving.

But if somebody wants leading zeros for their display ..... then that's ok too, as that's the way they want it.

I think that you should actually try using fixed-point math, do the work.

"For cases where there are decimal points" --- right over your head.

The machines I made tapes for used 5 digits. From high order to low:

10's of inches 1's of inches Tenths of inches Hundredths of inches Thousands of inches.

We used 1000's of inches the same way that in electronics we take a milliamps.

The machines I made tapes for back then were 60's up to 1980 tech. Reading the high order values first worked easier with less circuitry. To move an X-Y table to X05 Y0375 may seem counter-intuitive to you but it is not to anyone trained to work in the field and it is downright native to the machine.

"Somebody also mentioned that leading zeros can indicate when roll-over occurs. But normally, roll-over for an odometer is probably not even on somebody's mind when driving."

If I'm looking at buying used car that shows wear and the mileage is too low to be all it has covered, knowing how many digits to rollover tells me something very real.

This is also true of electric, gas and water meters. You see the dials and when the current reading is less than the last reading you know that the meter has rolled over and you require knowing the number of dials to calculate usage.

If your paycheck depended on it, you'd know these things. Mine did, moreso when I wrote software to do these things and more. I'm not joking or philosophizing or trying to figure this out as I go, I covered these things before 1990.

Note that the system we’re talking about is digital… with a led or lcd etc display.

The display can be programmed to provide leading zeroes if that’s required. I’m just saying that having leading zeroes for a display with decimal point numbers is quirky. But … if required… do it.

GoForSmoke: If I'm looking at buying used car that shows wear and the mileage is too low to be all it has covered, knowing how many digits to rollover tells me something very real.

It is good for your purpose..... although I did mention 'while driving'. For used car buying.... a different situation.

For odometer and trip odometer I see no advantage but then I'm pretty retro, like to program 8 bit no-OS computers.

I got a possibly best of both worlds idea.... leading underscores... like..

__0.00 __5.67