Go Down

### Topic: How to get Array Position State with digitalRead ? (Read 754 times)previous topic - next topic

#### drxm

##### Nov 15, 2017, 05:05 amLast Edit: Nov 15, 2017, 05:41 am by drxm Reason: updated information
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:

Code: [Select]
`int dipPins[] = {2, 3, 4, 5}; //dip switch pinsint led[] = {6, 7, 8, 9, 10, 11, 12, 13}; //led pinsint var = 0; //switch variablevoid 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 patternsvoid 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

#### 6v6gt

#1
##### Nov 15, 2017, 05:40 am
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.

#### drxm

#2
##### Nov 15, 2017, 05:46 am
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

#### jremington

#3
##### Nov 15, 2017, 05:57 am
Quote
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).

#### 6v6gt

#4
##### Nov 15, 2017, 06:06 am
Here is such a tutorial :
https://learn.sparkfun.com/tutorials/pull-up-resistors/what-is-a-pull-up-resistor

#### drxm

#5
##### Nov 15, 2017, 10:38 pmLast Edit: Nov 15, 2017, 10:48 pm by drxm
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:

Code: [Select]
`// global variablesint dipPins[] = {2, 3, 4, 5}; //pines dip switchint led[] = {6, 7, 8, 9, 10, 11, 12, 13}; //pines ledsint 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;  }}`

#### Delta_G

#6
##### Nov 15, 2017, 10:43 pm
Throw a serial print inside that for loop to prove to yourself that it is running or not.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

#### 6v6gt

#7
##### Nov 15, 2017, 10:45 pm
You should move the code set "var" to setup(). It should not be in the loop().

#### Delta_G

#8
##### Nov 15, 2017, 10:58 pm
What if he wants to read the dips every time through?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

#### 6v6gt

#9
##### Nov 15, 2017, 11:10 pmLast Edit: Nov 15, 2017, 11:12 pm by 6v6gt
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:
Code: [Select]
`  //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++;    }  }`

#### Delta_G

#10
##### Nov 15, 2017, 11:32 pm
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.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

#### DangerToMyself

#11
##### Nov 15, 2017, 11:50 pm
Karma to the OP for using code tags in his first post. That's a rarity.

#### drxm

#12
##### Nov 16, 2017, 12:09 am
What if he wants to read the dips every time through?
Yes, I want to check the dip switch state at runtime every time =)

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.

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

#### 6v6gt

#13
##### Nov 16, 2017, 12:22 am
Quote
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:

Code: [Select]
`00000001001000110100etc.`

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

#### drxm

#14
##### Nov 16, 2017, 12:43 amLast Edit: Nov 16, 2017, 01:12 am by drxm
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.

Code: [Select]
`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.

Code: [Select]
`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;  }`

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!

Go Up