U8g2 Error:invalid conversion from 'int' to 'const char*' [-fpermissive]

Ho to every one. I succesfull run a simple text in the display, but not show an integer.

/* Read Quadrature Encoder
   Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.

   Sketch by max wolf / www.meso.net
   v. 0.1 - very basic functions - mw 20061220

*/
#include <U8g2lib.h>      // make sure to add U8g2 library and restart Arduino IDE  

int val;
int encoder0PinA = D2;
int encoder0PinB = D3;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;


#define OLED_SDA  D2
#define OLED_SCL  D1
#define OLED_RST  D0
U8G2_SSD1306_128X32_UNIVISION_F_SW_I2C u8g2(U8G2_R0, OLED_SCL, OLED_SDA , OLED_RST);
const char *text = "U8g2 OTA Demo";  // scroll this text from right to left


void setup() {
  pinMode (encoder0PinA, INPUT);
  pinMode (encoder0PinB, INPUT);
  Serial.begin (9600);
  u8g2.begin();
  u8g2.clearBuffer();          // clear the internal memory
  u8g2.setFont(u8g2_font_8x13B_mf); // choose a suitable font
  u8g2.drawStr(0,10,text);  // write something to the internal memory
  u8g2.sendBuffer(); 
}

void loop() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
    } else {
      encoder0Pos++;
    }
    Serial.print (encoder0Pos);
    Serial.print ("/");
  }
  encoder0PinALast = n;
  u8g2.clearBuffer(); 
  //u8g2.drawStr(0,10,"Hola"); //THIS WORK FINE
  u8g2.drawStr(0,10,encoder0PinALast );
  u8g2.sendBuffer(); 
}

The error is : invalid conversion from 'int' to 'const char*' [-fpermissive]

if i chhange u8g2.drawStr(0,10,encoder0PinALast ); for u8g2.drawStr(0,10,encoder0PinALast );
Error: request for member 'c_str' in 'encoder0PinALast', which is of non-class type 'int'

I don't know how to solve tis problem :frowning:

If you look at the U8g2lib functions you will see that drawStr() only draws strings. https://github.com/olikraus/u8g2/wiki/u8g2reference#drawstr

If you want to draw a numeric expression you should turn it into a C string first. e.g.
u8

   char buf[7]; //big enough for -32768
   u8g2.drawStr(0,10, itoa(encoder0PinALast, buf, 10) );

This looks a bit messy. So it would be handy to just create a C helper function. Then you can use it in all of your projects.

void drawNumber(int x, int y, int value)
{
    char buf[7];  //big enough for -32768
    u8g2.drawStr(x, y, itoa(value, buf, 10);
}

then you can just say:

Note that this is intended for 16bit integer expressions. You would need a bigger buffer for signed 32bit integers e.g. on Arduino Due.

David.

Edit. Instead of u8g2.drawStr(x, y, "msg") you can use separate
u8g2.setCursor(x, y); and u8g2.print(expression); statements.

Note that this lets you print any kind of expression e.g. float, integer, "msg", String, ...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.