#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>


#define room_sensor 0
#define outside_sensor 1
#define motion_sensor 2

#define pb_up         2
#define pb_down      3
#define mode_sw  4
#define control_power 5

#define  cooler        0
#define  heater       1


int set_temp = 20;
int room_temp ;
int outside_temp ;

ISR(INT0_vect)
{
  set_temp ++;

}

ISR(INT1_vect)
{
  set_temp --;
}


ISR(TIMER1_COMPA_vect) {

  if (!(PINC & (1 << motion_sensor))) {

    Serial.println("Empty room");
    Serial.print("Outside temperature is: ");
    int outdide_temp = read_outside_temp();
    Serial.println(outdide_temp);
    OCR0B = 1;
    PORTB &= ~ (1 << cooler);
    PORTB &= ~ (1 << heater);
  }

  PORTB ^= (1 << 2);  // Toggle the state of pin 2 of PORTB

}

int read_room_temp() {
  ADMUX = 0b01100000;
  ADCSRA = 0b10000000;
  ADCSRA = ADCSRA | (1 << ADSC);
  while (ADCSRA & (1 << ADSC));
  return ADCH * 50 / 255;
}

int read_outside_temp() {
  ADMUX = 0b01100001;
  ADCSRA = 0b10000000;
  ADCSRA = ADCSRA | (1 << ADSC);
  while (ADCSRA & (1 << ADSC));
  return ADCH * 50 / 255;
}


void setup() {
  Serial.begin(9600);
  Serial.println("Air Conditionning controller");

  DDRC = 0b00000000;
  DDRD = 0b11100011;
  DDRB = 0b11111111;
  PORTD = 0b00011100; //internal pull up
  TCCR0A |= (1 << COM0B1) | (1 << WGM01) | (1 << WGM00);
  TCCR0B |= (1 << CS00);
  OCR0B = 1;

  TCCR1A = 0;
  TCCR1B = (1 << WGM12) | (1 << CS12) | (1 << CS10);
  OCR1A = 32768;
  TIMSK1 |= (1 << OCIE1A);


  EICRA = 0b00001010;  // falling endge
  EIMSK |= (1 << INT0) | (1 << INT1);
  sei();
  delay(1000);

}


void loop (void) {

  if (PINC & (1 << motion_sensor)) {


    room_temp  = read_room_temp();
    Serial.print("Room temperature is:");
    Serial.println(room_temp);
    Serial.print("The set temperaure is: ");
    Serial.println(set_temp);

    if (PIND & (1 << mode_sw)) {
      Serial.println("Heater mode");
      PORTB |= (1 << heater);
      PORTB &= ~ (1 << cooler);


      if (set_temp > room_temp + 10)  OCR0B = 255;
      else if (set_temp > room_temp + 8)  OCR0B = 127;
      else if (set_temp > room_temp + 6)  OCR0B = 100;
      else if (set_temp > room_temp + 4)  OCR0B = 50;
      else if (set_temp > room_temp + 2)  OCR0B = 20;
      else OCR0B = 1;
    }
    else {
      Serial.println("Cooler mode");
      PORTB |= (1 << cooler);
      PORTB &= ~ (1 << heater);

      if (set_temp < room_temp - 10)  OCR0B = 255;
      else if (set_temp < room_temp - 8)  OCR0B = 127;
      else if (set_temp < room_temp - 6)  OCR0B = 100;
      else if (set_temp < room_temp - 4)  OCR0B = 50;
      else if (set_temp < room_temp - 2)  OCR0B = 20;
      else OCR0B = 1;
    }
  }



  delay(1000);


}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
pot1:VCC
pot1:SIG
pot1:GND
pot2:VCC
pot2:SIG
pot2:GND
led1:A
led1:C
pir1:VCC
pir1:OUT
pir1:GND
led2:A
led2:C
led3:A
led3:C
sw1:1
sw1:2
sw1:3
led4:A
led4:C