if statement running when it's condition wouldn't be true?

I'm tinkering with Arduino to turn my little hydroponic garden at home into an automated system. Having an issue with my Serial and a simple alarm for my water level float I coded. I've tried moving code around and fixing it but I'm still having issues and keep hitting a wall.

I have a digital pin to detect my water reservoir float (5v to the float, if 5v comes back onto the pin, proceed with pumpschedule. If not, disable circulation pump).

Here's my serial:

                                                               //Debug info:
14:39:22.316 -> ################ Pump/Float ################
14:39:22.363 -> waterLevelFloat: 1
14:39:22.363 -> floatAlarm: Deactivated
14:39:22.410 -> previousPumpMillis: 0
14:39:22.410 -> circulationPump: 1
14:39:22.443 -> circulationPumpStatus: 
14:39:22.476 -> circulationPumpONStatus: ON
14:39:22.476 -> ################# Lighting #################
14:39:22.558 -> previousGrowLightsMillis: 0
14:39:22.558 -> growLightStatus: ON
14:39:22.605 -> growLights: 1
14:39:22.605 -> ############### Alarms/Others ###############
14:39:22.652 -> Millis: 38935
14:39:22.699 -> reset: 0
14:39:22.699 -> alarmCount: 0
14:39:22.699 -> cautionLightStatus: OFF
14:39:22.745 -> previousCautionMillis: 0
14:39:22.745 -> 
14:39:22.745 -> 
14:39:22.745 -> Free Ram = 5793
14:39:22.792 -> 
14:39:24.973 -> Float Alarm: Activated    // I disconnected the 5v from my input pin here
14:39:24.973 -> Caution: Water Level Low! Pump Disabled!
14:39:25.028 -> Float alarm reset
14:39:25.028 -> Float alarm reset
14:39:25.075 -> Float alarm reset
14:39:25.075 -> Float alarm reset
14:39:25.122 -> Float alarm reset
14:39:25.122 -> Float alarm reset
14:39:25.168 -> Float alarm reset
14:39:25.168 -> Float alarm reset
14:39:25.168 -> Float alarm reset
14:39:25.215 -> Float alarm reset
14:39:25.215 -> Float alarm reset
14:39:25.262 -> Float alarm reset
14:39:25.262 -> Float alarm reset
14:39:25.309 -> Float alarm reset
14:39:25.309 -> Float alarm reset
14:39:25.309 -> Float alarm reset
14:39:25.356 -> Float alarm reset
14:39:25.356 -> Float alarm reset
14:39:25.403 -> Float alarm reset
14:39:25.403 -> Float alarm reset
14:39:25.403 -> Float alarm reset
14:39:25.436 -> Float alarm reset
14:39:25.470 -> Float alarm reset 
14:39:25.470 -> Float alarm reset
14:39:25.504 ->                                  // Debug info:
14:39:25.504 -> ################ Pump/Float ################
14:39:25.538 -> waterLevelFloat: 0
14:39:25.571 -> floatAlarm: Deactivated
14:39:25.605 -> previousPumpMillis: 0
14:39:25.605 -> circulationPump: 0
14:39:25.638 -> circulationPumpStatus: 
14:39:25.671 -> circulationPumpONStatus: ON
14:39:25.705 -> ################# Lighting #################
14:39:25.738 -> previousGrowLightsMillis: 0
14:39:25.771 -> growLightStatus: ON
14:39:25.804 -> growLights: 1
14:39:25.804 -> ############### Alarms/Others ###############
14:39:25.873 -> Millis: 42151
14:39:25.873 -> reset: 0
14:39:25.873 -> alarmCount: 0
14:39:25.908 -> cautionLightStatus: OFF
14:39:25.943 -> previousCautionMillis: 41304

Here's the code:

void Float() {
  if (digitalRead(waterLevelFloat) == 0) {

      digitalWrite(circulationPump,LOW);
      circulationPumpStatus = "DISABLED";
      floatAlarm = "Activated";
      alarmCount = 1;
      digitalWrite(cautionLight,HIGH);
      cautionLightStatus = "ON";
      
    if (millis()-previousCautionMillis >= 10000) {

      previousCautionMillis = millis();
      Serial.print("Float Alarm: ");
      Serial.println(floatAlarm);
      Serial.println("Caution: Water Level Low! Pump Disabled!");
   }
  } 
  
  if (digitalRead(waterLevelFloat) == 1) {
    circulationPumpStatus = "";
    floatAlarm = "Deactivated";
    if (millis() == 0) {
      pumpSchedule(); 
    }
    if (alarmCount == 1) {
    Serial.println("Float alarm reset");
    alarmCount = 0;
    cautionLightStatus = "OFF";
    }
    else if (millis()-previousPumpMillis > intervalPump) { // Run pump every 20 Minutes.
      previousPumpMillis = millis();
      pumpSchedule();
    }
  }
}

I have to be missing something. The "Float alarm Reset" is only supposed to print once. When the water level float is satisfied and closes the circuit, sending 5v back to my input pin. Instead, it prints over and over when 5v is not applied, and deavtivates my floatAlarm. Even though I have the code to print "Float alarm Reset" inside my "if (digitalRead(waterLevelFloat) == 1)".

You'll even see in my second Debug info printout, it read that my waterLevelFloat == 0, yet is still printing the info that's only supposed to print when my waterLevelFloat == 1

As always, any and all constructive criticism is welcomed along side your help

Thanks for trying to follow the forum guidelines. Please change the quote tags to code tags.

Sorry, still a newbie. Didn't know how, even though it was right in the instructions :astonished: , will change now

I have a digital pin to detect my water reservoir float (5v to the float, if 5v comes back onto the pin, proceed with pumpschedule.

Is there an external pull down resistor on this input pin?

Please post complete code. Often the issue is in the part of the code not posted.

If your complete code is very long and complex you would do better to make an MCVE (minimal, complete, verifiable, example) which demonstrates the problem.

cattledog:
Is there an external pull down resistor on this input pin?

Please post complete code. Often the issue is in the part of the code not posted.

If your complete code is very long and complex you would do better to make an MCVE (minimal, complete, verifiable, example) which demonstrates the problem.

No pull down resistor

This is the only portion of the code that reads the waterLevelFloat, and alters the cautionAlarm, floatAlarm, alarmCount, and cautionLight. All of the variables with “Status” are just for debugging. I’m not 100% sure how to make a MCVE :frowning:

This section references the pumpSchedule, here is the code below:

void pumpSchedule() {
  
  if (digitalRead(circulationPump)==1) {
    previousPumpMillis = millis();
    digitalWrite(circulationPump,LOW);
    circulationPumpONStatus = "OFF";
    Serial.print("Pump: ");
    Serial.print(circulationPumpONStatus);
    Serial.print(" ");
    Serial.println(circulationPumpStatus);
  }
  
  else if (digitalRead(circulationPump)==0) {
    previousPumpMillis = millis();
    digitalWrite(circulationPump,HIGH);
    circulationPumpONStatus = "ON";
    Serial.print("Pump: ");
    Serial.print(circulationPumpONStatus);
    Serial.print(" ");
    Serial.println(circulationPumpStatus);
  }
}

I’m not sure how much you would consider “long and complex”. Although it doesn’t fit in one post, so I’ve attached it in a notepad.

Code.txt (9.75 KB)

No pull down resistor

Required to prevent the input pin from "floating" if there is no 5v applied.

cattledog:
Required to prevent the input pin from "floating" if there is no 5v applied.

https://www.arduino.cc/en/Tutorial/DigitalPins

Well shoot.

Would i be able to produce the result I'm wanting(not to have to use a pull down resistor and just have the mechanical float act as an on/off safety switch) with an I2C shield or serial port expander?
I haven't learned these yet but learning I2C is next on my list, followed by learning to use a serial port expander

Would i be able to produce the result I'm wanting(not to have to use a pull down resistor and just have the mechanical float act as an on/off safety switch)

Why the resistance to adding a resistor :wink:

You can certainly use INPUT_PULLUP for the pinMode, and wire through the float switch to ground. The code logic will need to be reversed.

I have a digital pin to detect my water reservoir float (5v to the float, if 5v comes back onto the pin, proceed with pumpschedule. If not, disable circulation pump).

Can you explain a bit more about the float switch. Is it closed or open when the water level is where you want it to activate the pump.

cattledog:
Why the resistance to adding a resistor :wink:

You can certainly use INPUT_PULLUP for the pinMode, and wire through the float switch to ground. The code logic will need to be reversed.

Can you explain a bit more about the float switch. Is it closed or open when the water level is where you want it to activate the pump.

I'll look into the pull up! Thank you

The float switch is a normally open, and would close to send the input signal when the water reservoir is full, to allow the pump to run its water circulation schedule

How long are the wires between the Arduino and the float switch?

The float switch is a normally open, and would close to send the input signal when the water reservoir is full, to allow the pump to run its water circulation schedule

OK. With INPUT_PULLUP the digitalRead() of the pin will be low when the switch is closed, and high when open.

Please report back if change this corrects the multiple prints. A floating pin, can be very unpredictable.

cattledog:
OK. With INPUT_PULLUP the digitalRead() of the pin will be low when the switch is closed, and high when open.

Please report back if change this corrects the multiple prints. A floating pin, can be very unpredictable.

Ok so this partially worked. Changed the code to be
pinMode(waterLevelFloat , INPUT_PULLUP);
also reversed the logic:

void Float() {
  if (digitalRead(waterLevelFloat) == HIGH) {

      digitalWrite(circulationPump,LOW);
      circulationPumpStatus = "DISABLED";
      floatAlarm = "Activated";
      alarmCount = 1;
      digitalWrite(cautionLight,HIGH);
      cautionLightStatus = "ON";
      
    if (millis()-previousCautionMillis >= 10000) {

      previousCautionMillis = millis();
      Serial.print("Float Alarm: ");
      Serial.println(floatAlarm);
      Serial.println("Caution: Water Level Low! Pump Disabled!");
   }
  }

Now I have another issue. When removing 5v from the pin I get the correct response "Caution: Water Level Low! Pump Disabled!", but when applying 5v to the pin again(simulating refilling the water resevoir) the digitalRead(waterLevelFloat); still shows a value of 1.

Here's the print out:

Float Alarm: Activated                               // Removed 5v from pin
Caution: Water Level Low! Pump Disabled!

                                   // My debug while 5v removed from pin
################ Pump/Float ################
waterLevelFloat: 1
floatAlarm: Activated
previousPumpMillis: 0
circulationPump: 0
circulationPumpStatus: DISABLED
circulationPumpONStatus: 
################# Lighting #################
previousGrowLightsMillis: 0
growLightStatus: ON
growLights: 1
############### Alarms/Others ###############
Millis: 23923
reset: 0
alarmCount: 1
cautionLightStatus: ON
previousCautionMillis: 20000


Free Ram = 5794


Float Alarm: Activated
Caution: Water Level Low! Pump Disabled!
Float Alarm: Activated                                // 5v was re-applied to the pin simulating water resivoir refilled
Caution: Water Level Low! Pump Disabled!

                                    // My debug while 5v applied to pin
################ Pump/Float ################
waterLevelFloat: 1
floatAlarm: Activated
previousPumpMillis: 0
circulationPump: 0
circulationPumpStatus: DISABLED
circulationPumpONStatus: 
################# Lighting #################
previousGrowLightsMillis: 0
growLightStatus: ON
growLights: 1
############### Alarms/Others ###############
Millis: 43743
reset: 0
alarmCount: 1
cautionLightStatus: ON
previousCautionMillis: 40000


Free Ram = 5794


Float Alarm: Activated
Caution: Water Level Low! Pump Disabled!
Float Alarm: Activated
Caution: Water Level Low! Pump Disabled!
Float Alarm: Activated
Caution: Water Level Low! Pump Disabled!
Float Alarm: Activated
Caution: Water Level Low! Pump Disabled!
Float Alarm: Activated
Caution: Water Level Low! Pump Disabled!          //5v is still applied

cattledog:
OK. With INPUT_PULLUP the digitalRead() of the pin will be low when the switch is closed, and high when open.

Please report back if change this corrects the multiple prints. A floating pin, can be very unpredictable.

Shoot nvm my bad. I didn't change the jumper to go to ground. Changed and it's running perfectly. Thank you!