#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "Wire.h"
#define LED1 11 // LED when humidity drops
#define LED2 9 // LED when temperature drops
#define LED3 10 // LED when pressure drops
Adafruit_BME280 Sensor;
float temp; // Sensor temperature value
float tempold;
float pressure; // Sensor pressure value
float pressureold;
float hum; //Sensor humidity value
float humold;
float DisplayTemp; // Sensor temperature value
float DisplayHum;
float DisplayPress;
unsigned long previousMillis = 0;
const int interval = 2000; // interval for time between readings
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
void setup() {
Serial.begin(115200);
Sensor.begin(0x76);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay();
display.setTextSize(3);
display.setTextColor(WHITE);
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
delay (2000);
digitalWrite(LED1, LOW); // turns LED1 off
digitalWrite(LED2, LOW); // turns LED2 off
digitalWrite(LED3, LOW); // turns LED3 off
display.clearDisplay();
display.setCursor(0, 0);
display.print(1.8 * Sensor.readTemperature() + 32, 0); display.println("F");
display.setCursor(70, 0);
display.print(Sensor.readHumidity(), 0); display.println("%");
display.setCursor(0, 40);
display.print(Sensor.readPressure() / 100.0F, 0); display.println("hPa");
display.display();
Serial.println("Ready..");
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
//read sensors
temp = Sensor.readTemperature();
pressure = Sensor.readPressure();
hum = Sensor.readHumidity();
//save raw reading for display later..
DisplayTemp = temp;
DisplayPress = pressure;
DisplayHum = hum;
//check for bad read..
if (isnan(temp) || isnan(pressure) || isnan(hum)) return;
pressure = (pressure * 0.0100);
hum = (hum * 0.1000);
/*
// ADDED FOR DEBUG
hum = random (50, 70);
pressure = random (100, 150);
temp = random (30, 40);
*/
//do we need to update display..
bool UpdateDisplay = false;
//tempold = tempnow; // compares last temperature measurement with the new one
// tempnow = temp; // this maps tempnow to the values of the temperature sensor
// if (tempold > temp) { //if temperature drops, turn on LED2
if (Deviation(tempold, temp) > 1) { //if temperature drops, turn on LED2
digitalWrite(LED2, HIGH);
}
else { //if temperature is unchanged turn off LED2
digitalWrite(LED2, LOW);
}
if (tempold != temp){
UpdateDisplay = true;
tempold = temp;
}
//pressureold = pressurenow; // compares last pressure measurement with the new one
//pressurenow = pressure; // this maps pressurenow to the values of the pressure sensor
// if (pressureold > pressure) { // if pressure changes turn on LED3
if (Deviation(pressureold, pressure) >1) { // if pressure changes turn on LED3
digitalWrite(LED3, HIGH);
Serial.print(pressureold);Serial.print(">");
Serial.println(pressure);
}
else { // if pressure unchanged, keep LED3 off
digitalWrite(LED3, LOW);
}
if (pressureold != pressure){
UpdateDisplay = true;
pressureold = pressure;
}
// humdown = humnow; // compares last humidity measurement with the new one
// humnow = hum; // this maps humnow to the values of the humidity sensor
// if (humold < hum) { //if humidity changes turn on LED1
if (Deviation(humold , hum) < 0) { //if humidity changes turn on LED1
digitalWrite(LED1, HIGH);
}
else { //if humidity is unchanged, keep LED1 off
digitalWrite(LED1, LOW);
}
if (humold != hum){
UpdateDisplay = true;
humold = hum;
}
if (UpdateDisplay)
{
display.clearDisplay();
display.setCursor(0, 0);
display.print(1.8 * DisplayTemp + 32, 0); display.println("F");
display.setCursor(70, 0);
display.print(DisplayHum, 0); display.println("%");
display.setCursor(0, 40);
display.print(DisplayPress / 100.0F, 0); display.println("hPa");
display.display();
}
}
}
int Deviation(float a, float b){
int result = a - b;
return result;
}