Showing values of measured voltage and temperature on an OLED*

I cannot understand what is wrong with my code (probably because I am a newbie) and I was wondering whether there is someone who could help me with making it WORK. I get the same
error each time: 'draw' was not declared in this scope. Maybe it has something to do with global variables. :smiling_face_with_tear:

//Include the graphics library.
#include "U8glib.h" 
#include <Wire.h>






//Initialize display.
U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0);

void setup(void)
{
    //Set font.
    u8g.setFont(u8g_font_unifont);
    Serial.begin(9600);
}

void loop(void)
{
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage1 = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println((float) voltage1);

  // read the input on analog pin 0:
  int sensorValue2 = analogRead(A1);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage2 = sensorValue2 * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println((float) voltage2);

  float val0 = analogRead(A3);
  float mv = ( val0 / 1024.0) * 5000;
  float tmp1 = mv / 10;
  float farh = (tmp1 * 9) / 5 + 32;
  Serial.print("TEMPRATURE = ");
  Serial.print((float) tmp1);
  Serial.print("*C");
  Serial.println();
  delay(1000);
  /* uncomment this to get temperature in farenhite
    Serial.print("TEMPRATURE = ");
    Serial.print(farh);
    Serial.print("*F");
    Serial.println();
  */

  float val5 = analogRead(A4);
  float mv5 = ( val5 / 1024.0) * 5000;
  float tmp2 = mv5 / 10;
  float farh5 = (tmp2 * 9) / 5 + 32;
  Serial.print("TEMPRATURE = ");
  Serial.print((float) tmp2);
  Serial.print("*C");
  Serial.println();
  delay(1000);
  /* uncomment this to get temperature in farenhite
    Serial.print("TEMPRATURE = ");
    Serial.print(farh);
    Serial.print("*F");
    Serial.println();
  */

  float val6 = analogRead(A5);
  float mv6 = ( val6 / 1024.0) * 5000;
  float tmp3 = mv6 / 10;
  float farh6 = (tmp3  * 9) / 5 + 32;
  Serial.print("TEMPRATURE = ");
  Serial.print((float) tmp3);
  Serial.print("*C");
  Serial.println();
  delay(1000);
    u8g.firstPage();
    do {
        draw();
    } while (u8g.nextPage());

    //Delay before repeating the loop.
    delay(50);
}



void draw(void)
{
    
    

    //Write text. (x, y, text)
//    u8g.drawStr(0, 0, "U 1" + ((float) voltage1));
    u8g.drawStr(0, 10, "U 2" + ((float) voltage2));
    u8g.drawStr(0, 20, "T 1" + (tmp1));
    u8g.drawStr(0, 30, "T 2" + (tmp2));
    u8g.drawStr(0, 40, "T 3" + (tmp3));
    
}

Please copy all Arduino diagnostic output to the forum as text.

You declared voltage2 in void loop() but not in void draw(void) (there may be more, I stopped at this one so you might have a second look and correct things)

If you use a variable in more than one function, declare it globally (at the top, near the #include<> or declare them locally, and pass values from one function to another.

[edit]Same declaration issue with all tmpX variables[/edit]
[edit]Then you will need to remove the "redeclarations" inside the functions[/edit]

Ah, SOOO many more to correct... you will find them. Just keep trying to "compile" (CTRL-R) and read the last error, fix it, and recompile. You will get the hang of what the compiler wants.

1 Like

This won't work for several reasons:

draw();

You need to learn how to draw basic text and lines on the screen, so start with the u8g library examples and work your way through them. Adafruit has a great graphics tutorial, as well.

Thank you a lot. I have defined the variables globally and fixed the names, it helped. However, a new problem popped out, in order to display them I have to convert the variables from float to string. Any ideas?

Explain why, or use dtostrf

[edit]
Look up u8g.print(); and format it for your use:

  u8g.drawStr(x_text, y_text, "text");
  u8g.setPrintPos(x_volt, y_volt);
  u8g.print(your_float);

[/edit]

1 Like

Post the revised code, along with the explanation requested above.

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