Variable not being recognized by an if Statement

Hello, and thank you for your time. I am using a variable to record the last state read from a switch before it was turned off by the if statement. I am using Serial.print to check the state of the variable for changes and also to Serial.print a digitalRead of the switch to see if there are changes. The digitalRead of the switch changes from 0 to 1 as it is supposed to, but the variable doesn't change from 0 to 1 like it should. Why is the variable not changing as I think it should? Thank you for your help

////////////////////////////        Skimmer Status and Display  global

const int SkimmerFullLed (4);          // the pin number of the Skimmer Status led pin
const int SkimmerFullFloat (3);        // the pin number of the Floatswich pin
const int SkimmerOnButton (2);         // the pin number of the pushbutton pin
const int SkimmerRelay (5);            // the pin number of the skimmer relay pin
const int ScreenDelayButton (20);        // the pin number of the screen delay button pin

// variables will change:
int SkimmerFloatState = 0;         // variable for reading the skimmer float state 
int SkimmerFullLedState = 0;
int SkimmerOnButtonState = 0;
int ScreenDelayButtonState = 0;
int SkimmerFullMsgState = 0;

void setup()      ////////////////////////      Setup     ///////////////////////////////////////////////////////////////////////////////////////////
{
  Serial.begin(9600);    ////////////////        Initialize Serial communications
  lcd.begin(20, 4);      ////////////////        Initialize the 2004 LCD
  pinMode(SkimmerFullLed, OUTPUT);       // initialize the LED pin as an output
  pinMode(SkimmerFullFloat, INPUT_PULLUP);      // initialize the Float Switch pin as an input
  pinMode(SkimmerOnButton, INPUT_PULLUP);       // initialize the Skimmer on Button pin as an input
  pinMode(SkimmerRelay, OUTPUT);         // initialize the Skimmer relay pin as an output
  pinMode(ScreenDelayButton, INPUT_PULLUP);     // initialize the screen delay button as an input
}

void DisplayTimeTemp()      ////////////Display time and Water temperatures function     Setup
  {
  lcd.clear();                       /////////////////Display Time and Date
  lcd.setCursor(0, 0);
  lcd.print(rtc.getDOWStr());
  lcd.print("   ");
  lcd.print(rtc.getDateStr());          
  lcd.setCursor(0, 1);
  lcd.print(rtc.getTimeStr());
  lcd.setCursor(9, 1);
  lcd.print("RTC ");
  lcd.print(rtc.getTemp());
  lcd.print(" C");

  V0 = analogRead(ThermistorTankPin0);  //////////////////  Read tank thermistor
  R2 = R1 * (1023.0 / (float)V0 - 1.0);
  logR2 = log(R2);
  Temp = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
  Temp = Temp - 273.15;
  //Tenp = (Temp * 9.0)/ 5.0 + 32.0;

  lcd.setCursor(0, 2); // Set the cursor at the 4th column and 1st row  /////////  Display tank water tempreture
  lcd.print("Tank Temp = ");
  lcd.print(Temp);
  lcd.print(" C");

  V1 = analogRead(ThermistorSumpPin1);    //////////////////  Read Sump thermistor
  R2 = R1 * (1023.0 / (float)V1 - 1.0);
  logR2 = log(R2);
  Temp = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
  Temp = Temp - 273.15;
  //Temp = (Temp * 9.0)/ 5.0 + 32.0;         /Convert celsius to fahrenheit

  lcd.setCursor(0, 3); // Set the cursor at the 4th column and 1st row /////////  Display tank water temperature
  lcd.print("Sump Temp = ");
  lcd.print(Temp);
  lcd.print(" C");
  }

void loop() ////////////////////////////////   Loop    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{

Serial.println("");
Serial.println("SkimmerFloatState");
Serial.println(SkimmerFloatState);
Serial.println("Digital read float ");
Serial.println(digitalRead(SkimmerFullFloat));
Serial.println("");

DisplayTimeTemp();
  
  if (SkimmerFloatState == 0)   //  Float state starting point it should do this every time until the skimmer is seen full
  {
  digitalRead(SkimmerFullFloat);  // read the state of the Skimmer full float switch value:    
  }
  
  if (SkimmerFullFloat == LOW)    // if skimmer is full   
  {
  SkimmerFloatState = 1;        //Set skimmer float state to 1 for last read was full
  }
    
digitalRead(SkimmerOnButton);     // read the state of the Skimmer reset button value:
  
  if (SkimmerFullMsgState == 1)
  {
  SkimmerFullMsg();
  }
    
ScreenDelayButtonState = digitalRead(ScreenDelayButton);   // read the state of the Skimmer button
  if (ScreenDelayButtonState  = LOW)                    //If screen delay button is pressed delay 5 seconds
  {
  delay(5000);
  }
    
  if (SkimmerFloatState  == 1)      //If Skimmer is full
  {
  digitalWrite(SkimmerRelay, HIGH);     ////ACTIVE LOW turn skimmer off
  analogWrite(SkimmerFullLed, 25);      ////turn skimmer full led on
  lcd.clear();
  SkimmerFullMsg();                       ////Display Skimmer Full Message
  }

  if (SkimmerFloatState == 1 && SkimmerOnButtonState == LOW)    //If skimmer is empty, and skimmer on button is pressed
  {
  digitalWrite(SkimmerRelay, LOW);        /////ACTIVE LOW turn on relay
  digitalWrite(SkimmerFullLed, LOW);      ////Turn off skimmer full led
  SkimmerFullMsgState = 0;                ////Turn off SkimmerFullMsg
  SkimmerFloatState = 0;                  ////Reset skimmer float to zero
  lcd.clear();
  }

delay(1000);
}

This is the function for the skimmer full Message if it is important

void SkimmerFullMsg()      /////////  Function for Display the skimmer full Message     Setup
  {
  lcd.clear();
  lcd.setCursor(0, 0);                  ////set cursor on the start of the 1 row
  lcd.print("    Skimmer Full");            ////write on the Lcd
  lcd.setCursor(0, 1);
  lcd.print("Empty Clean, Replace");
  lcd.setCursor(0, 2);
  lcd.print("   Then Press the");
  lcd.setCursor(0, 3);
  lcd.print("  Button to Restart");
  SkimmerFullMsgState = 1;
  delay(2000);
  }
  digitalRead(SkimmerFullFloat);  // read the state of the Skimmer full float switch value:

That is suspiciously not doing anything with the result of the digitalRead()
There are similar errors elsewhere in the code.

westfw:

  digitalRead(SkimmerFullFloat);  // read the state of the Skimmer full float switch value:

That is suspiciously not doing anything with the result of the digitalRead()
There are similar errors elsewhere in the code.

Hmm... What do you make of it? I can upload the whole sketch if it is helpful...

Has anyone else had similar experiences to this or any insight?

I will try again tomorrow, I'm beat. Thanks everyone for taking a look. hopefully i can get some sleep tonight....

const int SkimmerFullFloat (3);
...
...
 
  if (SkimmerFullFloat == LOW)    // if skimmer

You gave "SkimmerFullFloat" the value 3.
LOW has the value zero.
They are never, ever going to be equal.

TheMemberFormerlyKnownAsAWOL:

const int SkimmerFullFloat (3);

...
...

if (SkimmerFullFloat == LOW)    // if skimmer



You gave "SkimmerFullFloat" the value 3.
LOW has the value zero.
They are never, ever going to be equal.

So instead of declaring 3 as the pin I am using for the "SkimmerFullFloat", I am actually giving it a constant value of 3? What is the correct way to declare pin usages? The strange part is, it is seeing the "SkimmerFullFloat" on pin (3) because When I digitalRead it it changes from 0 - 1.
What would you try changing? Thank you for your help.

You could try a digitalRead

TheMemberFormerlyKnownAsAWOL:
You could try a digitalRead

The digitalRead confirms the switch is found on pin 3 and is giving the correct value for the state that the switch is in, but it does not change the variable like it is told to in this line:

 if (SkimmerFloatState == 0)     //  Float state starting point it should do this every time until theskimmer is seen full
  {
  digitalRead(SkimmerFullFloat);  // read the state of the Skimmer full float switch value:    
  }
  
  if (SkimmerFullFloat == LOW)    // if skimmer is full   
  {
  SkimmerFloatState = 1;        //Set skimmer float state to 1 for last read was full
  }

According to my Serial.println("SkimmerFloatState"); it is always 0 so it should be doing this every loop until it reads 1.

digitalRead doesn't change anything. It reads and returns the state of the pin.

See the documentation.

digitalRead(SkimmerFullFloat);

You're not doing anything with the return value.
This has already been pointed-out.

TheMemberFormerlyKnownAsAWOL:

const int SkimmerFullFloat (3);

...
...

if (SkimmerFullFloat == LOW)    // if skimmer



You gave "SkimmerFullFloat" the value 3.
LOW has the value zero.
They are never, ever going to be equal.

kaylebbccan814:
So instead of declaring 3 as the pin I am using for the "SkimmerFullFloat", I am actually giving it a constant value of 3? What is the correct way to declare pin usages? The strange part is, it is seeing the "SkimmerFullFloat" on pin (3) because When I digitalRead it it changes from 0 - 1.
What would you try changing? Thank you for your help.

I see what you mean, when I do a Serial.println(SkimmerFullFloat); it returns a 3 even though Serial.println(digitalRead(SkimmerFullFloat)); returned a 1 for closed, How do I fix this?

Serial.println(""); 
Serial.println("SkimmerFloatState"); ///////stays 0
Serial.println(SkimmerFloatState);
Serial.println("Digital read float ");
Serial.println(digitalRead(SkimmerFullFloat)); ////changes like it should from 1 to 0
Serial.println("");
Serial.println(SkimmerFullFloat); ////////////  stays at 3 
Serial.println("");

You seemed to be very confused about pin numbers and pin states. For example, the following code makes no sense:

  if (SkimmerFloatState == 0)   //  Float state starting point it should do this every time until the skimmer is seen full
  {
    digitalRead(SkimmerFullFloat);  // read the state of the Skimmer full float switch value:
  }

  if (SkimmerFullFloat == LOW)    // if skimmer is full
  {
    SkimmerFloatState = 1;        //Set skimmer float state to 1 for last read was full
  }

If you want the SkimmerFloatState to reflect whether the skimmer was full you could do something like this:

 if (digitalRead(SkimmerFullFloat) == LOW)
 {
   SkimmerFloatState = 1;
 }
 else
 {
   SkimmerFloatState = 0;
 }

A digitalRead() that is not assigned to a variable is useless.

ToddL1962:
You seemed to be very confused about pin numbers and pin states. For example, the following code makes no sense:

  if (SkimmerFloatState == 0)   //  Float state starting point it should do this every time until the skimmer is seen full

{
   digitalRead(SkimmerFullFloat);  // read the state of the Skimmer full float switch value:
 }

if (SkimmerFullFloat == LOW)    // if skimmer is full
 {
   SkimmerFloatState = 1;        //Set skimmer float state to 1 for last read was full
 }




If you want the **SkimmerFloatState** to reflect whether the skimmer was full you could do something like this:




if (digitalRead(SkimmerFullFloat) == LOW)
{
  SkimmerFloatState = 1;
}
else
{
  SkimmerFloatState = 0;
}





A **digitalRead()** that is not assigned to a variable is useless.

Yes , I am... I am trying to learn but always get stuck, thank you for your help, that makes more sense to me now, I will try changing some things now and see how I make out, please bear with me, I might not be that smart but I am determined to learn. Thanks again

Thank you ToddL1962, I appreciate you helping me understand pin states vs. pin numbers. Your example made sense to me, and I now have a better understanding of what I was actually asking in the line:

if (SkimmerFullFloat == LOW)

SkimmerFullFloat will always be 3 as that is what I declared it as by the line:

const int SkimmerFullFloat (3);        // the pin number of the Floatswich pin

But if I digital Read it and compare the result, then I can change a variable accordingly.

  if (SkimmerFloatState == 0 && (digitalRead(SkimmerFullFloat) == LOW))     //  Float state starting point it should do this every time until theskimmer is seen full
  {
  SkimmerFloatState = 1;          //Set skimmer float state to 1 for last read was full
  }

  if (SkimmerFloatState  == 1)      //If Skimmer is full
  {
  digitalWrite(SkimmerRelay, HIGH);     ////ACTIVE LOW turn skimmer off
  analogWrite(SkimmerFullLed, 25);      ////turn skimmer full led on
  lcd.clear();
  SkimmerFullMsg();                       ////Display Skimmer Full Message
  SkimmerFloatState = 1;
  }

  if (SkimmerFloatState == 1) 
  {
  SkimmerFullMsg();  
  }
    
  if (SkimmerFloatState == 1 && (digitalRead(SkimmerOnButton) == LOW))    //If skimmer is empty, and skimmer on button is pressed
  {
  digitalWrite(SkimmerRelay, LOW);        /////ACTIVE LOW turn on relay
  digitalWrite(SkimmerFullLed, LOW);      ////Turn off skimmer full led
  SkimmerFloatState = 0;
  lcd.clear();
  }
  
  if (digitalRead(ScreenDelayButton) == LOW)
  {
  delay(5000);
  }

Anyhow I got it, and it works the way I had intended. Thank you all for your patience and help. Thanks to all of you guys and the wealth of knowledge in this community, I am learning ,and creating more every day. Hopefully one day I can actually give back and help others as well. Much appreciated all...