olikraus / u8g2

U8glib library for monochrome displays, version 2
Other
5.06k stars 1.04k forks source link

How to pass a variable as a parameter to u8g2.drawstr? #2464

Closed diegomaquino closed 2 months ago

diegomaquino commented 3 months ago

I'm working with Atmel Studio and Arduino Atmega328p. The variables I pass do not show on the display, or the attempts I made returned garbage or strange characters.

My code:

char qtd_serial[];

for (int i = 0; i < 12;i++) { qtd_serial[i] = USART_Receive(); }

u8g2_ClearBuffer(&u8g2);
u8g2_SetFont(&u8g2, u8g2_font_helvB08_tr);
u8g2_SetFontRefHeightText(&u8g2);
u8g2_SetFontPosTop(&u8g2);
u8g2_DrawStr(&u8g2, 0, 15, "Result:");

u8g2_DrawStr(&u8g2, 0, 17, qtd_serial);

u8g2_DrawStr(&u8g2, 0, 4, "End");
u8g2_SendBuffer(&u8g2);

I sent it via serial : 123456789123 the display shows:

Result: xf~ End

olikraus commented 2 months ago

If the incoming data is not a string, then the data needs to converted to decimal or hex numbers first. Maybe look into "sprintf".