outputting serial data to an oled display

Hi all, I'm fairly new to arduino & programming, I'm trying to send serial data read from analogue pin(a0) to my oled display rather than the serial monitor.
my code compiles ok and I get a display but the data doesn't change from the initial value of 0.

here is the code:

#include "U8glib.h"

U8GLIB_SSD1306_128X64 u8g(10, 9);    
// HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)



 const int analogPin = A0;    // pin that the sensor is attached to
 const int relay1 = 2;       // pin that the relay is attached to
 const int relay2 = 4;
 const int relay3 = 7;
 int g_lastValue = 0;
 int nowValue = 0;


 void setup() {
   // initialize the digital pins as an output:
   pinMode(relay1, OUTPUT);
   pinMode(relay2, OUTPUT);
   pinMode(relay3, OUTPUT);
   u8g.setFont(u8g_font_unifont);
   u8g.setColorIndex(1); // Instructs the display to draw with a pixel on.
   // initialize serial communications:
   Serial.begin(9600);
}

 void loop() {
   // read the value of the sensor:
   int analogValue = analogRead(analogPin);
   int nowValue = analogValue;


   // relays operate depending on sensor position
   if (analogValue > 50 && analogValue < 325) {
     digitalWrite(relay1, LOW);
   } else {
     digitalWrite(relay1, HIGH);
   }

   if (analogValue > 325 && analogValue < 700) {
    digitalWrite(relay2, LOW);
   } else {
    digitalWrite(relay2, HIGH);
   }

   if (analogValue > 700 && analogValue < 975) {
   digitalWrite(relay3, LOW);
   } else {
   digitalWrite(relay3, HIGH);
   } 

   // check if reading has changed
   if (nowValue != g_lastValue) {
   g_lastValue = nowValue;
   
   delay(500);        // delay in between reads for stability
   }


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

    // send to oled display
void draw(){
  u8g.drawStr( 0, 20, "Sensor Reading");
  u8g.setPrintPos( 0, 40); 
  u8g.print(nowValue);
}

You have declared two nowValue

One is a global, and one local in the loop. If this is not an attention mistake the You should read about variable scope

Just get rid of the int in front of nowValue at the begining of your loop and you will be using the global variable, which then will be visible in your Draw function

here is the code:

That code is NOT properly posted. You are supposed to press the icon with the </> symbol on it, not type the letters.

// HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)

I have no idea what this comment is trying to say. A0 is NOT 9 no matter how much you wish it to be.

The analog pin number is 0, NOT A0. A0 is the number of the pin when you want it to be a digital pin. While that will work, look like you know what you are doing, and use 0.

int nowValue = analogValue;

Useless.

What makes that statement particularly odious is the fact that it hides the global variable of the same name. The local nowValue goes out of scope, and the global, unchanged, value gets printed.

Thanks for the replies although i feel like ive been told off !
This was my first post, i've been trying to learn how to do this myself, this is the 1st bit of code iv'e written in 30 years (Z80 assembler !) and i wasn't sure how to post the code correctly.

The A0 = 9 simply refers to the oled data pin which in my case connects to digital 9 on the Uno board if that makes sense.

I will do some more studying on variables, before asking again.

Thanks.

See - Paul has posted 71373 posts - out of those 32768 where for variable scope, so he lost a bit of patience when explaining what's wrong sometimes :slight_smile:

I felt mine was no so didactic... though... but with written communication it is tough to get the intent.

Just don't take it personally - keep cranking, that's the best attitude.

Now if you could modify your first post and add the [code]..your code here ...[/code] tags around your code, that would look grant!