7

(avery) #1
FORGE

initiates the display and runs a function to clear the
display of the noise that typically accompanies the
display turning on:


dht ourDHT;
Stack temperature_stack;
Adafruit_SSD1306 display(4);

void setup() {
display.begin(SSD1306_SWITCHCAPVCC, SCREENADR);
display.clearDisplay();
}

The next piece of code is all that’s needed to draw
the histogram. Thanks to Adafruit’s graphics library,
we call its display.drawLine function to draw a line
from one set of coordinates to another, and we do
this to first black out a column (the same x value)
and then to draw a white line up to the temperature
value in that column. We get the value from our
stack using our peek function.


void displayChart() {
for (int x = 0; x < MAXSTACK; x++) {
display.drawLine(x, display.height(), x,
display.height(), BLACK);
display.drawLine(x, display.height(), x,
display.height() - temperature_stack.peek(x),
WHITE);
}}

For good measure, we’re also going to add text
to show the current temperature and humidity
readings. This is just as easy as drawing a line,
although we do pull the readings directly from the
sensor rather than our stack:


// Function to display a character
void displayNum() {
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.setCursor(0, 0);
display.println(“Temp:” + String(ourDHT.
temperature) + “ Hum:” + String(ourDHT.
humidity));
}

All that’s now left to do is write the main loop
function. This simply pushes a new temperature
value onto the stack, runs both the text and
histogram generation functions, and finishes up with
the display.display() function to update the display.
We then add a delay in milliseconds to wait until
we repeat the sequence. Changing this will affect
the duration between each reading, altering the
scroll speed from seconds to hours if you so wish,


which is great if you want to monitor the change in
temperature over an entire day – try delay(86400000).
void loop() {
int chk = ourDHT.read11(DHT11_PIN);
temperature_stack.push(ourDHT.temperature);
displayChart();
displayNum();
display.display();
delay(100);
}

The code for this tutorial can be found at
git.io/vh4x9.

Below
A rear view of the
mini OLED display,
showing its four pins

Above
You need to edit the
screen driver header
to make sure it uses
the correct resolution
for your display
Free download pdf