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