Pages: [1]   Go Down
Author Topic: DIP Switch 8 Position to address Sensor  (Read 4109 times)
0 Members and 1 Guest are viewing this topic.
Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: November 19, 2009, 04:15:45 am by abocanegra » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25735
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: November 19, 2009, 04:22:06 am by AWOL » Logged

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

Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-cool. 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:
//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
}
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33328
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Untested code but try this:-

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

« Last Edit: November 19, 2009, 08:47:20 am by Grumpy_Mike » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25735
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
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
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33328
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

« Last Edit: November 20, 2009, 03:42:18 am by Grumpy_Mike » Logged

Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//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
Logged

B0100111001000011, USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1503
I'm confused.  Wait, maybe not..
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thought provoking... smiley-wink
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: December 11, 2009, 11:52:08 pm by Osgeld » Logged


B0100111001000011, USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1503
I'm confused.  Wait, maybe not..
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: