Go Down

Topic: Big Numbers from int (Read 787 times) previous topic - next topic

cbroc

I am new to Arduino and started a project to make a digital guage for a boat. I saw the large fonts all over the forum and on projects like MPGuino and ODBuino, but it took me a while to figure it out. I thought it would be good to post this code for anyone else who is trying to do the same. This is still not perfect, but I think it would be simple for a newbie like me to implement into a project. Here is the code,  like I said I am a newbie, so all comments are welcome.

My main goal is to write a function that takes an int and prints it onto a 16X2 LCD using two lines for each digit.

Code: [Select]
/*This sketch uses functions taken from various forums on the Auduino website. It is for a specific project, but could easily be modified for other uses.
the purpose of this sketch is to create a function that can take an integer and print it in a 2 line "large font"
on an Alpha-numberic LCD screen. This sketch uses those functions to demostrate the possibilties in a loop in big numbers
written by Chad Brochard; with a trememdous amount of material obtained from the Arduino forum at www.arduino.cc
Date 11/11/11 started at 11:11 in the afternoon. Just on more chance for that mathematical redundancy this century in one year, one month, one day, one hour,
and one minute from now... wait next year is a leap year... so that is one year, one month, two days, .... you get the idea.
*/

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

byte custchar[8][8] = {
{
   B11111,
   B11111,
   B11111,
   B00000,
   B00000,
   B00000,
   B00000,
   B00000
}, {
   B00000,
   B00000,
   B00000,
   B00000,
   B00000,
   B11111,
   B11111,
   B11111
}, {
   B11111,
   B11111,
   B11111,
   B00000,
   B00000,
   B11111,
   B11111,
   B11111
}, {
   B00000,
   B00000,
   B00000,
   B00000,
   B00000,
   B01110,
   B01110,
   B01110
}, {
   B00000,
   B00000,
   B00000,
   B01110,
   B01110,
   B01110,
   B00000,
   B00000
}, {
   B00000,
   B00100,
   B01010,
   B10001,
   B01010,
   B00100,
   B00000,
   B00000
}, {
   B00000,
   B11011,
   B11011,
   B00000,
   B00100,
   B10001,
   B01010,
   B00100
}, {
   B00000,
   B00000,
   B00000,
   B00000,
   B00000,
   B00000,
   B00000,
   B00000
}
};

byte bignums[10][2][3] = {
{
   {255, 0, 255},
   {255, 1, 255}
},{
   {0, 255, 254},
   {1, 255, 1}
},{
   {2, 2, 255},
   {255, 1, 1}
},{
   {0, 2, 255},
   {1, 1, 255}
},{
   {255, 1, 255},
   {254, 254, 255}
},{
   {255, 2, 2},
   {1, 1, 255}
},{
   {255, 2, 2},
   {255, 1, 255}
},{
   {0, 0, 255},
   {254, 255, 254}
},{
   {255, 2, 255},
   {255, 1, 255}
},{
   {255, 2, 255},
   {254, 254, 255}
}
};

void loadchars() {
lcd.command(64);
for (int i = 0; i < 8; i++)
   for (int j = 0; j < 8; j++)
     lcd.write(custchar[i][j]);
lcd.home();
}


//This function prints an integer between o and 999 in a big font at the left of the display
// Argument is a byte array Digits[# of digits, Digit 1, Digit 2, Digit 3]
// Nothing is returned.
// Numbers outside of the limits of this function will print an error screen.


void printCustom (int x)
{
byte digits[4];
if (x>99)
{
digits[0] = 3;
digits[1] = x/100;
digits[2] = (x%100)/10;
digits[3] = (x%100)%10;
}
if ((x>9) && (x<=99))
{
digits[0] = 3;
        digits[1] = 0;
digits[2] = x/10;
digits[3] = x%10;
}
if (x<=9)
{
digits[0] = 3;
        digits[1] = 0;
digits[2] = 0;
        digits[3] = x%10;
}


if (digits[0] > 3){
  lcd.print("PRINT ERROR");
  return;
}
for(int e = 1; e < 2; e++)
{
if(digits[e] > 9){
            lcd.print("PRINT ERROR");
             return;
            }
}

for(int digitLoc= 0;digitLoc<digits[0];digitLoc++)
{
for (int i = 0; i <2; i++)
{
lcd.setCursor(digitLoc*4,i);
for(int j = 0; j < 3; j++)
{
lcd.write(bignums[digits[(digitLoc+1)]][i][j]);
}
lcd.write(254);
}
}
}


//Setup the lcd screen and load the custom characters

void setup() {
loadchars();
lcd.begin(16,2);
Serial.begin(9600);

}

// main loop, this sketch counts from 1 to 10, then from 90 to 105, Then from 159 to 959 by 100s. to show the capabilty of printing up to 3 digits.
// these nubers are arbitrary, just want to be sure to test numbers with 1, then 2, then 3 digits.


void loop() {

int sampletemp = 75;
int errortest = 1234;
 
for (int i=0; i < 10; i++)
{
printCustom (i);
         Serial.println(i);
delay(250);
}

for (int i=90; i < 105; i++)
{
printCustom (i);
         Serial.println(i);
         delay(250);
}

for (int i=159; i < 959; i=i+100)
{
printCustom (i);
        Serial.println(i);
delay(250);
}
lcd.setCursor(0,0);
printCustom(sampletemp);
Serial.println(sampletemp);//write the sample temperature, 75. using printCustom (big font)
lcd.setCursor(11,0);
lcd.write(5); //show a 5th custom character which is supposed to look like a degree mark.
delay(2000);

//cause a print error to test the error
lcd.setCursor(0,0);
printCustom(errortest);
delay(2000);

}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy