Problem with slideswitch

Hey everyone! I need help with using a slideswitch to turn on an LED or a DC Motor depending on its state.
I wrote the code specifying that when its on HIGH, it turns on the LED in a specific pattern and the DC Motor in a specific direction, and when its on LOW, the LED stays off and the motor spins in the opposite direction. But when i run the simulation it acts as if the switch was always on HIGH, and depending on how I place the resistance and connections sometimes it acts as if it was always on LOW. Here’s my code:

int ROJO=2;
int AZUL=4;
int VERDE=7;
int contador=1;
unsigned long tiempoinicial=0;
int intervalo=1000;
int deslizador=12;
int enable=8;
int sentido1=11;
int sentido2=10;

void setup()
{
  pinMode(ROJO,OUTPUT);
  pinMode(AZUL,OUTPUT);
  pinMode(VERDE,OUTPUT);
  pinMode(deslizador,INPUT);
  pinMode(enable,OUTPUT);
  pinMode(sentido1,OUTPUT);
  pinMode(sentido2,OUTPUT);
  digitalWrite(enable,HIGH);
  
  
  
}

void loop()
{ int estadoDelSwitch= digitalRead(deslizador);
 if (digitalRead(deslizador)==HIGH)
 {digitalWrite(sentido1,HIGH);
   digitalWrite(sentido2,LOW);
   unsigned long tiempoActual=millis();
  if (tiempoActual- tiempoinicial>=intervalo)
     {tiempoinicial=tiempoActual;
      if (contador==1)
         {
           digitalWrite(ROJO,HIGH);
           digitalWrite(VERDE,LOW);
          digitalWrite(AZUL,LOW);
        contador=2;}
      else if (contador == 2)
      {
        digitalWrite(VERDE,HIGH);
       digitalWrite(ROJO,LOW);
       digitalWrite(AZUL,LOW);
      contador=3;}    
      else if (contador == 3)
      {contador=1;
      digitalWrite(AZUL,HIGH);
  digitalWrite(VERDE,LOW);
       digitalWrite(ROJO,LOW);}}
 }
 else
 { digitalWrite(ROJO,LOW);
           digitalWrite(VERDE,LOW);
          digitalWrite(AZUL,LOW);
 digitalWrite(sentido1,LOW);
 digitalWrite(sentido2,HIGH);
 }
}

Attached is a picture of the circuit

Any help would be appreciated!

OP's picture:

Yup, you've definitely got that switch wired up wrong. When it is to the left your pin is connected to ground, but when the switch is to the right you've got ground and hot connected through a resistor and nothing connected to your input pin.

Take the resistor loose from the pin on the right of the switch and put it on the one on the left that goes to the input pin. Leave the middle one on ground. Don't connect anything to the one on the right. That would leave you in a pull-up style switch. If would read low when the switch is on the left because it is connected directly to ground and high when it is on the right because it is connected to hot through the resistor.

When it is to the left your pin is connected to ground,

There does not appear to be a GND connection to the negative bus bar of the breadboard unless it is coming from pin 16 of the IC

Thanks for you answers!
I connected the switch the way you said but it still acts like it was always on LOW
This is how it's connected now

I've tried different configurations and sometimes it stays HIGH and other times it stays LOW

Looks like you need the Arduino pin connected to the center pin of the slide switch, not an end pin.

Paul

It finally worked! I connected the arduino to the center pin, with GND on one side and the 5V on the other and now it works like a charm! Thanks everyone for your replies!

Hope it is a break before make switch then so you don't short out your supply pins.

Covedrim:
It finally worked! I connected the arduino to the center pin, with GND on one side and the 5V on the other and now it works like a charm! Thanks everyone for your replies!

Delta_G:
Hope it is a break before make switch then so you don't short out your supply pins.

Yeah, don't do it like that. Wire your pin to one side of the switch. Wire the center of the switch to ground. When you initialize the switch do it like this:

     pinMode(switchPin, INPUT_PULLUP);

Then when you read the switch, do this:

     switchState = [b]![/b]digitalRead(switchPin);

This minimizes the external hardware by using the internal pull-up resistor, and is very reliable.