SOLVED - Counting a output change in a IF doesn't count right

POST NUMBER 9 SOLVED THE PROBLEM :blush:

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
  }

Help us help you.

its' confusing ... click the "</>" icon and paste code into the highlighted area

do you want to increment the count whenever "val2" is within those ranges or only when the value of "val2" changes?

suggest you use Serial print to print "val2", "I" and "G"

I want to count the changes of digitalWrite(25, LOW); and digitalWrite(26, LOW); from HIGH to LOW and Show the counter on my LCD display

but your execute that code which toggles the output and increments the count whenever "val2" is with that range.

if toggling the output affects "val2", the sub-function will repeatedly be invoked until "val2" moves out of range

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.

1 Like

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

try using google translate

should you only turn the fridge OFF, if not OFF if it's within the range and turn it ON, if not ON when it is not longer within range?

also, you can use the Serial monitor for debugging

The problem is in the part of your sketch that you did not show.

It works perfectly so far. Maybe it's made too complicated. Is possible :slight_smile:
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 :slightly_smiling_face:

I gave you a potential solution in post #9. It only changes the outputs when they need to be changed and counts every change.

sorry somehow i missed this post. i will try it tomorrow.
Thank you :slightly_smiling_face:

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.

Thank you very match. :blush:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.