Sie sind auf Seite 1von 6

#include <LiquidCrystal595.

h>
#include <math.h>
double Thermister(int RawADC) {
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Tem
p );
Temp = Temp - 273.15; // Converte Kelvin para Celcius
return Temp;
}
//==============================================================================
====

// Definio dos pinos dos botes, display e buzzer
#define Buzzer 6
#define bMenu A0 // Os pinos analgicos podem ser
#define bChange A1 // usados como digitais, bastando
#define bUp A2 // referenci-los por A0, A1..
#define bDown A3

#define bMenu0 90 // Valor de referncia que a
#define bChange0 91 // funo CheckButton() passa
#define bUp0 92 // indicando que um boto foi
#define bDown0 93 // solto

boolean aMenu, aChange, aUp, aDown; // Grava o ultimo valor lidos nos botes.
// Utilizado pela funo Checkbutton p/ identificar quando h uma alterao no estado do p
ino dos botes
//==============================================================================
=========================
//variaveis geral
int tempfinal;
byte pinout = 10;
int valor1 = 0;
float calc;
byte variavel; // variavel a ser alterada pelo menu
char state=1; // varivel que guarda posio atual do menu
//----------------------------------- variaveis controle de tempo
const int pinrele = 13;
int ajuste = 60;
byte segundos = 0;
long previousMillis = 0;
long interval = 1000;
byte minutos = 0;
byte horas = 0;
byte dias = 0;
byte contadias = 0;
byte pinbotao = 6;
byte valorbotao;
//--------------------------------------- variaveis buzzer e controle de agua
byte valorsensora = 0;
byte pinsensorA = 7;
float sinVal;
int toneVal;
byte pinbuzzer = 8;
LiquidCrystal595 lcd(2,3,4); // Declarao do objeto tipo lcd

//============================================== SETUP
void setup()
{
lcd.begin(16, 2); // Iniciando a biblioteca do LCD

pinMode(Buzzer, OUTPUT); // Buzzer
pinMode(bMenu, INPUT); // Botes
pinMode(bChange,INPUT);
pinMode(bUp, INPUT);
pinMode(bDown, INPUT);
pinMode(pinout, OUTPUT);
pinMode(pinrele, OUTPUT);
pinMode(pinbotao, INPUT);
pinMode(pinsensorA, INPUT);
pinMode(pinbuzzer, OUTPUT);

digitalWrite(bMenu, HIGH); // Aciona o pull-up interno
digitalWrite(bChange,HIGH); // dos botes
digitalWrite(bUp, HIGH);
digitalWrite(bDown, HIGH);
digitalWrite(pinbotao, HIGH);
digitalWrite(pinsensorA, HIGH);

}

//==============================================
//============================================== LOOP
void loop(){
unsigned long currentMillis = millis();
valorbotao = digitalRead(pinbotao);

if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;

segundos++;
}
if(segundos == ajuste){ // tempo q eu regulo as horas em segundos, si eu coloc
ar 3600 vai contar uma hora
minutos++;
segundos = 0;
}
if(minutos == 60){
horas++;
minutos = 0;
}
if(horas == 6){
contadias++;
digitalWrite(pinrele, 1);
horas = 0; //remover a barra para voltar ao normal
}
if(contadias == 4){
dias++;
contadias = 0;
}
if(valorbotao == 0){
digitalWrite(pinrele, 0);
}
//------------------------------------------------------
tempfinal = (Thermister(analogRead(5)));
//------..----..-----
valor1 = constrain(variavel, 0, 50);
calc = ((valor1 - tempfinal)*16);
int valor2 = constrain(calc, 0, 255);
if(tempfinal <= variavel){
analogWrite(pinout, valor2);
}else{
analogWrite(pinout, 0);
}
valorsensora = digitalRead(pinsensorA);
if(valorsensora == 1){
buzzer();
}

//============================================================================
===========
switch (state) { // Define checa qual tela atual

case 1: // executado quando na TELA 1
switch (CheckButton()) {
break;
case bUp:
lcd.clear(); Set_state(6); // antes de mudar de tela, necessrio limpar
o
break; // display com a funo lcd.clear()
case bDown:
lcd.clear(); Set_state(2);
break;
default: // Caso nenhum boto tenha sido apertado, ela executa a set_sta
te
Set_state(1); // mesmo assim para atualizar o display.
}
break;

case 2: // executado quando na TELA 2
switch (CheckButton()) {
break;
case bUp:
lcd.clear(); Set_state(1);
break;
case bDown:
lcd.clear(); Set_state(3);
break;
default:
Set_state(2);
}
break;

case 3: // executado quando na TELA 3
switch (CheckButton()) {
break;
case bUp:
lcd.clear(); Set_state(2);
break;
case bDown:
lcd.clear(); Set_state(4);
break;
default:
Set_state(3);
}
break;

case 4: // executado quando na TELA 4
switch (CheckButton()) {
case bMenu:
lcd.clear(); variavel++;
break;
case bChange:
lcd.clear(); variavel--;
break;
case bUp:
lcd.clear(); Set_state(3);
break;
case bDown:
lcd.clear(); Set_state(5);
break;
default:
Set_state(4);
}
break;
case 5: // executado quando na TELA 5
switch (CheckButton()) {
case bMenu:
lcd.clear(); ajuste++;
break;
case bChange:
lcd.clear(); ajuste--;
break;
case bUp:
lcd.clear(); Set_state(4);
break;
case bDown:
lcd.clear(); Set_state(6);
break;
default:
Set_state(5);
}
break;
case 6: // executado quando na TELA 4
switch (CheckButton()) {
break;
case bUp:
lcd.clear(); Set_state(5);
break;
case bDown:
lcd.clear(); Set_state(1);
break;
default:
Set_state(6);
}
break;
default: ;
}
}
//============================================== FIM da funo LOOP
//==============================================

//============================================== CheckButton
char CheckButton() {
if (aMenu!=digitalRead(bMenu)) {
aMenu=!aMenu;
if (aMenu) return bMenu0; else return bMenu;
} else
if (aChange!=digitalRead(bChange)) {
aChange=!aChange;
if (aChange) return bChange0; else return bChange;
} else
if (aUp!=digitalRead(bUp)) {
aUp=!aUp;
if (aUp) return bUp0; else return bUp;
} else
if (aDown!=digitalRead(bDown)) {
aDown=!aDown;
if (aDown) return bDown0; else return bDown;
} else
return 0;
}

//========================================================
//============================================== Set_state
void Set_state(char index) {
state = index; // Atualiza a varivel state para a nova tela
switch (state) { // verifica qual a tela atual e exibe o contedo correspondent
e
case 1: //==================== state 1
lcd.setCursor(3,0);
lcd.print("Dias ligado:");
lcd.setCursor(0,1);
lcd.print(dias , DEC); // mostra os segundos na tela
lcd.setCursor(4, 1);
lcd.print(horas, DEC);
lcd.setCursor(8, 1);
lcd.print(minutos,DEC);
lcd.print("s");
lcd.setCursor(12, 1);
lcd.print(segundos, DEC);
break;
case 2: //==================== state 2
lcd.setCursor(0,0);
lcd.print("Agua");
lcd.setCursor(0,1);
if(valorsensora == 0){
lcd.print("OK! ");
}else{
lcd.print("Faltando!");
}
break;
case 3: //==================== state 3
lcd.setCursor(0,0);
lcd.print("Temp atual");
lcd.setCursor(0,1);
lcd.print(tempfinal);
break;
case 4: //==================== state 4
lcd.setCursor(0,0);
lcd.print("Temp desejada");
lcd.setCursor(0,1);
lcd.print(valor1, DEC); // mostra o valor de "variavel"
break;
case 5: //==================== state 5
lcd.setCursor(0,0);
lcd.print("Ajuste do tempo");
lcd.setCursor(0,1);
lcd.print(ajuste, DEC); // mostra o valor de "ajuste"
break;
case 6: //==================== state 4
lcd.setCursor(0,0);
lcd.print("Humidade do ar");
lcd.setCursor(0,1);
lcd.print("aguarde..."); // mostra o valor de "variavel"
break;
default: ;
}
}
void buzzer(){
for (int x=0; x<180; x++) { // converte graus para radianos, e depois obtm o va
lor do seno
sinVal = (sin(x*(3.1416/180)));// gera uma frequncia a partir do valor do seno
toneVal = 2000+(int(sinVal*1000));
tone(pinbuzzer, toneVal);
delay(2);
}
noTone(pinbuzzer);
}

Das könnte Ihnen auch gefallen