Good morning Mates,
I have an on going project that has 3 DIP switches to control 6 different modes. for example
Mode1
DIP SW1=0
DIP SW2=0
DIP SW3= 1
Mode2
DIP SW1=0
DIP SW2=1
DIP SW3= 0
Mode3
DIP SW1=1
DIP SW2=0
DIP SW3= 0
Mode4
DIP SW1=0
DIP SW2=1
DIP SW3= 1
Mode5
DIP SW1=1
DIP SW2=1
DIP SW3= 0
Mode6
DIP SW1=0
DIP SW2=0
DIP SW3= 0
my question is how can I convert the value of each DIP switch as hex so that I can avoid long programming. please check below code
if (alt < 0 && DIP1 ==0 && DIP2 ==0 && DIP3 ==1 || Blt >0 && DIP1 ==0 && DIP2 ==1 && DIP3 ==0 )
alt is ultrasonic sensor
blt LDR
it is much better if I can convert the DIP switch values as HEX Bits like this one
Mode1
4
Mode2
2
Mode3
6
Mode4
3
Mode5
0
Mode
7
sample code will be:
if (alt < 0 && DIP == 4 || Blt >0 && DIP==2 )
hope somebody can help me with this problem thanks in advance
u have 3 dip switches..
just read the three switches, binary weight them and combine them to an integer..
such as..
int DIP;
DIP=digitalRead(dip1)+digitalRead(dip2)x2+digitalRead(dip3)x4;
syntax may not be exact, but u can see the concept
Why not treat each switch as a binary bit. Then:
Mode1 = 4
Mode2 = 2
Mode3 = 1
Mode4 = 6
Mode5 = 3
Mode6 = 0
Then you could write
byte findBits()
{
byte sum = 0;
if (sw1) // You'd need code to read switch states, like sw1 = digitalRead(dip1);
sum = 1;
if (sw2)
sum += 2;
if (sw3)
sum += 4;
return sum;
}
Then this:
if (alt < 0 && DIP1 ==0 && DIP2 ==0 && DIP3 ==1 || Blt >0 && DIP1 ==0 && DIP2 ==1 && DIP3 ==0 )
can be replaced with:
byte switchValues = findBits();
// more code...
if ( (alt < 0 && switchValues == 4) || (Blt >0 && switchValues == 2) )
I would use #define's or enums for the numeric constants above to make it easier to read the code.
backwoodsjack:
u have 3 dip switches..
just read the three switches, binary weight them and combine them to an integer..
such as..
int DIP;
DIP=digitalRead(dip1)+digitalRead(dip2)x2+digitalRead(dip3)x4;
syntax may not be exact, but u can see the concept
thanks backwoodsjack ill try this code im in the office right now and I don't have access on my PC. very simple.
I just want to clarify things. the reason why you multiply dip2 to 2 and dip3 to 4 is because of the value of the Dip2 and dip3 falls on 2 and 4 value in the hex. im I right?
@econjack thanks for the help mate ill try it when I get home I don't have access on my PC ill try this one too. im analyzing the code right now to better understand the code. thanks guy for the help salute!!!
It's always best to explicitly check for HIGH or LOW when doing a digital read. I'm assuming here we are using the usual INPUT_PULLUP convention.
byte settings = (digitalRead(DIP1)==LOW?4:0)
| (digitalRead(DIP2)==LOW?2:0)
| (digitalRead(DIP3)==LOW?1:0);
switch(settings) {
case 0: // MODE 6
break;
case 1: // MODE 1
break;
case 2: // MODE 2
break;
case 3: // MODE 4
break;
case 4: // MODE 3
break;
case 5: // this never happens ??
break;
case 6: // MODE 5
break;
case 7: // this never happens ??
break;
}
PaulMurrayCbr:
byte settings = (digitalRead(DIP1)==LOW?4:0)
| (digitalRead(DIP2)==LOW?2:0)
| (digitalRead(DIP3)==LOW?1:0);
switch(settings) {
case 0: // MODE 6
break;
case 1: // MODE 1
break;
case 2: // MODE 2
break;
case 3: // MODE 4
break;
case 4: // MODE 3
break;
case 5: // this never happens ??
break;
case 6: // MODE 5
break;
case 7: // this never happens ??
break;
}
Thanks @PaulMurrayCbr I rewrite you code using if else function do you think this will work I don't have access on Arduino right now I just want to check if this will work
byte settings = (digitalRead(DIP1)==LOW?4:0) | (digitalRead(DIP2)==LOW?2:0) | (digitalRead(DIP3)==LOW?1:0);
if (alt < 0 && settings == 4 || Blt >0 && settings==2 )
{
action A
}
else if (alt < 20 && settings == 1 || Blt >45 && settings==3 )
{
action B
}
else if (alt < 300 && settings == 7 || Blt >125 && settings==5 )
{
action C
}
using @econjack code I come up with this code what do you think guys??? will this work I don't have access on arduino right now im just assuming base on the code and my personal experience.
#define dipsw1 4
#define dipsw2 5
#define dipsw3 6
int DIP = 0;
void setup()
{
pinMode(dipsw1, INPUT_PULLUP);
pinMode(dipsw2, INPUT_PULLUP);
pinMode(dipsw3, INPUT_PULLUP);
}
void loop()
{
DIP=digitalRead(dipsw1)+digitalRead(dipsw2)x2+digitalRead(dipsw3)x4;
if (alt < 0 && DIP == 4 || Blt >0 && DIP==2 )
{
action A
}
else if (alt < 20 && DIP == 1 || Blt >45 && DIP==3 )
{
action B
}
else if (alt < 300 && DIP == 7 || Blt >125 && DIP==5 )
{
action C
}
}