How to perform code based on the state of a switch

Hello,

I have a project I am working on that will read a sensor and either count up or count down based on the state of a switch. I have the switch connected to D3 and ground, I can read the state of the switch and it changes when I switch it from 0 to 1, I can also comment out the if statement related to the switch and am able to process the reading to serial as expected, so I assume I have an issue with how my If/Else is setup but cant figure out what I have done wrong, any help would be greatly appreciated.

#include <Wire.h>
//The Arduino Wire library uses the 7-bit version of the address, so the code example uses 0x70 instead of the 8‑bit 0xE0
#define SensorAddress byte(0x70)
//The Sensor ranging command has a value of 0x51
#define RangeCommand byte(0x51)
//These are the two commands that need to be sent in sequence to change the sensor address
#define ChangeAddressCommand1 byte(0xAA)
#define ChangeAddressCommand2 byte(0xA5)

// variables:
int sensorValue = 0;
int sensorMin = 0;            // minimum sensor value
int sensorMax = 0;                 // maximum sensor value
bool sensorReset = false;
int switchPin = 3;
int switchState;         // current state of the button
int ledPin = 5;                //LED Pin


void setup() {
  pinMode(ledPin,OUTPUT); //LED as Output
  pinMode(switchPin, INPUT); //Switch Input
  digitalWrite(switchPin, HIGH);
  Serial.begin(9600);//Open serial connection at 9600 baud
  Wire.begin();//Initiate Wire library

  while (millis() < 2000) 
  {
    sensorValue = requestRange();
    // record the maximum sensor value
     if (sensorValue > sensorMax) 
      {
        sensorMax = sensorValue;
      }
  
  }
  Serial.print("BaseDistance "); Serial.println(sensorValue);
}

//Commands the sensor to take a range reading
void takeRangeReading(){
Wire.beginTransmission(SensorAddress);             //Start addressing
Wire.write(RangeCommand);                             //send range command
Wire.endTransmission();                                  //Stop and do something else now
}

//Returns the last range that the sensor determined in its last ranging cycle in centimeters. Returns 0 if there is no communication.
word requestRange(){
Wire.requestFrom(SensorAddress, byte(2));
if(Wire.available() >= 2){                            //Sensor responded with the two bytes
byte HighByte = Wire.read();                        //Read the high byte back
byte LowByte = Wire.read();                        //Read the low byte back
int range = int(HighByte, LowByte);         //Make a 16-bit word out of the two bytes for the range
return range;
}
else {
return int(0);                                             //Else nothing was received, return 0
}
}


void loop() {
switchState = digitalRead(switchPin);
Serial.print("Switch "); Serial.println(switchState);
takeRangeReading();                                       //Tell the sensor to perform a ranging cycle
delay(100);                                                    //Wait for the sensor to finish
int range = requestRange();                           //Get the range from the sensor
if (!sensorReset)
{
  if ( range < sensorMax-3)
      {
            if(switchState == HIGH) 
            {
               digitalWrite(ledPin, HIGH);
              Serial.println("1");       //Print to the user
              sensorReset = true;
            }
            else
            {             
             digitalWrite(ledPin, HIGH);
             Serial.println("-1");       //Print to the user
             sensorReset = true;
             }
             
           
      }  
  else if ( range > sensorMax-5)
     {
      sensorReset = !sensorReset;
     }
}
delay(200);
digitalWrite(ledPin, LOW);
}

How is the switch wired? Is there a pulldown resistor on the switch input?

I have tried without a resistor as well as with a 10k resistor with the same effect.

Use serial print statements to follow the execution flow of the program and the values of variables.

I assume this is what you mean? I will try it to see what outputs I get.

{
  if ( range < sensorMax-3)
      { 
        Serial.print("Past first if");Serial.println(switchState);
            if(switchState == HIGH) 
            {
              Serial.print("Past first second");Serial.println(switchState);
               digitalWrite(ledPin, HIGH);
              Serial.println("1");       //Print to the user
              sensorReset = true;
            }
            else
            {     
              Serial.print("Past else");Serial.println(switchState);        
             digitalWrite(ledPin, HIGH);
             Serial.println("-1");       //Print to the user
             sensorReset = true;
             }
             
           
      }  
  else if ( range > sensorMax-5)
     {
      Serial.print("Sensor Reset Before");Serial.println(sensorReset);
      sensorReset = !sensorReset;
      Serial.print("Sensor Reset After");Serial.println(sensorReset);
     }
  pinMode(switchPin, INPUT); //Switch Input
  digitalWrite(switchPin, HIGH);

You have the internal pullup enabled on the input pin, the switch must be wired between input and GND. It will read HIGH when open, LOW when closed.

Why are you trying to write to the switch INPUT?

 pinMode(switchPin, INPUT); //Switch Input
  digitalWrite(switchPin, HIGH);

Why aren’t you using the internal pull-up?
When you had an external one, which rail did you connect it to?

pcbbc:
Why are you trying to write to the switch INPUT?

That's the way internal pullups were enabled before "INPUT_PULLUP" was invented.

I always watch for:

pinMode(buttonPin,INPUT); // no pullup enable

Then, in loop():

if(digitalRead(buttonPin) == HIGH) // might always == HIGH ?