http://i.imgur.com/maLKaf1.jpg#include #include "Wire.h" #include #include #include #include #include #include #include #include #define ONE_WIRE_BUS 2 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); DeviceAddress sensor1; Time t; DS3231 rtc(SDA, SCL); LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); Servo motorAux; int RECV_PIN = 8; // Pino 8: controle iR IRrecv irrecv(RECV_PIN); decode_results results; float valorIR; int valorSensor; int valorS; long previousLCDMillis = 0; // for LCD screen update long lcdInterval = 4000; int screen = 0; int screenMax = 1; int porta_rele1 = 3; //luz Amarela int porta_rele2 = 4; // luz Branca bool screenChanged = true; // initially we have a new screen, by definition #define TEMPERATURA 0 #define HORARIO 1 int symbolFds; int symbolPopup; float tempC; byte customChar[8] = { 0b01100, 0b10010, 0b10010, 0b01100, 0b00000, 0b00000, 0b00000, 0b00000 }; byte customLampSymbol[8] = { 0b10101, 0b00000, 0b01110, 0b10001, 0b10001, 0b01110, 0b01010, 0b00100 }; byte customSabado[8] = { 0b00000, 0b01100, 0b10011, 0b01100, 0b10011, 0b01100, 0b10011, 0b00000 }; void setup() { Serial.begin(9600); Wire.begin(); rtc.begin(); sensors.begin(); //Serial.println("Sensor is Starting Up"); irrecv.enableIRIn(); // Starta controle iR motorAux.attach(5); motorAux.write(0); lcd.begin(16, 2); lcd.createChar(1, customChar); lcd.createChar(2,customLampSymbol); lcd.createChar(8,customSabado); pinMode(porta_rele1, OUTPUT); pinMode(porta_rele2, OUTPUT); digitalWrite(porta_rele1, LOW); digitalWrite(porta_rele2, LOW); if (!sensors.getAddress(sensor1, 0)) Serial.println("Sensores nao encontrados !"); // Mostra o endereco do sensor encontrado no barramento //Serial.print("Endereco sensor: "); mostra_endereco_sensor(sensor1); //Serial.println(); //Serial.println(); showWelcome(); mostra_endereco_sensor(sensor1); } void mostra_endereco_sensor(DeviceAddress deviceAddress) { for (uint8_t i = 0; i < 8; i++) { // Adiciona zeros se necessário if (deviceAddress[i] < 16) Serial.print("0"); Serial.print(deviceAddress[i], HEX); } } void loop() { acionaLuz(); tempoAlimentacao(); if (irrecv.decode(&results)) { irControlador(); irrecv.resume(); // Receive the next value } sensors.requestTemperatures(); float tempC = sensors.getTempC(sensor1); t = rtc.getTime(); unsigned long currentLCDMillis = millis(); // MUST WE SWITCH SCREEN? if(currentLCDMillis - previousLCDMillis > lcdInterval) // save the last time you changed the display { previousLCDMillis = currentLCDMillis; screen++; if (screen > screenMax) screen = 0; // all screens done? => start over screenChanged = true; } // debug Serial.println(screen); // DISPLAY CURRENT SCREEN if (screenChanged) // only update the screen if the screen is changed. { screenChanged = false; // reset for next iteration switch(screen) { case TEMPERATURA: showTemperature(tempC); break; case HORARIO: showHora(50); break; default: // cannot happen -> showError() ? break; } } } void irControlador() { valorIR = (results.value); //Serial.print(valorIR); if (irrecv.decode(&results)) { //Serial.println(results.value, HEX); /*if(valorIR == 0xFFB04F) { servo.write(180); //Serial.print("Botao # pressionadp"); delay(3000); servo.write(0); //irrecv.resume(); // Receive the next value }*/ if(valorIR == 0xFF6897) { motorAux.write(180); delay(3000); motorAux.write(0); //irrecv.resume(); //Serial.print("Botao * pressionado"); } if(valorIR == 0xFFA25D) //Numero 1 { //irrecv.resume(); valorSensor = digitalRead(porta_rele2); if(valorSensor==LOW) { digitalWrite(porta_rele1, LOW); //teste digitalWrite(porta_rele2, HIGH); //Liga luz verde } if(valorSensor==HIGH) { digitalWrite(porta_rele1, LOW); digitalWrite(porta_rele2, LOW); //Desliga luz verde } } if(valorIR == 0xFFE21D) // Numero 3 { irrecv.resume(); valorS = digitalRead(porta_rele1); if(valorS==LOW) { digitalWrite(porta_rele2, LOW); //teste digitalWrite(porta_rele1, HIGH); //Liga luz rosa } if(valorS==HIGH) { digitalWrite(porta_rele2, LOW);//teste digitalWrite(porta_rele1, LOW); //Desiga luz rosa } } } } void showWelcome() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Calibrando"); lcd.setCursor(0, 1); lcd.print("sistemas..."); } void showHora(int H) { lcd.clear(); if(symbolPopup==1) { lcd.setCursor(14,0); lcd.write((uint8_t)2); } if(symbolFds==1) { lcd.setCursor(11,0); lcd.write((uint8_t)8); } lcd.setCursor(0, 0); lcd.print(t.date, DEC); lcd.print("/0"); lcd.print(t.mon, DEC); lcd.print("/17"); lcd.setCursor(0, 1); lcd.print(t.hour, DEC); lcd.print(":"); if(t.min<10) { lcd.print("0"); } lcd.print(t.min, DEC); } void showTemperature(int T) { lcd.clear(); if(symbolPopup==1) { lcd.setCursor(14,0); //lcd.write((uint8_t)2); } lcd.setCursor(0, 0); //lcd.write((uint8_t)5); lcd.print("AMB:"); lcd.print(int(rtc.getTemp())); //lcd.print(char(223)); lcd.write((uint8_t)1); lcd.print("C"); lcd.setCursor(9,0); lcd.print("AQ:"); //lcd.write((uint8_t)3); lcd.print(T); //lcd.print(char(223)); lcd.write((uint8_t)1); lcd.print("C"); lcd.setCursor(0, 1); lcd.print("Perle V3.52b"); } void acionaLuz() { // 1 BRANCA // 2 AMARELA if((t.dow==3)||(t.dow==4)) //É fds ? { // É fds !!! Horário especial //Serial.print("É FDS "); //Serial.print(t.dow); symbolFds = 1; if(t.hour==13) //Hora de acender as luzes amarela! { digitalWrite(porta_rele2, HIGH); //Desliga rele 2 digitalWrite(porta_rele1, LOW); //Liga rele 1 symbolPopup = 1; } if(t.hour==14) { if(t.min==1) { digitalWrite(porta_rele2, LOW); digitalWrite(porta_rele1, LOW); } } if((t.hour==20)||(t.hour==21)) //Hora de acender as luzes amarela! { digitalWrite(porta_rele2, HIGH); //Desliga rele 2 digitalWrite(porta_rele1, LOW); //Liga rele 1 symbolPopup = 1; } if(t.hour==22) //Hora de acender as luzes branca { digitalWrite(porta_rele1, HIGH); //Desliga rele 1 digitalWrite(porta_rele2, LOW); //Liga rele 2 symbolPopup = 1; } if(t.hour==23) { if(t.min==1) { digitalWrite(porta_rele2, LOW); digitalWrite(porta_rele1, LOW); } } } if((t.dow<3)||(t.dow>4)) //Não é fim de semana :| { symbolFds = 0; if(t.hour==13) //Hora de acender as luzes amarela! { digitalWrite(porta_rele2, HIGH); //Liga rele 2 digitalWrite(porta_rele1, LOW); //Desliga rele 1 symbolPopup = 1; } if(t.hour==14) { if(t.min==1) { digitalWrite(porta_rele2, LOW); digitalWrite(porta_rele1, LOW); } } if(t.hour==20) //Hora de acender as luzes amarela! { digitalWrite(porta_rele2, HIGH); //Liga rele 2 digitalWrite(porta_rele1, LOW); //Desiga rele 1 symbolPopup = 1; } if(t.hour==21) //Hora de acender as luzes branca { digitalWrite(porta_rele1, HIGH); //Liga rele 1 digitalWrite(porta_rele2, LOW); //Desliga rele 2 symbolPopup = 1; } if(t.hour==22) { if(t.min==1) { digitalWrite(porta_rele2, LOW); digitalWrite(porta_rele1, LOW); } } } } void tempoAlimentacao() { if(t.hour==13) { if(t.min==30) { if(t.sec==1) { //Time to feed the fishes! motorAux.write(180); delay(1500); motorAux.write(0); delay(1000); motorAux.write(180); delay(1500); motorAux.write(0); } } } }