LONGJA
Posts: 23
Joined: Wed Mar 28, 2018 4:04 pm

Trouble printing integer on I2C LCD in C

Wed Mar 28, 2018 4:21 pm

Hello,

I'm relatively new to RPi and have used a tutorial on Sunfounder to connect an LCD to a RPi3 via I2C. The program runs fine but I'm struggling to print the output from a sensor (via MCP3008 AtD). I've tried a number of configurations but the program will always print "%d" on the LCD instead of an integer. I imagine it's a simple fix but as I said, new to Pi and C so would appreciate some help.

The tutorial can be found here...

https://www.sunfounder.com/learn/lesson ... -plus.html

Many thanks

James

User avatar
Paeryn
Posts: 2902
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Trouble printing integer on I2C LCD in C

Wed Mar 28, 2018 6:58 pm

It helps if you give examples of code that doesn't work otherwise we can only guess at what code you used.

From your explanation it sounds like you are passing a formatting string directly to the write() function (bad choice of function name by whoever wrote that example code) but write() will just display the string exactly as is. I don't know how you tried to give the variable to be substituted in that string. You need to use something like snprintf() to fill in a buffer according to a given format string, just like printf() except you have two extra parameters at the start, the first is a pointer to the buffer where the string will be written and the second is the size of the buffer snprintf() will trunctate the string so that it doesn't overrun the buffer, the terminating null char will always be added.

Code: Select all

    int number = 4; // variable we are going to print
    char buffer[40]; // buffer to hold string
    
    snprintf(buffer, 40, "Value = %d", number);
    write(1, 1, buffer);
She who travels light — forgot something.

User avatar
DougieLawson
Posts: 38502
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Trouble printing integer on I2C LCD in C

Wed Mar 28, 2018 7:31 pm

The rest of the code is identical to the sample at https://www.sunfounder.com/learn/lesson ... -plus.html, I've changed the main routine to.

Code: Select all

void main(){
        fd = wiringPiI2CSetup(LCDAddr);
        init();
        write(0, 0, "Greetings!");
        write(1, 1, "From SunFounder");
        delay(200);
        clear();
        int i;
        for (i=0; i<1000; i++)
        {
                char strA[40];
                sprintf(strA,"%04d", i);
                write(0,0,strA);
                delay(200);
                clear();
        }
}
That counts up from 0001 to 1000 on my PCF based LCD.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
Paeryn
Posts: 2902
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Trouble printing integer on I2C LCD in C

Wed Mar 28, 2018 8:00 pm

DougieLawson wrote:
Wed Mar 28, 2018 7:31 pm

Code: Select all

        for (i=0; i<1000; i++)
        {
                char strA[40];
                sprintf(strA,"%04d", i);
                write(0,0,strA);
                delay(200);
                clear();
        }
}
That counts up from 0001 to 1000 on my PCF based LCD.
Surely it would count from 0000 to 0999. :roll:
She who travels light — forgot something.

User avatar
DougieLawson
Posts: 38502
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Trouble printing integer on I2C LCD in C

Wed Mar 28, 2018 11:57 pm

Paeryn wrote:
Wed Mar 28, 2018 8:00 pm
DougieLawson wrote:
Wed Mar 28, 2018 7:31 pm

Code: Select all

        for (i=0; i<1000; i++)
        {
                char strA[40];
                sprintf(strA,"%04d", i);
                write(0,0,strA);
                delay(200);
                clear();
        }
}
That counts up from 0001 to 1000 on my PCF based LCD.
Surely it would count from 0000 to 0999. :roll:
I only ran it for a minute until I was convinced the leading zeros were working. I can fix the 0 to 999 versus 1 to 1000 later.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

LONGJA
Posts: 23
Joined: Wed Mar 28, 2018 4:04 pm

Re: Trouble printing integer on I2C LCD in C

Thu Mar 29, 2018 3:46 pm

Many thanks for all your responses. I'll have a go and let you know have it goes.

LONGJA
Posts: 23
Joined: Wed Mar 28, 2018 4:04 pm

Re: Trouble printing integer on I2C LCD in C

Thu Mar 29, 2018 9:41 pm

Worked perfectly - many thanks!

Return to “C/C++”