// Using a DHT 22 on pin 7 and a 0.96 inch OLED display
#include <DHT.h>
#include <U8g2lib.h>
#include "RTClib.h"
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
RTC_DS1307 RTC;
//Constants
#define DHTPIN 7 // PIN for DHT 22
#define DHTTYPE DHT22 //DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
//Variabler
int count;
int chk; //counter for cursor
float hum; //Stores humidity value
float temp; //Stores temperature value
void setup(void)
{
u8g2.begin();
RTC.begin();
if (! RTC.isrunning()) {
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
void TempHum() { // write temp and hum on OLED
u8g2.clearBuffer();
u8g2.setContrast(255);
u8g2.drawRFrame(0 - chk, 0, 128, 64, 7);
u8g2.setFont( u8g2_font_helvB24_tf); // Font
u8g2.setCursor( 17 - chk, 28); // Kursor
u8g2.print(temp, 1); // Write temp
u8g2.drawCircle(80 - chk, 5, 2, U8G2_DRAW_ALL); // Make a °
u8g2.print(" C"); // Celsius
u8g2.setCursor( 17 - chk, 60); // Cursor
u8g2.print(hum, 1); // write hum
u8g2.print(" %"); // Write %
u8g2.sendBuffer();
}
void dateTime() { //Write date and time on OLED
DateTime now = RTC.now();
u8g2.clearBuffer();
u8g2.drawRFrame(0 - chk, 0, 128, 64, 7);
u8g2.setFont( u8g2_font_timB24_tn); // Font
u8g2.setCursor(5 - chk, 27); // Kursor
if (now.hour() < 10)
u8g2.print("0");
u8g2.print(now.hour(), DEC); // Write hour
u8g2.print(":");
if (now.minute() < 10)
u8g2.print("0");
u8g2.print(now.minute(), DEC); // Write hour
u8g2.print(":");
if (now.second() < 10)
u8g2.print("0");
u8g2.print(now.second(), DEC);
u8g2.setCursor( 7 - chk, 60); // Cursor
u8g2.setFont( u8g2_font_timB18_tn); // Font
u8g2.print(now.year(), DEC); // Write year
u8g2.print("-");
if (now.month() < 10)
u8g2.print("0");
u8g2.print(now.month(), DEC); // Write month
u8g2.print("-");
if (now.day() < 10)
u8g2.print("0");
u8g2.print(now.day(), DEC); // write day
u8g2.sendBuffer();
}
void loop(void)
{
chk = -128;
do { // let Temp and hum slide in
TempHum();
chk = chk + 1;
delay(1);
} while (chk < 1);
delay(3000);
hum = dht.readHumidity(); // read hum
temp = dht.readTemperature(); // read temp
chk = 0;
do { // let Temp and hum slide out
TempHum();
delay(1);
chk = chk + 1;
} while (chk < 129);
chk = -128;
do { // let time and date slide in
dateTime();
chk = chk + 1;
delay(1);
} while (chk < 0);
count = 0;
do { // split the delay of 3000 into six 500 delays to have a smoother sec display
dateTime();
delay(10);
count = count + 1;
} while (count < 30);
chk = 0;
do { // let Temp and hum slide out
dateTime();
delay(1);
chk = chk + 1;
} while (chk < 129);
chk = 0;
}
Comments
Please log in or sign up to comment.