#include <LiquidCrystal_I2C.h> // Libreria que controla el I2C
#include <Wire.h> // La libreria Wire viene incluida en el IDE de arduino
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // dependiendo del fabricante del LCD, la direccion 0x27 puede cambiar, utilice el I2C_Scan primero por favor!
const int channelPinA = 3;
const int channelPinB = 2;
const int channelPinOK = 4;
const int timeThreshold = 30;
long timeCounter = 0;
const int maxSteps = 9;
volatile int ISRCounter = 0;
int counter = 1;
bool IsCW = true;
bool dato = LOW;
bool datoAnterior = LOW;
bool Flag_0 = LOW;
int N_Valvulas = 5;
unsigned long Conteo = 0;
unsigned long Conteo2 = 0;
unsigned long Contador_1 = 0;
unsigned long Contador_2 = 1;
unsigned long Contador_3 = 0;
unsigned long Tiempo_1 = 350;
bool Flag_1 = 0;
bool Flag_1B = 0;
unsigned long Contador_1Aux = 0;
unsigned long Contador_2Aux = 0;
unsigned long Delta_1 = 0;
void setup()
{
EEPROM.put(1,5);
pinMode(channelPinA, INPUT_PULLUP);
pinMode(channelPinB, INPUT_PULLUP);
pinMode(channelPinOK, INPUT_PULLUP);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(channelPinA), doEncodeA, CHANGE);
attachInterrupt(digitalPinToInterrupt(channelPinB), doEncodeB, CHANGE);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Filtro de Mangas "); // Mensaje a desplegar
delay (2000);
// LeerEeprom()
}
void loop()
{
if (counter != ISRCounter){counter = ISRCounter; Contador_1Aux = ISRCounter; Serial.println(counter);}
delay(10);
Menu1();
lcd.setCursor(5,1);
lcd.print(Conteo);
lcd.print(" ");
Conteo = Conteo + 1;
}
void Menu1()
{
if (Flag_0 == 0){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Cant Valvulas:");
//EEPROM.get(1,N_Valvulas);
lcd.setCursor(1,1);
lcd.print(N_Valvulas);
//ISRCounter = 0;
//Contador_1Aux = ISRCounter;
//ISRCounter = N_Valvulas;
//counter = N_Valvulas;
lcd.setCursor(12,1); lcd.print(Contador_2);
Flag_0 = 1;} // Cambiar a 0 cuando termine de configurar
dato = digitalRead(channelPinOK);
if (dato == LOW && datoAnterior == HIGH){
Flag_1 = 1; // detectó flanco ascendente
ISRCounter = N_Valvulas;
counter = N_Valvulas;
Contador_2++;
if(Contador_2 > 4){Contador_2 =1;}
lcd.setCursor(12,1); lcd.print(Contador_2); lcd.print(" ");
} datoAnterior = dato;
if (Flag_1 == 1){
if (millis() > Contador_1 + Tiempo_1){Contador_1 = millis();
if (Flag_1B == 1){Flag_1B = 0; lcd.setCursor(1,1); lcd.print(N_Valvulas);}
else {Flag_1B = 1; lcd.setCursor(1,1); lcd.print(" ");}
}
N_Valvulas = counter;
}
//if (Contador_1Aux != ISRCounter && IsCW == true){Contador_1Aux = ISRCounter; N_Valvulas++;}
//if (Contador_1Aux != ISRCounter && IsCW == false){Contador_1Aux = ISRCounter; N_Valvulas--;}
//Contador_1 = millis();
//Delta_1 = ISRCounter - Contador_1Aux;
//Contador_1Aux = ISRCounter;
//N_Valvulas = N_Valvulas + Delta_1;
}
//while(1){}
void Proceso()
{
}
void LeerEeprom()
{
EEPROM.get(1,N_Valvulas);
}
void doEncodeA()
{
if (millis() > timeCounter + timeThreshold)
{
if (digitalRead(channelPinA) == digitalRead(channelPinB))
{
IsCW = true;
if (ISRCounter + 1 <= maxSteps) ISRCounter++;
}
else
{
IsCW = false;
if (ISRCounter - 1 >= 0) ISRCounter--;
}
timeCounter = millis();
}
}
void doEncodeB()
{
if (millis() > timeCounter + timeThreshold)
{
if (digitalRead(channelPinA) != digitalRead(channelPinB))
{
IsCW = true;
if (ISRCounter + 1 <= maxSteps) ISRCounter++;
}
else
{
IsCW = false;
if (ISRCounter - 1 >= 0) ISRCounter--;
}
timeCounter = millis();
}
}