// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 9, 10);

// Define the pins being used on the Arduino.
#define TEMP_PIN 	A0
#define FAN_PIN		 6

// Macro to calculate Temperature based on thermistor resistance. 
#define GET_TEMP()		((((analogRead(TEMP_PIN) * 5.0) / 1024.0) - 0.5) * 100);

// Macro to set Fan Speed from 0% to 100%.
#define FAN_SPEED(i)	analogWrite(FAN_PIN, map(i, 0, 100, 0, 255))

// Store the temperature in this variable.
int TempC = 0;
int speedM = 0;
int speedA = 0;
int i = 0;
int a[9];
int min = 20;
int max = 30;
int mode = 3;
  
int pinSensorPIR = 13;
int PIRvalue = 0;

int Potensiovalue = 0;

void array(){
  for(i = 1; i<10; i++){
    a[i] = min + i;
  } 
}

//Manual fan and speed
void manualfan(){
  FAN_SPEED(speedM); 
}

//automatic fan and speed
void autofan(){   // Set boundaries for when the fan should turn on/off.

  if (TempC <= min) {							// Less than 21 C:
    speedA = 0;
 	FAN_SPEED(speedA);							// Fan Fully Off.  
  }  
  else if (TempC > max){ 					// Greater than 29 C:
    speedA = 100;
  	FAN_SPEED(speedA);							// Fan Fully On. 
  }
  else if (TempC > min && TempC < max ){ 
    for( i = 1 ; i < 10 ; i++ ){
      if (TempC == a[i]){
        speedA = 10 + (i - 1)*10;
        FAN_SPEED(speedA);
        break;
      }
    }
  }
}

void modes(){
  mode = mode + 1;
  if ( mode >= 3 ){
  	mode = 1;
  }
  lcd.clear();
}

void lcdclear(){
  lcd.clear();
}

void setup() {
  Serial.begin(9600);
  
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  
  // Declare which pins are inputs and outputs.
  pinMode(TEMP_PIN, INPUT);
  pinMode(FAN_PIN, OUTPUT);
  
  // Button
  attachInterrupt(digitalPinToInterrupt(3), modes, FALLING);
  attachInterrupt(digitalPinToInterrupt(2), lcdclear, FALLING);
  
  //Potentiometer
  pinMode(A1, INPUT);

}

void loop() {
  array();
  // Get the current temperature.
  TempC = GET_TEMP();
  
  //set the PIRvalue
  PIRvalue = digitalRead(pinSensorPIR);
  
  // Set fan mode
  if (mode == 1 && PIRvalue == 1){
    manualfan();
    
    // Read Speed
    speedM = analogRead(A1)/10;

    if ( speedM <= 0 ){
      speedM = 0;
    }
    else if (speedM >= 100){
      speedM = 100;
    }
  
    // Print a message to the LCD.
    lcd.setCursor(10, 0);
    lcd.print("Manual");
    
    // set the cursor to column 8, line 1
    lcd.setCursor(8, 1);
    // print the number of seconds since reset:
    lcd.print(speedM);
  }
  else if (mode == 2 && PIRvalue == 1) {
    autofan();
    
    // Print a message to the LCD.
    lcd.setCursor(12, 0);
    lcd.print("Auto");

    // set the cursor to column 8, line 1
    lcd.setCursor(8, 1);
    
    // print the number of seconds since reset:
    lcd.print(speedA);
  }
  
  // Print a message to the LCD.
  lcd.setCursor(0, 0);
  lcd.print("Temp :");
  lcd.setCursor(0, 1);
  lcd.print("Speed :");
  
  // set the cursor to column 7, line 0
  lcd.setCursor(7, 0);
  
  // print the number of seconds since reset:
  lcd.print(TempC);
 
  Serial.println(mode);
  Serial.println(speedM);
}



