#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define SENSOR_TEMP_1 4
#define SENSOR_TEMP_2 2
#define SENSOR_MOV 12
#define LED 13
int temp_1 = 0;
int temp_2 = 0;
double temp = 0;
double voltage = 0;
double Rt = 0;
double tempK = 0;
double tempC = 0;
int mov = 0;
int movState = LOW; // we start, assuming no motion detected
String interp_mov = "Motion not detected";
String ar_cond = "Ar condicionado OFF";
String interp_temp = "";
String temp_arcond = "";
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
digitalWrite(LED, 0);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
}
void loop() {
display.clearDisplay();
display.setCursor(0,0);
temperature(); //10 em 10 min medir a temperatura
motion(); //sp que ha 1 mov garantir q o ar cond funciona durante + 30min
//criar um contador para o ar cond ficar igado durante 30min e so dps verificar o mov outra vez
if(tempC >= 25){
interp_temp = "calor";
Serial.println(interp_temp);
}
else if(tempC > 15 && tempC < 25){
interp_temp = "normal";
Serial.println(interp_temp);
}
else if(tempC <= 15){
interp_temp = "frio";
Serial.println(interp_temp);
}
if(interp_mov == "Motion detected!")
ar_cond = "Ar condicionado ON";
else
ar_cond = "Ar condicionado OFF";
if(ar_cond == "Ar condicionado ON" && interp_temp == "frio" )
temp_arcond = "Aumentar a temperatura";
else if(ar_cond == "Ar condicionado ON" && interp_temp == "calor" )
temp_arcond = "Diminuir a temperatura";
else
temp_arcond = "";
display.print("{ Temperatura:");
display.println(tempC);
display.println(interp_temp);
display.print("movimento:");
display.println(interp_mov);
display.print("Ar condicionado:");
display.println(ar_cond);
display.print(temp_arcond);
display.println("}");
display.display();
delay(5000);
}
double temperature(){
temp_1 = analogRead(SENSOR_TEMP_1);
temp_2 = analogRead(SENSOR_TEMP_2); //read ADC pin
temp = (temp_1+temp_2)/2;
voltage = (float)temp / 4095.0 * 3.3; // calculate voltage
Rt = 10 * voltage / (3.3 - voltage); //calculate resistance value of thermistor
tempK = 1 / (1 / (273.15 + 25) + log(Rt / 10) / 3950.0); //calculate temperature (Kelvin)
tempC = tempK - 273.15; //calculate temperature (Celsius)
Serial.printf("tempC: %.2f\n", tempC);
return (tempC);
}
String motion(){
mov = analogRead(SENSOR_MOV);
if (mov == 4095) { // check if the input is HIGH
digitalWrite(LED, HIGH); // turn LED ON
if (movState == LOW) {
// we have just turned on
interp_mov = "Motion detected!";
Serial.println(interp_mov);
// We only want to print on the output change, not state
movState = HIGH;
}
} else {
digitalWrite(LED, LOW); // turn LED OFF
if (movState == HIGH) {
// we have just turned of
interp_mov = "Motion not detected!";
Serial.println(interp_mov);
// We only want to print on the output change, not state
movState = LOW;
}
}
return interp_mov;
}