Math question

One of the many "add on's" that I have made for my home built robotic arm is a money box that detects and sorts coins.
Currently it displays the balance on an LCD screen but, as an example, £11.56 shows as £1156 (without the point).

What maths should I use to create the output I require? (I did a similar thing with temperatures using a PICAXE a few years back but I have been to bed since then :slight_smile: )

Simple arithmetic?

Suppose:

void setup() {
  // put your setup code here, to run once:
  long totalMoney = 1156;

   Serial.begin(9600);
   Serial.print(totalMoney / 100L);   // Note that's an 'L' not a '1'
   Serial.print(".");
   Serial.println(totalMoney % 100L);

}

void loop() {
  // put your main code here, to run repeatedly:

}

If you need to store the values or add the pound sign, I'm sure you can figure that out.

So I expect you to use a int (or long)? Which is a good think!

unsinged int amount = 1156;
lcd.write('£'');
lcd.print(amount / 100);
lcd.write('.');
lcd.print(amount % 100);

Thank you econjack and Sepillion.

That is exactly what i needed.

Don't use econjack's code without testing it!
It isn't complete.

septillion:
So I expect you to use a int (or long)? Which is a good think!

unsinged int amount = 1156;

lcd.write(‘£’’);
lcd.print(amount / 100);
lcd.write(’.’);
lcd.print(amount % 100);

unsinged int amount = 1156;
lcd.write('£'');
lcd.print(amount / 100);
lcd.write('.');
unsigned int myFraction = amount % 100;
if ( myFraction < 10 )  write( 0 );
print( myFraction );

GoForSmoke:

unsinged int amount = 1156;

lcd.write(‘£’’);
lcd.print(amount / 100);
lcd.write(’.’);
unsigned int myFraction = amount % 100;
if ( myFraction < 10 )  write( 0 );
print( myFraction );

This works up to 09 but then I get 010,011,012 etc

  lcd.print(balance / 100);
  lcd.write('.');
  int myFraction = balance % 100;
  if ( myFraction <= 9 )
    lcd.write("0");
  lcd.print( myFraction );

Sorted :slight_smile:

One misplaced semi colon !!!

B1Gtone:

  lcd.print(balance / 100);

lcd.write(’.’);
  int myFraction = balance % 100;
  if ( myFraction <= 9 )
    lcd.write(“0”);
  lcd.print( myFraction );




Sorted :) 

One misplaced semi colon !!!!!

?

AWOL:
?

This would do it.

  lcd.print(balance / 100);
  lcd.write('.');
  int myFraction = balance % 100;
  if ( myFraction <= 9 );
    lcd.write("0");
  lcd.print( myFraction );

But the quoted code was if ( myFraction < 10 )  write( 0 ); which wouldn’t

What I posted was an example in almost-code just to show adding the leading zero after the decimal point.

It should have been lcd.write( 48 ); of course. Or lcd.write( '0' );

GoForSmoke:
This would do it.

  lcd.print(balance / 100);

lcd.write(’.’);
 int myFraction = balance % 100;
 if ( myFraction <= 9 );
   lcd.write(“0”);
 lcd.print( myFraction );

I understood the example that Goforsmoke wrote. The problem lies with the semi colon after if(myfraction …<=9)
This puts a zero before numbers below and above 9 hence 011,012 etc.
Removing this semi colon solves the problem

if executes the statement or block immediately following it, if the condition is true.

; is an empty statement, so it checks the condition. If it's false, it continues with the following line that prints the 0. If it's true, it executes the empty statement (ie, doing nothing) and continues with the following line that prints the 0.

If you always use blocks with your if statements, it's less tempting to slip up and put semicolons after them.

DrAzzy:
If you always use blocks with your if statements, it's less tempting to slip up and put semicolons after them.

It also makes the intent of the coder easier to see, more definite.

Back when I wrote interpreted Basic everything counted but what came after REM (remove) and still even that had to be read to find the next line, every shortcut mattered on those old slow 8-bit CPUs but with a compiler the only reason is to save typing or "look cool" which really only cuts it in opinion-space. With time, experience and aging eyes (and what's behind them) the "kewl" gets old too.