How to get Array Position State with digitalRead ?

Hi, this is my first post so if this belongs to another section please move (and sorry)

I’m trying to read a dip switch state to change a variable, and use that variable into a switch.
Problem is, I don’t understand how to get the State value from the dip switch pin. I believe it to have something to do with the dip switch pins being into an Array.

I had already read the docs about digitalRead and searched info about dip switch, but almost anyone use arrays or seems harder to understand (for me, I’m a student and this is my 3rd time with Arduino)

Hope you can help me or give me some guidance, thanks =)

Edit: I found out the dip switch needs to be powered and that’s why all pins are LOW, but i don’t know how to exactly do that.

This is my code:

int dipPins[] = {2, 3, 4, 5}; //dip switch pins
int led[] = {6, 7, 8, 9, 10, 11, 12, 13}; //led pins
int var = 0; //switch variable

void setup() {
  //declares input pins for dip switch
  for(int i = 0; i < 4; i++){
    pinMode(dipPins[i], INPUT);

    //if dip switch state == HIGH assign array position to var??
    //here is my problem
    if(digitalRead(dipPins[i]) == HIGH){
      var = i + 1;
    }
  }

  //declare led pins
  for(int i = 0; i < 8; i++){
    pinMode(led[i], OUTPUT);
  }
}

//all in here is tested ok, it just turns leds on in diferent patterns
void loop() {
  switch(var){
    case 1:
      digitalWrite(led[0], HIGH);
      delay(1000);
      digitalWrite(led[0], LOW);
      delay(500);
    break;
    case 2:
      for(int j = 0; j < 4; j++){
        digitalWrite(led[j], HIGH);
        delay(1000);
        digitalWrite(led[j], LOW);
        delay(500);
      }
    break;
    case 3:
      for(int j = 0; j < 4; j++){
        digitalWrite(led[j], HIGH);
        delay(1000);
        digitalWrite(led[j], LOW);
        delay(500);
      }
      for(int j = 3; j >= 0; j--){
        digitalWrite(led[j], HIGH);
        delay(1000);
        digitalWrite(led[j], LOW);
        delay(500);
      }
    break;
    case 4:
      for(int j = 0; j < 6; j++){
        digitalWrite(led[j], HIGH);
        digitalWrite(led[j + 2], HIGH);
        delay(1000);
        digitalWrite(led[j], LOW);
        digitalWrite(led[j + 2], LOW);
        delay(500);
      }
    break;
    default:
    break;
  }
}

Arduino: Elegoo Uno R3
OS: Manjaro xfce

If the goal is to read the settings of the dip switches into an array, you have to define an array:

bool dipSwitch[ 4 ] ;

Then later load the array (could be more compact and could be inverted):

if(digitalRead(dipPins[ i ] ) == LOW){
dipSwitch[ i ] = LOW ;
}
else {
dipSwitch[ i ] = HIGH;
}

Important also to know is how the dip switches are wired and what you are going to do with pull up or pull down resistors.

6v6gt:
Important also to know is how the dip switches are wired and what you are going to do with pull up or pull down resistors.

Yes i just realized i need to send power to the dip switch.. Can you send me some basic tutorial for doing that? I'm searching but many people do really complicated projects I don't understand

power to the dip switch. Can you send me some basic tutorial

The dip switch is just like a button, for which there are many tutorials, and one or more examples in the Arduino IDE. Google "Arduino read button".

You will need a pullup or pulldown resistor (the pullup can be internal to the Arduino).

Here is such a tutorial :
https://learn.sparkfun.com/tutorials/pull-up-resistors/what-is-a-pull-up-resistor

Wow thank you so much! I followed the link and read the info related and now i have it working! Almost…

When the loop starts it gets into the for loop and change the “var” variable, but when the loop restarts seems to not enter into the loop to reverify the dip switch states.

This is my code now:

// global variables
int dipPins[] = {2, 3, 4, 5}; //pines dip switch
int led[] = {6, 7, 8, 9, 10, 11, 12, 13}; //pines leds
int var = 0;

void setup() {
  //dip switch pins
  for(int i = 0; i < 4; i++){
    pinMode(dipPins[i], INPUT);
  }

  //led pins
  for(int i = 0; i < 8; i++){
    pinMode(led[i], OUTPUT);
  }
}

void loop() {
  //here is the loop I'm using to verify the dip switch state
  //but seems to run just the first time the void loop runs
  for(int i = 0; i < 4; i++){
    if(digitalRead(dipPins[i]) == 0){
      var = i + 1;
    }
  }
  
  //everything's fine from here
  switch(var){
    case 1:
      digitalWrite(led[0], HIGH);
      delay(1000);
      digitalWrite(led[0], LOW);
      delay(500);
    break;
    case 2:
      for(int j = 0; j < 4; j++){
        digitalWrite(led[j], HIGH);
        delay(1000);
        digitalWrite(led[j], LOW);
        delay(500);
      }
    break;
    case 3:
      for(int j = 0; j < 4; j++){
        digitalWrite(led[j], HIGH);
        delay(1000);
        digitalWrite(led[j], LOW);
        delay(500);
      }
      for(int j = 3; j >= 0; j--){
        digitalWrite(led[j], HIGH);
        delay(1000);
        digitalWrite(led[j], LOW);
        delay(500);
      }
    break;
    case 4:
      for(int j = 0; j < 6; j++){
        digitalWrite(led[j], HIGH);
        digitalWrite(led[j + 2], HIGH);
        delay(1000);
        digitalWrite(led[j], LOW);
        digitalWrite(led[j + 2], LOW);
        delay(500);
      }
    break;
    default:
    break;
  }
}

Throw a serial print inside that for loop to prove to yourself that it is running or not.

You should move the code set "var" to setup(). It should not be in the loop().

What if he wants to read the dips every time through?

Ah yes. That could indeed be the case. I had assumed the dip switches were for “configuration”, not something for “runtime”.

Anyway, he’ll also have a problem that the last iteration of the for loop determines var. It looks like he is attempting to count the number of set switches, irrespective of their position, to determine the value of var.

I guess then it should be something like this:

  //here is the loop I'm using to verify the dip switch state
  //but seems to run just the first time the void loop runs
  var = 1 ;
  for(int i = 0; i < 4; i++){
    if(digitalRead(dipPins[i]) == 0){
      var++;
    }
  }

I was guessing he was only interested in the state of the last switch. I'd dip0 and dip1 are both on then I think he just wants to know about dip1.

Karma to the OP for using code tags in his first post. That's a rarity.

Delta_G:
What if he wants to read the dips every time through?

Yes, I want to check the dip switch state at runtime every time =)

Delta_G:
I was guessing he was only interested in the state of the last switch. I'd dip0 and dip1 are both on then I think he just wants to know about dip1.

Well I have 4 cases for the dip switch. I want to check through the four pins to verify which one is open to change the switch var accordingly.

DangerToMyself:
Karma to the OP for using code tags in his first post. That's a rarity.

Thanks! I had read the "How to use.." topic =p

Well I have 4 cases for the dip switch. I want to check through the four pins to verify which one is open to change the switch var accordingly.

For a choice of 4 values of var , you need only 2 dip switches. 4 dip switches actually gives you 16 cases:

0000
0001
0010
0011
0100
etc.

What value should var have if none of the dip switches is set ?

Delta_G:
Throw a serial print inside that for loop to prove to yourself that it is running or not.

In my protoboard I have selected option 1, but serial monitor displays the four options as “1” like:

2 - 4 - 1
3 - 4 - 1
4 - 4 - 1
5 - 4 - 1

The switch is entering case: 4. I believe the state is not changing the “var” variable, the for is just looping until the end and “var” gets equal 4.

for(int i = 0; i < 4; i++){
    if(digitalRead(dipPins[i]) == 0){
      var = i + 1;
    }
    Serial.print(dipPins[i]);
    Serial.print(" - ");
    Serial.print(var);
    Serial.print(" - ");
    Serial.println(digitalRead(dipPins[i]));
  }

I tried with the following if-else if statement and isn’t working too. It catch the “var” the first time, but later didn’t recognize the dip switch change at runtime.

if(digitalRead(dipPins[0]) == 0){
    var = 1;
  }
  else if(digitalRead(dipPins[1]) == 0){
    var = 2;
  }
  else if(digitalRead(dipPins[2]) == 0){
    var = 3;
  }
  else if(digitalRead(dipPins[3]) == 0){
    var = 4;
  }

6v6gt:
For a choice of 4 values of var , you need only 2 dip switches. 4 dip switches actually gives you 16 cases…

I thought to received HIGH/LOW or 0/1 only. In which case yes, i can use only 2. I’ll change that at the end! Thanks!

2 - 4 - 1
3 - 4 - 1
4 - 4 - 1
5 - 4 - 1

If none of the pins was low var would keep it's old value. In the output you posted, all the pins read HIGH, so it never went into the if statement and therefore never changed the value of var.

Delta_G:
If none of the pins was low var would keep it's old value. In the output you posted, all the pins read HIGH, so it never went into the if statement and therefore never changed the value of var.

You're right, i had the resistors in the wrong position and that's why the bad lecture. Thank you, now everything is working ok! =D