#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#define NUMBER_OF_MENUS 5
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int tempAlarmPin = 22; //Buzzer ligado no PIN 22
#define ONE_WIRE_BUS 28 //Sensores ligados no PIN 28
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//Endereços sensores
DeviceAddress insideThermometer = { 0x28, 0x0C, 0x0D, 0xB0, 0x04, 0x00, 0x00, 0x80 };
DeviceAddress outsideThermometer = { 0x28, 0x49, 0x04, 0xB1, 0x04, 0x00, 0x00, 0x62 };
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define butRIGHT 0
#define butUP 1
#define butDOWN 2
#define butLEFT 3
#define butSELECT 4
#define butNONE 5
#define HOME 0
#define MENU_1 1
#define MENU_2 2
#define MENU_3 3
#define MENU_4 4
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return butNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return butRIGHT;
if (adc_key_in < 195) return butUP;
if (adc_key_in < 380) return butDOWN;
if (adc_key_in < 555) return butLEFT;
if (adc_key_in < 790) return butSELECT;
return butNONE; // when all others fail, return this...
}
void setup(void)
{
// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(insideThermometer, 10);
sensors.setResolution(outsideThermometer, 10);
pinMode(tempAlarmPin, OUTPUT); //Buzzer
lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.clear(); // start with a blank screen
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
lcd.print("Error");
} else {
lcd.print(tempC);
lcd.print("C");
}
}
void alarm(DeviceAddress deviceAddress){
float tempA = sensors.getTempC(deviceAddress);
if ( (tempA) >= 25) //Temp superior a 23 Graus
{
digitalWrite(tempAlarmPin, HIGH);
delay(500);
digitalWrite(tempAlarmPin, LOW);
}
else if ( (tempA) <= 20) //Temp inferior a 20 graus
{
digitalWrite(tempAlarmPin, HIGH);
delay(500);
digitalWrite(tempAlarmPin, LOW);
}
}
void updateHeader() { //isto imprime as partes estaticas do LCD.
//máquina de estados.
switch(Menu) {
case HOME: {
lcd.clear();
lcd.print("hora:");
lcd.setCursor(0,1);
lcd.print("data:");
break;
};
case MENU_1: {
lcd.clear();
lcd.print("temp aquario");
break;
};
case MENU_2: {
lcd.clear();
lcd.print("temp exterior");
break;
};
case MENU_3: {
lcd.clear();
lcd.print("distancia");
break;
};
case MENU_4: {
lcd.clear();
lcd.print("settings");
break;
}//end case
}//end switch
updateValues(); //update the non-static values
}
void updateValues() {
static unsigned int hora = 0;
switch(Menu) {
case HOME: { //tens de definir o relogio...
lcd.setCursor(7,0);
lcd.print(hora++); //impar
lcd.setCursor(7,1);
lcd.print(hora++); //par...
break;
};
case MENU_1: {
lcd.setCursor(0,1);
lcd.print(sensors.getTempC(insideThermometer));
lcd.print(" C");
break;
};
case MENU_2: {
lcd.setCursor(0,1);
lcd.print(sensors.getTempC(outsideThermometer));
lcd.print(" C");
break;
};
case MENU_3: {
lcd.setCursor(0,1);
lcd.print(hora);
lcd.print(" cm");
break;
};
case MENU_4: {
lcd.setCursor(0,1);
lcd.print("min:);
lcd.print(minima);
lcd.print("max:");
lcd.print(maxima);
break;
}//end case
}//end switch
}
char Menu = HOME;
float minima = 20.0, maxima = 25.0;
unsigned long antes = 0;
void loop(void) //se esta funç~o estiver vazia so por milagre e que algo acontecera...
{
static unsigned int button=0, previousButton = 0;
char temp = Menu;
unsigned long agora;
if ((button=read_LCD_buttons()) != previousButton){ //alguem carregou na tecla.
previousButton = button; //fazer update do ultimo botao pressionado.
if (button == butUP) {
Menu = (temp++)%NUMBER_OF_MENUS;//bounded buffer.
}
if (button == butDOWN) {
Menu = (temp--)%NUMBER_OF_MENUS;//bounded buffer.
}
updateHeader();
}//end if
//fazer update do LCD
if ((agora = millis()) - antes > 1000) { //passou um segundo
antes = agora;
updateValues();
}
}