#include <Wire.h> // For I2C communication
#include <DHT.h> // DHT Sensor library
#include <Adafruit_SSD1306.h> // Adafruit_SH1106.h
#define DHTPIN 4 // Pin which DHT pin is connected to
#define DHTTYPE DHT22 // Type of DHT sensor that is being used
#define SCREEN_HEIGHT 64 // Height of the OLED display (pixels)
#define SCREEN_WIDTH 128 // Width of the OLED display (pixels)
#define BUTTON_PIN 3 // Pin which the button is connected to
DHT dht(DHTPIN, DHTTYPE); // initalize the DHT sensor
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // Adafruit_SH1106 display(-1)
void setup() {
// put your setup code here, to run once:
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) { // if(!display.begin(sh1106_switchcapvcc, 0x3c or 0x3D)
Serial.println(F("SSD1306 allocation failed")); // print the message
for(;;); // get stuck in a infinite loop
}
pinMode(BUTTON_PIN, INPUT); // Make the pin button is connected to as input
display.display(); // show the adafruit splash screen
delay(1000); // wait a bit
display.clearDisplay(); // clear the display buffer
dht.begin(); // start the dht sensor
Serial.begin(9600); // Start serial communication
Serial.println("Initializing...");
}
void loop() {
// put your main code here, to run repeatedly:
int lastbuttonstate = LOW; // Button variable 1 set to low,
int currentbuttonstate = digitalRead(BUTTON_PIN); // Set the current button state as the state of the button pin
bool selection = 0; // boolean with values 1 or 0
float h = dht.readHumidity(); // read the dht humidity
float c = dht.readTemperature(); // read the dht temperature in celsius
float f = dht.readTemperature(false); // read the dht temperature in farenheit
float c2;
int graphxpos; // for the graph
if (currentbuttonstate != lastbuttonstate) { // if the button state is different than last logged, then
delayMicroseconds(20); // delay for jitter
if (currentbuttonstate == HIGH) { // if the button was pressed
selection = 1; // make the selection 1 (make the boolean true)
} else if (currentbuttonstate == LOW) { // if the button was not pressed
Serial.println("Button no press"); // print the button was not pressed
selection = 0; // make the selection 0 (boolean false)
}
lastbuttonstate = currentbuttonstate; // update the last button state to the current button state
}
delay(100); // slight delay
if (selection == true) { // if the button is being pressed
display.clearDisplay(); // clear display buffer
display.setCursor(0,0); // set the cursor at the top left of the screen
display.setTextSize(2); // set the text size as 2
display.setTextColor(SSD1306_WHITE); // make the text color white as no other color is available
display.println("Temp: "); // print "temp " to the screen
display.print(c); // print the temperature
display.write(0xF8); // print the degrees symbol
display.println("C");
display.setTextSize(1); // set the text size lower
display.println("Humidity: "); // print the humidity to the screen
display.print(h);
display.print("%");
display.display(); // you have to run this command after writing something to the screen
} else if (selection == false) { // if the button isnt pressed
display.clearDisplay(); // clear the display buffer
display.setCursor(0,0); // set the cursor at the top left
display.setTextSize(1); // set text size as 1
display.setTextColor(SSD1306_WHITE); // make the text white
for (;;) { // run an infinite for loop
graphxpos = graphxpos + 1; // make the graph x pos move over by 1 every time the for loop is completed
c = dht.readTemperature() / -1 * 3 + 125; // make it so that the temp is displayed properly on the screen and amplified for bigger differences
c2 = dht.readTemperature(); // for the text display of the temp just in case
display.drawLine(graphxpos,128,graphxpos,c,SSD1306_WHITE); // draw line of the graph
display.display(); // you have to run this every time u draw something on the display idk why
display.setCursor(0,0); // i have already explained this part too many times
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.print("Temp: ");
display.print(c2);
display.write(0xF8);
display.print("C");
display.display(); // same here
currentbuttonstate = digitalRead(BUTTON_PIN); // have to run this code again as we are in this for loop
if (graphxpos > 128) { // if the graph has reached the end of the screen,
Serial.println("End of screen"); // print that
display.clearDisplay(); // clear the display buffer
graphxpos = 0; // reset the graph position
}
if (currentbuttonstate == HIGH) { // if the button is pressed again
Serial.println("Opinion changed"); // print that
display.clearDisplay(); // clear buffer
graphxpos = 0; // dk why this doesnt work
return; // return to break out of the loop
}
delay(1000); // delay so that the dht sensor can read the temperature properly
display.fillRect(0,0,128,10,SSD1306_BLACK); // put a rectangle on top of the text to make it so that the text display can change
display.display(); // explained too many times
}
}
}