Go Down

Topic: DIP Switch 8 Position to address Sensor (Read 5187 times) previous topic - next topic

abocanegra

Nov 19, 2009, 10:02 am Last Edit: Nov 19, 2009, 10:15 am by abocanegra Reason: 1
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.

Thanks regardless

AWOL

#1
Nov 19, 2009, 10:19 am Last Edit: Nov 19, 2009, 10:22 am by AWOL Reason: 1
Quote
I have of course tried dozens of approaches


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).

Quote
Also, why is the on functioning as off whether or not I invert the connections.
It's probably floating; pull-ups will fix that.
http://arduino.cc/en/Reference/DigitalWrite
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

abocanegra

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.


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

Grumpy_Mike

#3
Nov 19, 2009, 02:46 pm Last Edit: Nov 19, 2009, 02:47 pm by Grumpy_Mike Reason: 1
Untested code but try this:-

Code: [Select]
//Create and Define Global Variables
int ledPin = 11; // LED connected to digital pin 6
int dipPins[] = {2, 3, 4, 5}; //DIP Switch Pins

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()
{
 Serial.print(address(), DEC);
 Serial.print("\n");
delay(1000);
}

//Create Address from DIP Switch (4 positions used)
byte address(){
 int i,j;
 
 //Get the switches state
 for(i=0; i<=3; i++){
 j = (j << 1) | digitalRead(dipPins[i]);   // read the input pin
 }
   
 return j; //return address
}


AWOL

Quote
Is there a simpler and cleaner method. I am currently getting 15 possible addresses from 4 switches

24 = 16.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

abocanegra

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

Grumpy_Mike

#6
Nov 20, 2009, 09:40 am Last Edit: Nov 20, 2009, 09:42 am by Grumpy_Mike Reason: 1
That doesn't look right. there should be small numbers. Try replacing the bit that says:-
int i,j;
with:-
int i,j=0;

To initialise the variable.
If that doesn't work try :-
j = j & 0x0F;
just before the return.


abocanegra

Thanks again for all your help. I have been working on another element of the project and just got back to this.  Your new code worked perfectly.

The final script to sequence addresses using a dip switch (4 switch) between address 0 and 15

Code: [Select]

//Create and Define Global Variables
int dipPins[] = {2, 3, 4, 5}; //DIP Switch Pins
int transAddress;
void setup()
{
 Serial.begin(2400);
 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
 }
 transAddress = address();
 delay(100);
}

void loop()
{

}

//Create Address from DIP Switch (4 positions used)
byte address(){
 int i,j=0;
 
 //Get the switches state
 for(i=0; i<=3; i++){
 j = (j << 1) | digitalRead(dipPins[i]);   // read the input pin
 }
 return j; //return address
}


This results in the following sequence setup:

0000 - 0
0001 - 1
0010 - 2
0011 - 3
0100 - 4
0101 - 5
0110 - 6
0111 - 7
1000 - 8
1001 - 9
1010 - 10
1011 - 11
1100 - 12
1101 - 13
1110 - 14
1111 - 15

TchnclFl

Interesting.. That means you could get 64 possible settings (82) by using an 8-Position DIP switch... :).

Thought provoking... ;)

PaulS

With 8 switches, you get 28 possibilities, not 82.

Osgeld

#10
Dec 12, 2009, 05:49 am Last Edit: Dec 12, 2009, 05:52 am by Osgeld Reason: 1
aye

each switch has 2 states, on or off

2^8 = 256

binary values:
128,64,32,16,8,4,2,1

binary notation:
B11111111

~OR~

 128
 064
 032
 016
 008
 004
 002
 001
+-----
 255
+----
All zeros
------
256


another example

binary notation:
B10101010

~OR~
128
032
008
002
+----
170
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

TchnclFl

Wow I'm too tired to be trusted with exponents.  LMAO  ;D!

Go Up