can't get lcd to display characters and integers correctly (leading zeros issue)

hey everyone,

can anyone tell me why this code isn't working on my 16x1 LCD. channelCounter is a variable that can be anything from 0 to 9999, but I want to insert leading zeros when either the 1000's, 100's or 10's are zero, i.e. if channelCounter = 23, the display should read "Ch. Number:0023"

if (channelCounter < 1000)
        {
          lcd.clear();
          lcd.print("Ch. Numb");
          lcd.setCursor(0,1);
          lcd.print("er:0");
          lcd.setCursor(4,1);
          lcd.print(channelCounter);
        }
        else if (channelCounter < 100)
        {
          lcd.clear();
          lcd.print("Ch. Numb");
          lcd.setCursor(0,1);
          lcd.print("er:00");
          lcd.setCursor(5,1);
          lcd.print(channelCounter);
        }
        else if (channelCounter < 10)
        {
          lcd.clear();
          lcd.print("Ch. Numb");
          lcd.setCursor(0,1);
          lcd.print("er:000");
          lcd.setCursor(6,1);
          lcd.print(channelCounter);
        }
        else if (channelCounter < 1)
        {
          lcd.clear();
          lcd.print("Ch. Numb");
          lcd.setCursor(0,1);
          lcd.print("er:0000");
        }
        else
        {
          lcd.clear();
          lcd.print("Ch. Numb");
          lcd.setCursor(0,1);
          lcd.print("er:");
          lcd.setCursor(3,1);
          lcd.print(channelCounter);
        }

:~

ok, turns out i'm an eejit. I had the conditions in the wrong order, it should be checking < 1 first, then < 10, < 100, etc., not the way I had it, because then a number like eg. 23, satisfied a number of conditions within the loop.

feck. ah well, at least it's solved :slight_smile:

can a mod please mark this as solved. thanks

it should be checking < 1 first

He he.

can a mod please mark this as solved.

I think you just change the title and add [SOLVED] to it.


Rob