#include <LiquidCrystal.h> // Inclui a biblioteca do LCD
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // Pinos do LCD
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#include <Servo.h>
Servo myservo;
#define Y1 13
#define Y2 A5
#define buzzer A3
#define bt_esq 6
#define bt_ent 7
#define bt_dir 8
#define sensorcoffee A0
#define sensormilk A1
void pronto(){
lcd.clear();
lcd.setCursor(2,0);
lcd.print("BEBIDA PRONTA");
lcd.setCursor(4,1);
lcd.print("OBRIGADO!");
delay(2000);
}
void copo(){
lcd.clear();
lcd.setCursor(4,0);
lcd.print("AGUARDE");
lcd.setCursor(6,1);
lcd.print(">>>");
myservo.write(0); // Comando para mandar o servo para posição 180
for(int n=0;n<8;n++){
digitalWrite(buzzer, HIGH);
delay(400);
digitalWrite(buzzer, LOW);
delay(400);
}
myservo.write(90); // Comando para mandar o servo para posição 0
}
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(bt_esq, INPUT_PULLUP); //botão esquerdo
pinMode(bt_ent, INPUT_PULLUP); //botão enter
pinMode(bt_dir, INPUT_PULLUP); //botão direito
pinMode(Y1, OUTPUT); //Valvula Y1
pinMode(Y2, OUTPUT); //Válvula Y2
pinMode(buzzer, OUTPUT); //Buzzer
pinMode(sensorcoffee, INPUT); //sensor de nível cafe
pinMode(sensormilk, INPUT); //sensor de nível leite
myservo.attach(9); //servomotor
//inicialização das variáveis
digitalWrite(Y1, LOW);
digitalWrite(Y2, LOW);
digitalWrite(buzzer, LOW);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("INICIANDO");
delay(1000);
lcd.setCursor(3,1);
lcd.print("COFFEE V1");
delay(1000);
lcd.clear();
}
int menu=0; //seleção do menu
int rt_BotaoD=1; //variável auxiliar para borda do botão direito
int rt_BotaoE=1; //variavel auxiliar para borda do botão esquerdo
int rt_BotaoEnter=1; //variavel auxiliar para borda do botão enter
bool activescreen2=0;
bool erro=0;
bool activemenu=0;
float pot1=0;
float pot2=0;
float sensorcafe=0;
float sensorleite=0;
int tempo=0;
void loop() {
//Declaração dos sensores e limite dos mesmos
pot1 = analogRead(A0); //potenciometro sensor café
sensorcafe = 12.0*(pot1/1023.0);
pot2 = analogRead(A1); //potenciometro sensor leite
sensorleite = 12.0*(pot2/1023.0);
//Lógica de seleção de menu
if (digitalRead(bt_dir)==0 && rt_BotaoD==1){
menu = menu +1;
rt_BotaoD=0;
}
if (digitalRead(bt_esq)==0 && rt_BotaoE==1){
menu = menu -1;
rt_BotaoE=0;
}
//Lógica de navegação no menu
if (activescreen2 ==0){
if (menu ==0){
lcd.clear();
lcd.setCursor(3,0);
lcd.print("SELECIONE:");
lcd.setCursor(0,1);
lcd.print("< CAFE PEQUENO >");
delay(200);
}
else if (menu ==1){
lcd.clear();
lcd.setCursor(3,0);
lcd.print("SELECIONE:");
lcd.setCursor(0,1);
lcd.print("< CAFE MEDIO >");
delay(200);
}
else if (menu ==2){
lcd.clear();
lcd.setCursor(3,0);
lcd.print("SELECIONE:");
lcd.setCursor(0,1);
lcd.print("< CAFE GRANDE >");
delay(200);
}
else if (menu ==3){
lcd.clear();
lcd.setCursor(3,0);
lcd.print("SELECIONE:");
lcd.setCursor(0,1);
lcd.print("< PINGADO >");
delay(200);
}
else if (menu ==4){
lcd.clear();
lcd.setCursor(3,0);
lcd.print("SELECIONE:");
lcd.setCursor(0,1);
lcd.print("< LEITE >");
delay(200);
}
}
if (activescreen2 ==1){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("CAFE:");
lcd.setCursor(5,0);
lcd.print(sensorcafe);
lcd.setCursor(11,0);
lcd.print("L");
lcd.setCursor(0,1);
lcd.print("LEITE:");
lcd.setCursor(6,1);
lcd.print(sensorleite);
lcd.setCursor(11,1);
lcd.print("L");
delay(200);
}
//RETORNO DA TELA DE MONITORAMENTO ATRAVÉS DO BOTÃO ESQUERDO
if (activescreen2 ==1 && digitalRead(bt_esq)==0 && rt_BotaoE==1){
activescreen2 =0;
rt_BotaoE=0;
}
//limita valor máximo
if (menu >= 4){
menu =4;
}
//limita valor mínimo
if (menu < 0){
menu =0;
}
//Bordas dos botões
if (digitalRead(bt_dir)==HIGH){
rt_BotaoD=1;
}
if (digitalRead(bt_esq)==HIGH){
rt_BotaoE=1;
}
if (digitalRead(bt_ent)==HIGH){
rt_BotaoEnter=1;
}
//GERA ERRO SE NIVEL BAIXO
if (digitalRead(bt_ent)==0 && sensorcafe <= 0.3){
lcd.clear();
lcd.setCursor(1,0);
lcd.print("ERRO 101");
lcd.setCursor(1,1);
lcd.print("NIVEL:");
lcd.setCursor(8,1);
lcd.print(sensorcafe);
lcd.setCursor(12,1);
lcd.print("L");
delay(3000);
erro=1;
}
if (digitalRead(bt_ent)==0 && sensorleite <= 0.3){
lcd.clear();
lcd.setCursor(1,0);
lcd.print("ERRO 102");
lcd.setCursor(1,1);
lcd.print("NIVEL:");
lcd.setCursor(8,1);
lcd.print(sensorleite);
lcd.setCursor(12,1);
lcd.print("L");
delay(3000);
erro=1;
}
//Ativa tela de nível
if(digitalRead(bt_ent)==0 && activescreen2==0 && erro==0){
int tempo=0;
while(digitalRead(bt_ent)==0){
tempo+=10;
if(digitalRead(bt_ent)==1) break;
Serial.println(tempo);
}
if(tempo>=4000){
activescreen2=1;
activemenu=0;
};
if(tempo<4000){
activescreen2=0;
activemenu=1;
}
//Lógica do funcionamento para cada tipo de café
if (menu ==0 && activemenu==1){
copo();
digitalWrite(Y1, HIGH);
delay(3000);
digitalWrite(Y1, LOW);
pronto();
}
else if (menu ==1 && activemenu==1){
copo();
digitalWrite(Y1, HIGH);
delay(6000);
digitalWrite(Y1, LOW);
pronto();
}
else if (menu ==2 && activemenu==1){
copo();
digitalWrite(Y1, HIGH);
delay(9000);
digitalWrite(Y1, LOW);
pronto();
}
else if (menu ==3 && activemenu==1){
copo();
digitalWrite(Y2, HIGH);
digitalWrite(Y1, HIGH);
delay(6000);
digitalWrite(Y2, LOW);
digitalWrite(Y1, LOW);
pronto();
}
else if (menu ==4 && activemenu==1){
copo();
digitalWrite(Y2, HIGH);
delay(6000);
digitalWrite(Y2, LOW);
pronto();
}
}
//reseta o erro quando nível subir
if (sensorcafe > 0.3 && sensorleite > 0.3){
erro=0;
}
}