So I have a decent amount of experience with arduino, but none with DIP Switches. My problem is that I want to be able to switch addresses as I create more sensors within my array. Each sensor is attached to its own ardiuno. I plan on using the DIP Switch 8 position I bought from Sparkfun to do the job. However, I do not seem to know how to wire it, or read from it, that is how confused I am.
I have searched extensively and found nothing that helped. I have of course tried dozens of approaches, I am currently attaching ground to the on position and digital pin 8 as the off, I have also tried inverting them. What happens is that when i switch to on i get 0 as the value when printed as DEC, when i flip it to off i get 1, and im some instances random noise. I understood that I would be able to get up to 255 possible positions, but without being able to get the proper byte data I would have to manually code each possibility, which seems wrong. Also, why is the on functioning as off whether or not I invert the connections.
Does anybody have any experience with using DIP Switches to control addresses on the arduino, a schematic and brief explanation would go a long way.
Have you tried using the built-in pull-up resistors?
A DIP switch is just an array of individual, unconnected SPST switches; if you can read one, you can read them all (assuming you've got enough spare pins).
Also, why is the on functioning as off whether or not I invert the connections.
Your help made a big difference. However, I feel that my method is still quite sloppy. I want to get the most possible addresses out of 4 switches (i do not have enough room to use all 8). I created 3 situations:
A zero
Each switch is a face value and multiplied by one another (off = 1)
fake a 5th switch (switch 1 = 5 the others are face values multilpied together).
Is there a simpler and cleaner method. I am currently getting 15 possible addresses from 4 switches.
//Create and Define Global Variables
int ledPin = 11; // LED connected to digital pin 6
int dipPins[] = {2, 3, 4, 5}; //DIP Switch Pins
byte val[] = {0,0,0,0}; // assign val
byte value = 0; //create address variable
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // sets the digital ledPin as output
int i;
for(i = 0; i<=3; i++){
pinMode(dipPins[i], INPUT); // sets the digital pin 2-5 as input
digitalWrite(dipPins[i], HIGH); //Set pullup resistor on
}
delay(100);
}
void loop()
{
//address();
//Print Address
Serial.print(address(), DEC);
Serial.print("\n");
}
//Create Address from DIP Switch (4 positions used)
byte address(){
//Set Switch Values
int i;
//Loop True False through the val[]
for(i=0; i<=3; i++){
val[i] = digitalRead(dipPins[i]); // read the input pin
}
//create 0 position
if(val[0] == 0 && val[1] == 0 && val[2] == 0 && val[3] == 0){
for(i=0; i<=3; i++)
{
val[i] = 0;
}
//Fake a switch 5 on Switch 1 if open with 1 or more other switches
}else if(val[0] == 1 && (val[1] == 1 || val[2] == 1 || val[3] == 1)){
val[0] = 5; //set pin 1 to value of 5
for(i=1; i<=3; i++)
{
if(val[i] == 1){ //set face values to pins that are true
val[i] = i+1;
}else if(val[i] == 0){ //set face switch value to 1 for pins that are false
val[i] = 1;
}
}
//Set Switch 1 - 4 at face value
}else{
for(i=0; i<=3; i++)
{
if(val[i] == 1){ //set face values to pins that are true
val[i] = i+1;
}else if(val[i] == 0){ //set face switch value to 1 for pins that are false
val[i] = 1;
}
}
}
//End Switch Values
//Create Address
value = val[0] * val[1] * val[2] * val[3]; //multiply values to create address
analogWrite(ledPin, int(value)); //write to LED to give visual feedback (temporary)
return value; //return address
}
AWOL, thanks, I thought that was what I was gunning for but uncertain.
GRUMPY_MIKE, Very elegant answer. I could not figure out how to get the binary version to work. the bitwise or function is exactly what I was missing. This solves a lot of problems I would have ran into again.
With your answer I was able to get 16 combinations.
Binary - Address
0000 - 144
0001 - 145
0010 - 146
0011 - 147
0100 - 148
0101 - 149
0110 - 150
0111 - 151
1000 - 152
1001 - 153
1010 - 154
1011 - 155
1100 - 156
1101 - 157
1110 - 158
1111 - 159