#include <SPI.h> //for IIC -Inter intergrated circuit
#include <Wire.h>
#include <Adafruit_GFX.h> // for graphic
#include <Adafruit_SSD1306.h> //for oled screen
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// no need to change, always as default
#define LOGO_HEIGHT 32
#define LOGO_WIDTH 32
// 'DINO', 32x32px
//https://javl.github.io/image2cpp/
int val = 0; //
int cnt = 0;
int prev_val = 0;
bool mode = false;
unsigned long dur_t, start_t;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
pinMode(3, INPUT_PULLUP);
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3c, safety precheck
Serial.println(F("SSD1306 allocation failed"));
while(true){} // Don't proceed, loop forever
}
dht.begin();
display.clearDisplay();
delay(300);
}
void loop() {
while(mode == true){// DYNAMIC DISPLAY
if(digitalRead(3) == LOW){
dur_t = counting(3);
if(dur_t > 100){
mode = !mode;
cnt = 1;
display.clearDisplay();
break;
}
}
float h = dht.readHumidity();
float t = dht.readTemperature();
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // set position
display.print("Temp(C): ");
display.setCursor(70, 0);
display.print(t);
display.fillRect(10, 30, 0.5*t + 20 , 10, SSD1306_INVERSE);
//when temperature = minimum = -40, width = 0
//when temperature = maximum = 80, width = 60
display.setCursor(0, 10); // set position
display.print("Humid(%): ");
display.setCursor(70, 10);
display.print(h);
display.fillRect(10, 45, 0.6*h , 10, SSD1306_INVERSE);
//when humidity = minimum = 0, width = 0
//when humidity = maximum = 100, width = 60.
display.display();
delay(1000);
}
while(mode == false){//continuous diagram
if(digitalRead(3) == LOW){
dur_t = counting(3);
if(dur_t > 100){
mode = !mode;
display.clearDisplay();
break;
}
}
cnt++;
if(cnt > 127){
cnt = 1;
display.clearDisplay();
}
//float h = dht.readHumidity();
float t = dht.readTemperature();
t = map(t, -40, 80, 63, 0);
display.drawLine(0, 63, 127, 63, SSD1306_WHITE);// DRAW x axis
display.drawLine(0, 0, 0, 63, SSD1306_WHITE);// DRAW y axis
//display.drawPixel(cnt, t, SSD1306_WHITE);
display.drawLine(cnt - 1, prev_val, cnt, t, SSD1306_WHITE);//
prev_val = t;
display.display();
//Serial.println(t);
delay(1000);
}
}
unsigned long counting(int pin){
start_t = millis();
while(digitalRead(pin) == LOW){
//do nothing
}
return millis() - start_t;
}