#include <Keypad.h>
#include <Wire.h>               // Inclua a biblioteca Wire para a comunicação I2C
#include <LiquidCrystal_I2C.h>  // Inclua a biblioteca LiquidCrystal_I2C para o LCD I2C
#define ROW_NUM     4 // four rows
#define COLUMN_NUM  4 // four columns
#include "PCF8575.h"


char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROW_NUM] = {13, 12, 14, 27}; // Pinos das linhas conectados ao teclado numérico 4x3
byte colPins[COLUMN_NUM] = {26, 25, 33, 32}; // Pinos das colunas conectados ao teclado numérico 4x3

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROW_NUM, COLUMN_NUM );
LiquidCrystal_I2C lcd(0x27, 16, 2); // Endereço I2C 0x27, 16 colunas e 2 linhas
PCF8575 pcf8575(0x20);
int flag = 0;
int QtdDose = 0;
int BebidaSel,QtdDoseSel = 0;   
int flowMilliLitres = 0;
unsigned int totalMilliLitres,Number,contador = 0; 
int DoseMl = 50;

int sensorPin     = 0; //Digital Pin 2
int relePin       = 0;
int ledPin        = 0;
int restartPin    = 12;
volatile byte pulseCount = 0; 
const int startButtonPin = 4; // Pino do botão de início
bool buttonState = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  lcd.init();                      // Inicialize o LCD
  lcd.backlight();                 // Ligue a retroiluminação do LCD
  pinMode(startButtonPin, INPUT_PULLDOWN);
  pcf8575.begin();

	pcf8575.pinMode(P0, OUTPUT);
	pcf8575.pinMode(P1, OUTPUT);
	pcf8575.pinMode(P2, OUTPUT);
	pcf8575.pinMode(P3, OUTPUT);
	pcf8575.pinMode(P4, OUTPUT);
	pcf8575.pinMode(P5, OUTPUT);
	pcf8575.pinMode(P6, OUTPUT);
	pcf8575.pinMode(P7, OUTPUT);
	pcf8575.pinMode(P8, OUTPUT);
	pcf8575.pinMode(P9, OUTPUT);
	pcf8575.pinMode(P10, OUTPUT);
	pcf8575.pinMode(P11, OUTPUT);
	pcf8575.pinMode(P12, OUTPUT);
	pcf8575.pinMode(P13, OUTPUT);
	pcf8575.pinMode(P14, OUTPUT);
	pcf8575.pinMode(P15, OUTPUT);

  limpar();
  


}

void loop() {
  if (flag == 0) //Aguardando para selecionar Babida
  {
      //char opcao_bebida = Serial.read(); // Armazena caractere lido
      char opcao_bebida = keypad.getKey();   
      Serial.println(opcao_bebida);
      if (opcao_bebida == '1'){
        flag = 1; 
        BebidaSel = 1; 
        defineSensor(14,P0,P8);
      }
      if (opcao_bebida == '2'){
        flag = 1;  
        BebidaSel = 2; 
        defineSensor(27,P1,P9);
      }
      if (opcao_bebida == '3'){
        flag = 1;  
        BebidaSel = 3; 
        defineSensor(26,P2,P10);
      }    
      if (opcao_bebida == '4'){
        flag = 1;  
        BebidaSel = 4; 
        defineSensor(25,P3,P11);
      }
      if (opcao_bebida == '5'){
        flag = 1;  
        BebidaSel = 5; 
        defineSensor(33,P4,P12);
      }
      if (opcao_bebida == '6'){
        flag = 1;  
        BebidaSel = 6; 
        defineSensor(35,P5,P13);
      }
      if (opcao_bebida == '7'){
        flag = 1;  
        BebidaSel = 7; 
        defineSensor(34,P6,P14);
      }
      if (opcao_bebida == '8'){
        flag = 1;  
        BebidaSel = 8; 
        defineSensor(39,P7,P15);
      }
      if (opcao_bebida == '*'){
        limpar();
      }   
      
      lcd.setCursor(0, 0);
      lcd.print("Bebida 1 a 8 ");
      lcd.setCursor(14, 0);
      lcd.print(BebidaSel);           
  }

  if (flag == 1) //Selecionou o a bebida
  {
      //char key = Serial.read(); // Armazena caractere lido
      char opcaodose = keypad.getKey();      
      lcd.setCursor(0, 1);
      lcd.print("Qtd Dose(s)?  ");
      if (opcaodose == '1') //If Button 1 is pressed
      { 
        if (Number==0)
          Number=1;
        else
          Number = (Number*10) + 1; //Pressed twice
        QtdDoseSel = 1;
      }
      if (opcaodose == '2') //Button 2 is Pressed
      { 
        if (Number==0)
          Number=2;
        else
          Number = (Number*10) + 2; //Pressed twice
        QtdDoseSel = 2;
      }
      if (opcaodose == '3')
      { 
        if (Number==0)
           Number=3;
        else
           Number = (Number*10) + 3; //Pressed twice
        QtdDoseSel = 3;
      }    
      if (opcaodose == '*'){
        limpar();
      }     
      if (QtdDoseSel > 0){
         flag = 2;
         lcd.setCursor(14, 1);
         lcd.print(QtdDoseSel);
         Number = QtdDoseSel * DoseMl;
         Serial.println(Number);
         QtdDoseSel = 0;
      }
  }
  if (flag == 2){
      char opcaolimpar = keypad.getKey();
      if (opcaolimpar == '*'){
        limpar();
      }    
  }
  buttonState = digitalRead(startButtonPin);
  if (buttonState == HIGH){
    //pcf8575.digitalWrite(relePin, LOW);
    pcf8575.digitalWrite(ledPin, LOW);
    pcf8575.digitalWrite(relePin, HIGH);
   // pcf8575.digitalWrite(ledPin, LOW);
    Serial.println("LedPin selecionado: "); 
    Serial.print(ledPin);
    calculaVasao();
  }
}
void limpar()
{
  Number = 0;
  flag = 0;
  QtdDoseSel = 0;
  lcd.clear();
  BebidaSel = 0;
  totalMilliLitres = 0;
  for (int i = 0; i < 8; i++) {
    pcf8575.digitalWrite(i, LOW);
  }
  for (int i = 8; i < 16; i++) {
    pcf8575.digitalWrite(i, LOW);
  }

  
}

void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}
void defineSensor(int sensorpin, int relepin, int ledpin){
  sensorPin = sensorpin;
  relePin = relepin;
  ledPin = ledpin;
 // pcf8575.digitalWrite(relePin, HIGH);
  pcf8575.digitalWrite(ledPin, HIGH);
   
  pinMode(sensorPin, INPUT);
  Serial.println("rele selecionado: ");
  Serial.print(relePin);


  //attachInterrupt(digitalPinToInterrupt(sensorPin), pulseCounter, FALLING); //you can use Rising or Falling

}

void calculaVasao(){
  lcd.clear();
  for (int i = 0; i < Number; i++) {
    lcd.setCursor(0,0);
    lcd.print("Qtd ml: ");
    lcd.setCursor(9,0);
    lcd.print(i);
    delay(100);
  }    
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Finalizado!!! ");
  delay(1000);
  limpar();
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
pcf8575Breakout