#include "SparkFun_Si7021_Breakout_Library.h"
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include <EEPROM.h>
//Variables and pin settings for temp sensor:
//Temperature
float humidity = 0;
float tempf = 0;
int power = A3;
int GND = A2;
float previousHigh = 0; //= EEPROM.read(0);
float previousLow = 0; //= EEPROM.read(9);
float a = 1;
int hourAgo = 0;
int twoAgo = 0;
int threeAgo = 0;
int fourAgo = 0;
int fiveAgo = 0;
int sixAgo = 0;
int sevenAgo = 0;
int eightAgo = 0;
//Create Instance of HTU21D or SI7021 temp and humidity sensor and MPL3115A2 barrometric sensor
Weather sensor;
//Setup OLED Screen
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 128 // OLED display height, in pixels
// You can use any (4 or) 5 pins
#define SCLK_PIN 13
#define MOSI_PIN 11
#define DC_PIN 7
#define CS_PIN 10
#define RST_PIN 8
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
Adafruit_SSD1351 tft = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, CS_PIN, DC_PIN, MOSI_PIN, SCLK_PIN, RST_PIN);
void setup()
{
// open serial over USB at 9600 baud
Serial.begin(9600);
//Set temp pins as output and set one high, one low to create 3.3v and GND connection
pinMode(power, OUTPUT);
pinMode(GND, OUTPUT);
digitalWrite(power, HIGH);
digitalWrite(GND, LOW);
//Initialize the I2C sensors and ping them
sensor.begin();
//Initialize the OLED Screen
tft.begin();
//Clear the OLED Screen
tft.fillScreen(BLACK);
//Set the previous high and low to the current Temp
getWeather();
previousHigh=tempf;
previousLow=tempf;
}
void loop()
{
//Get readings from all sensors
getWeather();
compareWeather();
printInfo();
clockTimer();
}
void getWeather()
{
// Measure Relative Humidity from the HTU21D or Si7021
humidity = sensor.getRH();
// Measure Temperature from the HTU21D or Si7021
tempf = sensor.getTempF();
// Temperature is measured every time RH is requested.
// It is faster, therefore, to read it from previous RH
// measurement with getTemp() instead with readTemp()
}
void compareWeather()
{
if(tempf>previousHigh){
previousHigh=tempf;
}
if(tempf<previousLow){
previousLow=tempf;
}
}
void printInfo()
{
//This part of the function prints the weather data out to the default Serial Port
// Serial.print("Temp:");
// Serial.print(tempf);
// Serial.print("F, ");
// Serial.print("Humidity:");
// Serial.print(humidity);
// Serial.println("%");
// Serial.print("Previous High:");
// Serial.print(previousHigh);
// Serial.println("F");
// Serial.print("Previous Low:");
// Serial.print(previousLow);
// Serial.println("F");
//This part of the function sends info to the OLED screen
tft.setCursor(0, 0);
//If it's over 80 degrees the text color is red, under 50 it's blue, in between it's green.
if(tempf < 80){
tft.setTextColor(GREEN, BLACK);
} else {
tft.setTextColor(RED, BLACK);
}
if(tempf < 50){
tft.setTextColor(BLUE, BLACK);
}
//Blank Line
tft.setTextSize(1);
tft.println();
//Current Temp
tft.setTextSize(1);
tft.println("Current Temp: ");
tft.setTextSize(3);
tft.print(tempf);
tft.println("F");
}
void printPastTemp(){
tft.fillRect(0, 50, 128, 9, BLACK);
tft.fillRect(0, 50, hourAgo, 9, YELLOW);
tft.setTextSize(1);
tft.setTextColor(BLACK, YELLOW);
tft.setCursor(2, 51);
tft.print(hourAgo);
tft.print("F");
tft.fillRect(0, 60, 128, 9, BLACK);
tft.fillRect(0, 60, twoAgo, 9, YELLOW);
tft.setTextSize(1);
tft.setTextColor(BLACK, YELLOW);
tft.setCursor(2, 61);
tft.print(twoAgo);
tft.print("F");
tft.fillRect(0, 70, 128, 9, BLACK);
tft.fillRect(0, 70, threeAgo, 9, YELLOW);
tft.setTextSize(1);
tft.setTextColor(BLACK, YELLOW);
tft.setCursor(2, 71);
tft.print(threeAgo);
tft.print("F");
tft.fillRect(0, 80, 128, 9, BLACK);
tft.fillRect(0, 80, fourAgo, 9, YELLOW);
tft.setTextSize(1);
tft.setTextColor(BLACK, YELLOW);
tft.setCursor(2, 81);
tft.print(fourAgo);
tft.print("F");
tft.fillRect(0, 90, 128, 9, BLACK);
tft.fillRect(0, 90, fiveAgo, 9, YELLOW);
tft.setTextSize(1);
tft.setTextColor(BLACK, YELLOW);
tft.setCursor(2, 91);
tft.print(fiveAgo);
tft.print("F");
tft.fillRect(0, 100, 128, 9, BLACK);
tft.fillRect(0, 100, sixAgo, 9, YELLOW);
tft.setTextSize(1);
tft.setTextColor(BLACK, YELLOW);
tft.setCursor(2, 101);
tft.print(sixAgo);
tft.print("F");
tft.fillRect(0, 110, 128, 9, BLACK);
tft.fillRect(0, 110, sevenAgo, 9, YELLOW);
tft.setTextSize(1);
tft.setTextColor(BLACK, YELLOW);
tft.setCursor(2, 110);
tft.print(sevenAgo);
tft.print("F");
tft.fillRect(0, 120, 128, 9, BLACK);
tft.fillRect(0, 120, eightAgo, 9, YELLOW);
tft.setTextSize(1);
tft.setTextColor(BLACK, YELLOW);
tft.setCursor(2, 121);
tft.print(eightAgo);
tft.print("F");
}
void clockTimer(){
//Add time to timer
a = a+1.19;
//a = a+1200;
//Every ~hour shift the values
if (a > 3600){
a=0;
eightAgo=sevenAgo;
sevenAgo=sixAgo;
sixAgo=fiveAgo;
fiveAgo=fourAgo;
fourAgo=threeAgo;
threeAgo=twoAgo;
twoAgo=hourAgo;
hourAgo=tempf;
printPastTemp();
}
}