Problem with IR receiver and ultrasonic sensor together in a project

Hi. I'm pretty new to sensors and stuff, so I am having some problems with one project. So, what I want to do is: there is a slideswitch, an led, ultrasonic sensor and an IR receiver on my breadboard. Ultrasonic sensor will not print the distance from some object if the slideswitch is on the left. If the slideswitch is on the right(thats means its on), the ultrasonic sensor will print the distance. If the IR receiver actually gets some code from the IR remote, it will light the led up, no matter on what side the slideswitch is. But the problem is, when the slideswitch is on the left(that means the ultrasonic sensor should not print the distance on the serial monitor), and I press a button on the IR remote, the distance prints on the serial monitor. I dont know why. Hope you can help me. Thanks in advance.

The code:

# include <IRremote.h>
int led = 8;
int trig = 4;
int echo = 3;
int recPin = 11;
int duration;
int distance;
int rightSwitchSide = 13;
int rightSwitchSideValue;
IRrecv irrecv(recPin);
decode_results results; 


void setup() {
  Serial.begin(9600);
  pinMode(rightSwitchSide, INPUT);
  pinMode(led, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(trig, OUTPUT);
  irrecv.enableIRIn();
  irrecv.blink13(true);
  

}

void loop() {
  rightSwitchSideValue = digitalRead(rightSwitchSide);
    if(irrecv.decode(&results)){
      digitalWrite(led, HIGH);
      irrecv.resume();
    }

  if(rightSwitchSideValue == HIGH){

  digitalWrite(trig, LOW);
  delayMicroseconds(10);
  digitalWrite(trig, HIGH);
  delayMicroseconds(500);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;
  Serial.println(distance);
  }

  
}

This shows up when I press a button and the slideswitch is on the left(the distance isnt always 4):

4
4
4
4
4
4

How exactly is your switch wired? It sounds like the input pin is floating (not connected to anything) when it is switched to the left. It probably needs a pull down resistor to ground.

Steve

Hi. It is correctly wired. Like this:

https://www.google.com/search?biw=1440&bih=758&tbm=isch&sa=1&ei=gmfQXOaYJMSQkwWProj4Dw&q=slideswitch+arduino&oq=slideswitch+arduino&gs_l=img.3...1674.7330..7447...2.0..0.90.1397.21......0....1..gws-wiz-img.......0i67j0j0i30j0i10i24.OWZm4uzVdKE#imgrc=CohhCA_4jNbprM:

Ehh... here is the attachment instead.