I = I + 1 and G = G + 1 // Counter is counting wrong because he is in IF and a loop. instead he counts the loops.
my need is that he counts the change of digitalWrite(25, HIGH) and digitalWrite(26, HIGH) from HIGH to LOW.
I have a medium size Solar System. This code allows me to run extra devices when batterys are full but still have Sun enough to run them. If the Battery voltage goes lower i switch some devices to the Grid while TV, lights and some small devices still run on battery.
i tryed a few samples for Counting but nothing worked. Everything else works perfect
Using Arduino IDE and ESP32DEV Board. In my opinion it has no use to post the other 450 linens of code where has nothing to do with my problem. maybe i am wrong.
My code looks like this
if ((val2 >= 12.60) && (val2 < 14.60)) {
delay(1000);
digitalWrite(25, HIGH); // Fridge OFF GRID (K3 = OFF)
lcd.setCursor(10, 3);
lcd.print("K3=");
lcd.setCursor(13, 3);
lcd.write(byte(0)); // GRID OFF
delay(1000);
digitalWrite(26, LOW); // Fridge ON INVERTER (K4 = ON)
lcd.setCursor(15, 3);
lcd.print("K4=");
lcd.setCursor(18, 3);
lcd.print("I"); // GRID OFF
//I = I + 1; // Counter I
}
if (val2 > 0.00 && val2 < 12.35) {
delay(1000);
digitalWrite(26, HIGH); // Fridge OFF INVERTER (K4 = OFF)
lcd.setCursor(15, 3);
lcd.print("K4=");
lcd.setCursor(18, 3);
lcd.print("0"); // INVERTER OFF
delay(1000);
digitalWrite(25, LOW); // Fridge ON GRID (K4 = ON)
lcd.setCursor(10, 3);
lcd.print("K3=");
lcd.setCursor(13, 3);
lcd.print("G"); // GRID ON
//G = G + 1; // Counter G
}
It is hard to deduce what you want based on your statements. I am going to assume that the code within the if block gets executed over and over based on the value of val2 but you really only want it executed once until val2 falls outside of the range specified by the if expression. The following is a solution to that:
if (val2 >= 12.60 && val2 < 14.60 && digitalRead(25) == LOW && digitalRead(26) == HIGH) {
delay(1000);
digitalWrite(25, HIGH); // Fridge OFF GRID (K3 = OFF)
lcd.setCursor(10, 3);
lcd.print("K3=");
lcd.setCursor(13, 3);
lcd.write(byte(0)); // GRID OFF
delay(1000);
digitalWrite(26, LOW); // Fridge ON INVERTER (K4 = ON)
lcd.setCursor(15, 3);
lcd.print("K4=");
lcd.setCursor(18, 3);
lcd.print("I"); // GRID OFF
I = I + 1; // Counter I
} else if (val2 > 0.00 && val2 < 12.35 && digitalRead(25) == HIGH && digitalRead(26) == LOW) {
delay(1000);
digitalWrite(26, HIGH); // Fridge OFF INVERTER (K4 = OFF)
lcd.setCursor(15, 3);
lcd.print("K4=");
lcd.setCursor(18, 3);
lcd.print("0"); // INVERTER OFF
delay(1000);
digitalWrite(25, LOW); // Fridge ON GRID (K4 = ON)
lcd.setCursor(10, 3);
lcd.print("K3=");
lcd.setCursor(13, 3);
lcd.print("G"); // GRID ON
G = G + 1; // Counter G
}
It essentially checks the range of val2 and checks the states of digital outputs 25 and 26 to determine if they need to be changed. If they don't the if expression evaluates to false and there is no action taken.
Since the digital outputs 25 and 26 always have opposite states you could actually just check one or the other.
I would also suggest you use descriptive constant names for the digital outputs and a more descriptive name for val2. This is helpful to someone else looking at your code.
Sorry to bother you . i understand somehow what you mean. But i dont realy know to add or change the code to count. My home language is german and am still the beginner somehow. Maybe you can add a code sample or change my code if possible
It works perfectly so far. Maybe it's made too complicated. Is possible
Thanks for your answers.
but at the moment your answers don't really help me for counting the outputs
The code where is not shown are complete different functions and have nothing to do with my question. The whole code has 500 lines and +/- 450 lines have no use for my question. i think
The parts you have shown have no declaration, initialization, or display for the variables 'I' or 'G'. Those missing parts are important to understanding what is going wrong.