How do I use a rocker switch with ardino

Hi! I’m trying to use a rocker switch, but the code I wrote doesn’t work. It just switches the relay on and off rapidly.
I have the switch connected to D3 and the relay to D2
The code:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
float temp;
float cold;
float hot;
int tempPin = A1;
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
const byte switchPin = 3;  //give the switch pin a name
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

void setup()
{

  tone(11, 700 ,100);
  delay(100);
    tone(11, 800 ,100);
  delay(100);
    tone(11, 1000 ,100);
  delay(100);
    tone(11, 900 ,100);
  delay(100);
    tone(11, 1000 ,200);
  delay(200);
  Serial.begin(9600);
  pinMode(switchPin, INPUT_PULLUP);
  // Print a message to the LCD.
  pinMode(2, OUTPUT);
      lcd.init();                      // initialize the lcd 
    lcd.backlight();
        lcd.setCursor(0,0);
  lcd.print("Hello!");
      lcd.setCursor(0,1);
  lcd.print("All gud!");
delay(2000);

}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

void loop()
{
  byte switchState = digitalRead(switchPin);
  analogRead(tempPin);
  temp = analogRead(tempPin);
  analogRead(A7);
   // read analog volt from sensor and save to variable temp
   temp = temp * 0.48828125;
   // convert the analog volt to its temperature equivalent
   analogRead(A7);
   analogRead(tempPin);
   analogRead(A7); //NEED IT DONT DELETE
float  set = map(analogRead(A2), 0, 1023, 60, 20);   
cold = set - 2;
hot = set + 2;
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

if (temp > hot && digitalRead(3) == LOW) {
delay(1000);
  if (temp > set) {
  digitalWrite(2, HIGH);
  lcd.setCursor(0,0);
  lcd.print("     Fan/ON     ");
    lcd.setCursor(0,1);
  lcd.print("Act temp:  ");
  lcd.print(temp);
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

}
  if (temp < cold) {
  digitalWrite(2, LOW);
  digitalWrite(10, LOW);
 lcd.setCursor(0,0);
  lcd.print("Act Temp:  ");
  lcd.print(temp);
  lcd.setCursor(0,1);
  lcd.print("Set Temp:  ");
  lcd.print(set);
  lcd.print("   ");
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//this is the button code
if (switchState == HIGH)  //if it is LOW then the switch is closed
  {
    digitalWrite(2, HIGH); //Turn on the relay
  }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
if (switchState == LOW)  //if it is LOW then the switch is closed
  {
    digitalWrite(2, LOW); //Turn off the relay  }
}

Why do you have temperature based control of the relay and immediately after it, just switching the relay based on the state of the button?

   analogRead(tempPin);
   analogRead(A7); //NEED IT DONT DELETE
float  set = map(analogRead(A2), 0, 1023, 60, 20);

This is kind of "fishy". Which analog pin are you trying to read? You have 3 there.

The other analog reads are there because there is interfering with the other analog reads. For some reason, it fixes the interfering.

The A7 is for the interfering fix. The tempin is my temp sensor, and the A2 is my potentiometer

Please help!

Calling a switch a "rocker switch" only defines the movement, not the configuration, the number of connections, whether the switch is momentary or will stay in position. Some rocker switches are 3-position. Some operate slide switches which can have many terminals. What exactly do you have and how do yo have it wired?

Paul

I have a 3 leg rocker switch, one of them to ground, D3, 5V