Code doesn't display the sensor readings

Hey guys,

I'm trying to display sensor readings onto an LCD (128x64).

I tested the gas sensor to check if its functional. It worked and displayed the reading on a serial monitor.
I then wrote this code but I could not get it to display on the LCD. I'm basically trying to implement a smoke/gas alarm system.

Here is the code:

#include <U8g2lib.h>
#include <U8x8lib.h>

int LED1 = 9;
int LED2 = 12;
int buzzer = 22;
int smokeA0 = A6;
int sensorThreshold = 600;
 int analogSensor ;
 char gas [3];




U8G2_ST7920_128X64_1_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* CS=*/ 10, /* reset=*/17);

void setup() {  


  Serial.begin(9600); 
  u8g2.begin();
  u8g2.enableUTF8Print();
  u8g2.setFont(u8g2_font_helvB10_tf); 
  u8g2.setColorIndex(1);  
  pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(smokeA0, INPUT);
}

void draw(){



  
  u8g2.drawFrame(0,0,128,31);         
  u8g2.drawFrame(0,33,128,31);           
  
  u8g2.drawStr( 15, 13, "Gas/Smoke:");   
  u8g2.drawStr( 35, 38, analogSensor);   

  u8g2.drawUTF8(76, 28, "PPM");

}


void readsmoke()
{
  float g = analogRead(smokeA0);
  dtostrf(g,3 , 0, gas);
}

void loop() {  
  u8g2.firstPage();
  do {   
    draw();
  } while( u8g2.nextPage() );

analogSensor = analogRead(smokeA0); 

if (analogSensor > sensorThreshold)
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
tone(buzzer, 1000, 200);
}
else
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
noTone(buzzer);
}
delay(1000);
}

I have attached the results I got on the LCD. Everything is displayed but not the readings. It shows symbols instead of numbers.

Any help is appreciated

  float g = analogRead(smokeA0);

The first thing I notice is that analogRead() returns an int not a float

Maybe your library doesn't know about floats.

UKHeliBob:

  float g = analogRead(smokeA0);

The first thing I notice is that analogRead() returns an int not a float

How can I solve this problem?

Make "g" not a float.
Try "int"

Is "gas" big enough?

TheMemberFormerlyKnownAsAWOL:
Is "gas" big enough?

It should display 3 numbers max. So I think it is.

Three digits?
What about the terminator?

TheMemberFormerlyKnownAsAWOL:
Make "g" not a float.
Try "int"

I did but it still doesn't display the number.

TheMemberFormerlyKnownAsAWOL:
Three digits?
What about the terminator?

I dont know what a terminator is. If you could tell me how I can add a terminator, that would be great.

Google, "C terminator"

dtostrf converts a double to a string.
In C, strings need a terminator, and the array needs to be big enough to hold the characters in the string AND the terminator.

Make "gas" bigger.

aarg:
Google, "C terminator"

Basically what I understood is that is should add = '\0'; after the char array.

Is that what you mean?

dtostrf will add one for you, but you'd better be sure the destination buffer is large enough.

TheMemberFormerlyKnownAsAWOL:
Make "gas" bigger.

I made the array size 20 but still does not work.

 u8g2.drawStr( 35, 38, analogSensor);

"drawStr" suggests to me that "analogSensor" should be a string.

How about you?

TheMemberFormerlyKnownAsAWOL:

 u8g2.drawStr( 35, 38, analogSensor);

"drawStr" suggests to me that "analogSensor" should be a string.

How about you?

I tried it and that is the compiler message:

"no matching function for call to 'U8G2_ST7920_128X64_1_SW_SPI::drawStr(int, int, String&)'"

You tried a String?
Why?

"no matching function for call to 'U8G2_ST7920_128X64_1_SW_SPI::drawStr(int, int, String&)'"

Did you declare analogSensor as a String or as an array of chars (aka a C style string) ? From the error message it looks like you did the former. Please post your complete revised sketch

I did manage to solve it.For people having the same issue in the future, I was using the wrong display function.

This is what it should look like:

void draw(){

  u8g2.drawFrame(0,0,128,31);         
  u8g2.drawFrame(0,33,128,31);           
 
  u8g2.drawStr( 20, 13, "Gas/Smoke:");   
  

u8g2.setCursor(35, 28);
u8g2.print(analogSensor);

  u8g2.drawUTF8(65, 28, "PPM");

}

This is the part that I modified.

The print function is used for displaying numbers.