Manage Scopes in Arduino-Sketch

Hi,

i have a simple question. I want to use a display (one of those 128x96 OLEDs with an SH1106 controller) with my Arduino Uno. I'm using the Adafruit Librarys. I got it working so far, but to integrate it into my project i need to write to the display from a function. I created 2 functions.

  • Initialize the Display
  • Clear the Display and write something new

The first function creates a reference to the display with:

Adafruit_SH1106 display(OLED_RESET);

So i can do stuff like (in the first function):

display.println("Hello, world!");

But i want to use the second function to write to the display. Obviously i need to make display global, so i put Adafruit_SH1106 display; at the top of my sketch, but that didn't do the trick.

Sooo, the Question: How do i make the display reference global?

I'm fluent in Java, but new to C/C++... so this might be a dumb question...

cheers!

Function One(initialize):

void initDisplay(){

Adafruit_SH1106 display(OLED_RESET);
display.begin(SH1106_SWITCHCAPVCC, 0x3C);
     
display.fillScreen(WHITE);
display.display();
delay(500);
     
display.clearDisplay();
display.display();
      
display.setRotation(90);
display.setTextSize(1.2);
display.setTextColor(WHITE);
}

function Two(print):

void printDisp(){
display.clearDisplay();
display.setCursor(0,0);
display.println("Hello, world!");
display.display();
}

How do i make the display reference global?

Put

Adafruit_SH1106 display(OLED_RESET);

after #including the library. this will create a global object of type Adafruit_SH1106 named display which you can use throughout your program

Great, that fixed it!!! Thank you!