Go Down

Topic: LED Switches control problem (Read 503 times) previous topic - next topic

mkleinhentz

Good evening !

I am actually working on a video switcher project, in which LED pushswitches are needed. I am desperately trying to control them via an Arduino UNO, but none of my trials seem to work properly. After doing some research on the Internet, I wired up this simple circuit, with some code.

What I am trying to do is just to get a reading from each button's state, and have it light up when it is pushed. What it actually does : lights up everything and stays like this no matter what I do.

Can somebody please help me ?

I join pictures of my circuit, and a small schematic (disclaimer : I am just a hobbyist !)

Here is my code :

Code: [Select]
int SW1 = 8;
int SW2 = 9;
int SW3 = 10;

int LED1 = 2;
int LED2 = 3;
int LED3 = 4;

void setup() {
  pinMode(SW1, INPUT);
  pinMode(SW2, INPUT);
  pinMode(SW3, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);

  digitalWrite(LED1, HIGH);
  delay(200);
  digitalWrite(LED1, LOW);
  delay(200);
  digitalWrite(LED2, HIGH);
  delay(200);
  digitalWrite(LED2, LOW);
  delay(200);
  digitalWrite(LED3, HIGH);
  delay(200);
  digitalWrite(LED3, LOW);
  delay(200);
}

void loop() {
  if (digitalRead(SW1 == HIGH)) {
    digitalWrite(LED1, HIGH);
  }
  else if (digitalRead(SW1 == LOW)) {
    digitalWrite(LED1, LOW);
  }

  if (digitalRead(SW2 == HIGH)) {
    digitalWrite(LED2, HIGH);
  }
  else if (digitalRead(SW2 == LOW)) {
    digitalWrite(LED2, LOW);
  }

  if (digitalRead(SW3 == HIGH)) {
    digitalWrite(LED3, HIGH);
  }
  else if (digitalRead(SW3 == LOW)) {
    digitalWrite(LED3, LOW);
  }
}



PaulRB

#1
May 24, 2016, 10:27 pm Last Edit: May 24, 2016, 10:38 pm by PaulRB
Please attach the schematic. The picture is difficult to follow.


Also:
Code: [Select]
 if (digitalRead(SW1 == HIGH)) {
should be:
Code: [Select]
 if (digitalRead(SW1) == HIGH) {

And:
Code: [Select]
  else if (digitalRead(SW1 == LOW)) {
can simply be:
Code: [Select]
  else {
because if the input pin is not HIGH, it must be LOW, there is no need to check.

But you could simply write:
Code: [Select]
digitalWrite(LED1, digitalRead(SW1));

Go Up