//Library
#include <DHT.h>; //Library for temp/hum sensor
#include <LiquidCrystal_I2C.h> // Library for LCD
//#include <vector.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows


//Constants
#define DHTPIN 7     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

byte DegreeChar[8] = {
	0b00010,
	0b00101,
	0b00010,
	0b00000,
	0b00000,
	0b00000,
	0b00000,
	0b00000
};

byte ArrowupChar[8] = {
  0b00100,
  0b01110,
  0b11111,
  0b00100,
  0b00100,
  0b00100,
  0b00100,
  0b00100
};

byte ArrowdownChar[8] = {
  0b00100,
  0b00100,
  0b00100,
  0b00100,
  0b00100,
  0b11111,
  0b01110,
  0b00100
};

byte ConstantChar[8] = {
	0b00000,
	0b00000,
	0b00000,
	0b11111,
	0b11111,
	0b00000,
	0b00000,
	0b00000
};

//Variables
float hum;  //Stores humidity value
float temp; //Stores temperature value
int storageT[] = {0,0,0,0};//stores past temperature values
int storageH[] = {0,0,0,0};
int delta;
int num = 0;

void setup()
{
  Serial.begin(9600);
  dht.begin();
  lcd.init(); //initialize the lcd
  lcd.backlight(); //open the backlight 

  lcd.setCursor(4, 0);         // move cursor to   (4, 0)
  lcd.print("Welcome");        // print message at (0, 0)
  lcd.setCursor(0, 1);         // move cursor to   (2, 1)
  lcd.print("Temp+Hum Project"); // print message at (2, 1)
  delay(1000);
  lcd.clear();
  lcd.createChar(0, DegreeChar);
  lcd.createChar(1,ArrowupChar);
  lcd.createChar(2,ArrowdownChar);
  lcd.createChar(3,ConstantChar);
  PrintWorkingScreen();
}

void loop()
{
    //Read data and store it to variables hum and temp
    hum = dht.readHumidity();
    temp = dht.readTemperature();
    //Print temperature and humidity values to serial monitor
    //Serial.print("Humidity: ");
    //Serial.print(hum);
    //Serial.print(" %, Temp: ");
    //Serial.print(temp);
    //Serial.println(" Celsius");
    storageT[3] = temp; //num is a counter to store values
    storageH[3] = hum;
    
    Getlast3T(storageT);
    Getlast3H(storageH);

    Serial.print("Counter ");
    Serial.print(num);
    Serial.print(", \t StorageT: ");
    Serial.print(storageT[0]);
    Serial.print(",");
    Serial.print(storageT[1]);
    Serial.print(",");
    Serial.print(storageT[2]);
    Serial.print(",");
    Serial.print(storageT[3]);
    Serial.print(", \t StorageH: ");
    Serial.print(storageH[0]);
    Serial.print(",");
    Serial.print(storageH[1]);
    Serial.print(",");
    Serial.print(storageH[2]);
    Serial.print(",");
    Serial.println(storageH[3]);

   Serial.print("Delta(storageT): ");
   Serial.print(Delta(storageT));
   Serial.print(" \t Delta(storageH): ");
   Serial.print(Delta(storageH));
   
    if (Delta(storageT) > 0){
      lcd.setCursor(15, 0);
      lcd.write((byte)1);
    }
    else if (Delta(storageT) < 0){
      lcd.setCursor(15, 0);
      lcd.write((byte)2);
    }
    else if(Delta(storageT) == 0){
      lcd.setCursor(15, 0);
      lcd.write((byte)3);
    }

    if (Delta(storageH) > 0){
      lcd.setCursor(15, 1);
      lcd.write((byte)1);
    }
    else if (Delta(storageH) < 0){
      lcd.setCursor(15, 1);
      lcd.write((byte)2);
    }
    else if(Delta(storageH) == 0){
      lcd.setCursor(15, 1);
      lcd.write((byte)3);
    }

    num += 1;
    if (num == 3){
     num = 0;
    }
    


    //Print LCD screen text
    lcd.setCursor(6,0);
    lcd.print(temp,1);
    lcd.setCursor(5,1);
    lcd.print(hum,1);

    delay(1000); //Delay of loop
}

void PrintWorkingScreen(){
    lcd.setCursor(0,0);
    lcd.print("Temp:       C");
    lcd.setCursor(11, 0);
    lcd.write((byte)0); //Print celcuis character
    lcd.setCursor(0,1);
    lcd.print("Hum:      %");
}

int Getlast3T(int x[]){
  for (int i = 0; i < 3; i++){
    int u = i + 1;
    storageT[i] = x[u];
    }
    //return newstorage;
  storageT[3]=100; //have to add this just to not confuse values since the last index needs to be updated with the new value next tic.
}
int Getlast3H(int x[]){
  for (int i = 0; i < 3; i++){
    int u = i + 1;
    storageH[i] = x[u];
    }
    //return newstorage;
  storageH[3]=100; //have to add this just to not confuse values since the last index needs to be updated with the new value next tic.
}

int Delta(int x[]){
  int delta = x[2] - x[1];
  return delta;
}



// TO DO:
// - Set stored values to floats instead of int
FPS: 0
Power: 0.00W