Los Angeles
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« on: November 19, 2009, 04:02:29 am » |
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
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: November 19, 2009, 04:19:11 am » |
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). 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.
|
|
|
|
Los Angeles
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #2 on: November 19, 2009, 07:58:30 am » |
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  . I created 3 situations: - 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 }
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25517
Solder is electric glue
|
 |
« Reply #3 on: November 19, 2009, 08:46:02 am » |
Untested code but try this:- //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
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: November 19, 2009, 08:52:45 am » |
Is there a simpler and cleaner method. I am currently getting 15 possible addresses from 4 switches 2 4 = 16.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Los Angeles
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #5 on: November 19, 2009, 05:09:22 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25517
Solder is electric glue
|
 |
« Reply #6 on: November 20, 2009, 03:40:27 am » |
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
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #7 on: December 11, 2009, 10:34:37 pm » |
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 //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
Edison Member
Karma: 0
Posts: 1503
I'm confused. Wait, maybe not..
|
 |
« Reply #8 on: December 11, 2009, 11:13:22 pm » |
Interesting.. That means you could get 64 possible settings (8 2) by using an 8-Position DIP switch...  . Thought provoking... 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 315
Posts: 35519
Seattle, WA USA
|
 |
« Reply #9 on: December 11, 2009, 11:32:40 pm » |
With 8 switches, you get 28 possibilities, not 82.
|
|
|
|
|
Logged
|
|
|
|
|
SE USA
Offline
Faraday Member
Karma: 33
Posts: 3624
@ssh0le
|
 |
« Reply #10 on: December 11, 2009, 11:49:08 pm » |
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
|
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
|
|
|
|
B0100111001000011, USA
Offline
Edison Member
Karma: 0
Posts: 1503
I'm confused. Wait, maybe not..
|
 |
« Reply #11 on: December 11, 2009, 11:50:06 pm » |
Wow I'm too tired to be trusted with exponents. LMAO ;D!
|
|
|
|
|
Logged
|
|
|
|
|
|