#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_BMP085.h>
//**********************************************
#include <DHT.h>
#define DHTPIN 11
#define DHTTYPE DHT11
DHT dht(DHTPIN,DHTTYPE);
int butPin=2;
int butVal=0;
int butValOld=0;
int butCount=0;
int butRem=0;
float tempF;
float tempC;
float hum=50;
int hReadTime=0;
int hReadInt=2000;
float humRaw[128];
float normHum[128];
float tempRaw[128];
float normTemp[128];
int lastUpdate = 0;
int updateInterval = 600;
float rawXdata[128];
int normXdata[128];
float rawYdataP[128];
int normYdataP[128];
float rawYdataH[128];
int normYdataH[128];
float rawYdataT[128];
int normYdataT[128];
float xMin = 0; //************************************
float xMax = 127;
float yMinP = 28;
float yMaxP = 31;
float yMinH=30;
float yMaxH=90;
float yMinT=60;
float yMaxT=90;
float Pt = 14;
float Pb = 63;
float Pl = 0;
float Pr = 127;
int numPoints = 128;
Adafruit_BMP085 BP;
bool BMPconnected = false;
float BarPress = 30;
float BarPressNow = 30;
float alt = 1127; //****************************
int screenWidth = 128;
int screenHeight = 64;
int oledReset = -1;
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 oled(screenWidth, screenHeight, &Wire, oledReset);
void setup() {
// put your setup code here, to run once:
pinMode(butPin,INPUT_PULLUP); //*********************
Serial.begin(9600);
BMPconnected = BP.begin();
if (BMPconnected == true) {
Serial.println("BMP180 found and Connected");
}
if (BMPconnected == false) {
Serial.println("BMP180 not found, Check Connections");
}
oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
oled.clearDisplay();
oled.display();
for (int i = 0; i < numPoints; i= i + 1) {
rawXdata[i] = i;
}
dht.begin();
delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
BarPressNow = normPress(alt);
BarPress = .95 * BarPress + .05 * BarPressNow;
tempC=BP.readTemperature(); //***********************
tempF=tempC*9/5 +32;
if (millis()-hReadTime>hReadInt){
hum=dht.readHumidity();
Serial.println(hum);
hReadTime=millis();
}
if (millis() - lastUpdate >= updateInterval) {
for (int i = 0; i < numPoints - 1; i = i + 1) {
rawYdataP[i] = rawYdataP[i + 1];
rawYdataT[i] = rawYdataT[i + 1];
rawYdataH[i] = rawYdataH[i + 1];
}
lastUpdate=millis();
rawYdataP[numPoints-1]=BarPress;
rawYdataT[numPoints-1]=tempF;
rawYdataH[numPoints-1]=hum;
//normData();
//oled.clearDisplay();
//plotGraph();
}
butVal=digitalRead(butPin);
if (butVal==0 && butValOld==1){
butCount=butCount+1;
butRem=butCount%4;
}
butValOld=butVal;
if (butRem==0){
oled.clearDisplay();
oled.setCursor(0, 0);
oled.setTextColor(WHITE);
oled.setTextSize(2);
oled.print(tempF);
oled.println(" F");
oled.print(hum);
oled.println(" %Hum");
oled.print(BarPress);
oled.println(" InHG");
oled.display();
}
if (butRem==1){
oled.clearDisplay();
oled.setCursor(0, 0);
oled.setTextColor(WHITE);
oled.setTextSize(2);
oled.print(BarPress);
oled.setTextSize(1);
oled.println(" InHG");
normDataP(); //***********************
plotPress(); //***********************
oled.display();
delay(.1);
}
if (butRem==2){
oled.clearDisplay();
oled.setCursor(0, 0);
oled.setTextColor(WHITE);
oled.setTextSize(2);
oled.print(tempF);
oled.setTextSize(1);
oled.println(" F");
normDataT(); //***********************
plotTemp(); //***********************
oled.display();
delay(.1);
}
if (butRem==3){
oled.clearDisplay();
oled.setCursor(0, 0);
oled.setTextColor(WHITE);
oled.setTextSize(2);
oled.print(hum);
oled.setTextSize(1);
oled.println(" %HUM");
normDataH(); //***********************
plotHum(); //***********************
oled.display();
delay(.1);
}
}
float normPress(float elev) {
float BPRaw;
float BPNorm;
float T0 = 288.15;
float L = .0065;
float R = 8.3144598;
float M = .0289644;
float g = 9.8;
float c = M * g / (R * L);
BPRaw = BP.readPressure() * .0002953;
BPNorm = BPRaw / pow(1 - L * elev / T0, c);
return BPNorm;
}
void normDataP() {
for (int i = 0; i < numPoints; i = i + 1) {
normYdataP[i] = (Pb - Pt) / (yMinP - yMaxP) * (rawYdataP[i] - yMaxP) + Pt;
normXdata[i] = (Pr - Pl) / (xMax - xMin) * (rawXdata[i] - xMin) + Pl;
}
}
void normDataT() {
for (int i = 0; i < numPoints; i = i + 1) {
normYdataT[i] = (Pb - Pt) / (yMinT - yMaxT) * (rawYdataT[i] - yMaxT) + Pt;
normXdata[i] = (Pr - Pl) / (xMax - xMin) * (rawXdata[i] - xMin) + Pl;
}
}
void normDataH() {
for (int i = 0; i < numPoints; i = i + 1) {
normYdataH[i] = (Pb - Pt) / (yMinH - yMaxH) * (rawYdataH[i] - yMaxH) + Pt;
normXdata[i] = (Pr - Pl) / (xMax - xMin) * (rawXdata[i] - xMin) + Pl;
}
}
void plotPress() { //plot Pressure
oled.drawLine(Pl, Pt, Pl, Pb, WHITE);
oled.drawLine(Pl, Pb, Pr, Pb, WHITE);
int MID = (Pb - Pt) / (yMinP - yMaxP) * (29.92 - yMaxP) + Pt; //**********
oled.drawLine(Pl, MID, Pr, MID, WHITE);
for (int i = 0; i < numPoints - 1; i = i + 1) {
oled.drawLine(normXdata[i], normYdataP[i], normXdata[i + 1], normYdataP[i + 1], WHITE); //***********
}
}
void plotTemp() { //plot Pressure
oled.drawLine(Pl, Pt, Pl, Pb, WHITE);
oled.drawLine(Pl, Pb, Pr, Pb, WHITE);
//int MID = (Pb - Pt) / (yMinP - yMaxP) * (29.92 - yMaxP) + Pt; //**********
//oled.drawLine(Pl, MID, Pr, MID, WHITE);
for (int i = 0; i < numPoints - 1; i = i + 1) {
oled.drawLine(normXdata[i], normYdataT[i], normXdata[i + 1], normYdataT[i + 1], WHITE); //***********
}
}
void plotHum() { //plot Pressure
oled.drawLine(Pl, Pt, Pl, Pb, WHITE);
oled.drawLine(Pl, Pb, Pr, Pb, WHITE);
//int MID = (Pb - Pt) / (yMinP - yMaxP) * (29.92 - yMaxP) + Pt; //**********
//oled.drawLine(Pl, MID, Pr, MID, WHITE);
for (int i = 0; i < numPoints - 1; i = i + 1) {
oled.drawLine(normXdata[i], normYdataH[i], normXdata[i + 1], normYdataH[i + 1], WHITE); //***********
}
}