#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);
}