My Code doesnt have issues, but it doesn't work

Hi, I am super new to ardiuno. I have LEDs and a dip switch. I am trying to make the dip switch work as a binary thing. i.e. if switch one is on and switch two and three are off the turn on a light. I obviously also need it to turn the light off if this condition is not met. I have been playing around with the code and this is what I found as a sollution

const byte LIGHT_13 = 13;
const byte LIGHT_12 = 12;
const byte LIGHT_11 = 11;
const byte LIGHT_10 = 10;
const byte LIGHT_9 = 9;
const byte LIGHT_8 = 8;
const byte LIGHT_7 = 7;

const byte SWITCH_1_ = 2;
const byte SWITCH_2_ = 3;
const byte SWITCH_3_ = 4; 


void setup() {
  // put your setup code here, to run once:
pinMode (LIGHT_13, OUTPUT);
pinMode (LIGHT_12, OUTPUT);
pinMode (LIGHT_11, OUTPUT);
pinMode (LIGHT_10, OUTPUT);
pinMode (LIGHT_9, OUTPUT);
pinMode (LIGHT_8, OUTPUT);
pinMode (LIGHT_7, OUTPUT);

pinMode (SWITCH_1_, INPUT);
pinMode (SWITCH_2_, INPUT);
pinMode (SWITCH_3_, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
 if (digitalRead(((SWITCH_1_ == HIGH) && (SWITCH_2_ == LOW) && (SWITCH_3_ == LOW)))) {
  digitalWrite(LIGHT_13, HIGH);
 } else {
  digitalWrite(LIGHT_13, LOW);
 }
 }

This code verifies, but when I upload it the LED is constanly on.
If what I am saying does not make sense I am happy to answer clarifying questions.

Your code isn't complete, so anything could be wrong, or not.

1 Like

Please post the whole code.

At a guess I'd think it would be something with how the switches and LED are wired and whether or not there is a pullup.

You can test those sorts of problems by making sure you can run the built-in examples on your hardware and making Blink work on your current LED connection and making Button work on your current dip switch connections.

If you don't have actual pull-up/pull-down resistors on the DIP switch, consider using INPUT_PULLUP per https://docs.arduino.cc/learn/microcontrollers/digital-pins/

pinMode(pinNum,INPUT_PULLUP);

That and all the others are wrong. The evaluation of that is either false (0) or true (1) so e.g. digitalRead(SWITCH_1_ == HIGH) will either read pin 0 or pin 1.

You need three digitalReads, one for each pin and combine those.

As mentioned above, post you full code

2 Likes

Sorry about that, just did

What do you mean by combining them. As far as I understand, thats what I did. I posted the full code, sorry about that.

You need a separate digitalRead for each pin

That means only that the C/C++ language syntax is correct, not that the program will do anything useful.

You need to learn how to use digitalRead(), e.g.

if (digitalRead(pin) == HIGH) do_something();

There are a bunch of getting started examples in the Arduino IDE, File>Examples> etc.

if it not work - it is not a solution

This is one digitalRead() on what essentially translates into 0 if you work out the math on the constants inside the parentheses:

Your code does not what you think.
You have at mimium three wrong assumptions how this code works

if (digitalRead(((SWITCH_1_ == HIGH) && (SWITCH_2_ == LOW) && (SWITCH_3_ == LOW))))

SWITCH_1_ == HIGH:

SWITCH_1_ has value 2 so all in all this part is doing

2 == HIGH:

HIGH isvalue1

so all in all you are executing

2 == 1 which results in "false" where "false" is value 0

So the result of "2 == 1" is 0

This part
(SWITCH_1_ == HIGH) results in 0

same thing for
(SWITCH_2_ == LOW) results in 0

(SWITCH_3_ == LOW) results in 0

all in all you are executing

0 && 0 && 0 which finally results in 0

so what you line of code
if (digitalRead(((SWITCH_1_ == HIGH) && (SWITCH_2_ == LOW) && (SWITCH_3_ == LOW))))

is really doing is
if( digitalRead(0) )

which means you are reading IO-pin number 0

and depending on IO-pin beeing HIGH the condition would result in true
on IO-pin beeing LOW the condition would result in false

again:
You have at mimium three wrong assumptions how this code works

you should really learn how to use digitalRead with a much simpler example
and work up from there

nobody here should make you stumble forward faster by presenting a working solution
You should do some more basic exercises before you work on this combining

1 Like
const byte LEDs = 7;
const byte LIGHT[LEDs] = {13, 12, 11, 10, 9, 8, 7};

const byte SWITCHes = 3;
const byte SW[SWITCHes] = {2, 3, 4};

void setup() {
  for (int i = 0; i < LEDs; i++)    pinMode (LIGHT[i], OUTPUT);
  for (int i = 0; i < SWITCHes; i++)pinMode (SW[i], INPUT_PULLUP);
}

void loop() {
  byte state = 0;
  for (int i = 0; i < SWITCHes; i++)state |= (digitalRead(SW[i])==LOW)&(1<< i);
  state--;
  for (int i = 0; i < LEDs; i++)    digitalWrite(LIGHT[i], state == i);
}

@kolaha: I guess your code makes user @burtus_g stumble forward high speed

but I guess from his very basic knowledge he does not understand a single detail of your code

And you think this is a good way of learning? I don't think this is a good way to learn

To follow the API (I know, I don't either) this should be

  for (int i = 0; i < SWITCHes; i++) state |= (digitalRead(SW[i]) == HIGH) ? (1 << i) : 0;

or LOW for pulled up inputs.


@burtus_g may see this a bit (see what I did there?) faster:

   state = digitalRead(SWITCH1) * 4 + digitalRead(SWITCH2) * 2 + digitalRead(SWITCH3);

Yeah, I know. Bad meeting of the API. So sue me.

a7

1 Like

you are right, but it inverting only dip-switch position

With a little editing on the if() conditions, it works.

1 Like

A DIP switch is only SPST, and could have unreliable pull-up/pull-down issues if not carefully wired.

void setup() {
  DDRB |= 0x3f;
  DDRD |= 0x80;
  PORTD |= 0x1c;
}

void loop() {
  uint8_t state = PIND >> 2 & 0x7;
  for (int i = 0; i < 7; i++)digitalWrite( i+7, state == i);
}