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!
