U8Glib problem printing value

Hi!,

I'm trying to use a generic Ebay Oled display as a voltmeter whilst changing the voltage with a potentiometer for testing to use in a further project.

The display is working as it should until I try to use "u8g.print();" to display the voltage from a potentiometer, it works on serialprint but it keep saying "voltage was not declared" and I'm a little stuck now.. Here is the code;

#include "U8glib.h"

U8GLIB_SH1106_128X64 u8g(4, 5, 6, 7);

void draw(void) {
  u8g.setFont(u8g_font_9x15B);
  u8g.drawStr( 0, 20, "Voltage");
  u8g.setPrintPos(0, 50);
  u8g.print(voltage);
}


void setup() {
  
  Serial.begin(9600);
}


void loop() {

  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023.0);
  Serial.println(voltage);

   u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  
}

any idea's?

Connor9422:
any idea's?

Yes. You declared 'voltage' in 'loop()', making it a local variable that's not visible to code outside the 'loop()' function.
Therefore, 'voltage' can't be found when you call this from 'draw()':-u8g.print(voltage);
The fix is to declare 'voltage' as a global at the top of your program:-float voltage;
Then in loop when you use it, change this:-float voltage = sensorValue * (5.0 / 1023.0);to this:-voltage = sensorValue * (5.0 / 1023.0);
I'm surprised that no one saw this thread and answered earlier.

Thank's buddy! That a little tinkering sorted it out :slight_smile:

Connor9422:
Thank's buddy! That a little tinkering sorted it out :slight_smile:

Excellent. Glad I could help. :slight_smile: