Ciao, ho un problema che non so perchè fa cosi.
Spiego come funziona. ho un contatore che parte da 100 e conta all'indietro con un pulsante. arrivato a 0 mi attiva un relè. ho inserito la funzione EEprom per memorizzare il numero in caso che vada via la corrente. ora tutto funziona bene, ma se arriva a zero non si ferma e mi inizia a contare da 255.
dov'è l'errore ?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int rele = 6; // RELE pin 6
int puls =3; // pulsante pin 6
int val = 0; // variable for reading the pin status
//int counter = 10;
int currentState = 0;
int previousState = 0;
String readString;
byte datoeeprom;
void setup() {
lcd.clear();
pinMode(rele, OUTPUT); // declare LED as output
pinMode(puls, INPUT); // declare pushbutton as input
Serial.begin(9600);
lcd.init(); // INIZIALIZZA IL DISPLAY
lcd.backlight();
lcd.setCursor(4,0); // POSIZIONA IL CURSORE
lcd.print("CONTATORE");
lcd.setCursor(4,1);
lcd.print(" CONSUMI");
delay(3500);
datoeeprom = EEPROM.read(1);
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
val = digitalRead(puls); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
currentState = 100; //counter = countr -1;
}
else {
currentState = 0;
}
if(currentState != previousState){
if(currentState == 0){
{datoeeprom--;}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("RIMANENTI");
lcd.setCursor(5,1);
lcd.print(datoeeprom);
Serial.println(datoeeprom);
{EEPROM.write(1, datoeeprom);}
}
if (datoeeprom == 0){
digitalWrite(rele, HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("CONSUMAZIONI");
lcd.setCursor(0,1);
lcd.print("TERMINATE A 0");
//datoeeprom = 1;
delay(500);
digitalWrite(rele, LOW);
}
previousState = currentState;
delay(250);
}
if (readString.length() >0) {
Serial.println(readString);
if (readString == "Carica")
{
datoeeprom = 100;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("CONSUMAZIONI");
lcd.setCursor(0,1);
lcd.print("CARICATE A 100");
EEPROM.write(1, datoeeprom);
}
}
readString="";
}