Dip switches as address

Hi,
I'm hoping someone could help me with my project. I'm trying to set an address to an Arduino uno board that can be used in RS 485 communication protocol, the way I would like to do it is by having a 6 way dip switch on 6 inputs with pull up:
int dip1 = 2;
int dip2 = 3;
int dip3 = 4;
int dip4 = 5;
int dip5 = 6;
int dip6 = 7;
int Address;

void setup() {

Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
}

now the part that I can not get myself ...........

I would like to store the digital.read of all pins to the int Address as follow:

Address (000000) all switches off
Address (100000) all switch 1 on
Address (110000) all switches one and two on
Address (111000) all switches one, two and three on
Address (011000) all switch on off and switches two and three on

This would equivalent to counting in binary.

Your help would be appreciated

Regards

Waldo

you could use bitshift left and binary OR in the buts, e.g.

      int switch=(digitalRead(2)<<8) |( digitalRead(3)<<7)    .....  ;

Hi Horace
This is my code:

int dip1 = 2;
int dip2 = 3;
int dip3 = 4;
int dip4 = 5;
int dip5 = 6;
int dip6 = 7;
int Address;

void setup() {

Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);

}

void loop() {

int Address =(digitalRead(2)<<8) |( digitalRead(3)<<7);

Serial.println (Address);

delay(1000);// put your main code here, to run repeatedly:

}

But I'm not getting the right answer!
Results:
384
384
384

Maybe I'm doing something wrong

Hello wccoetzee

Try this to get a binary presentation:

Serial.println (Address,BIN);

I had one shift left too many - try

void loop() {
int Address =(digitalRead(2)<<7) |( digitalRead(3)<<6);
Serial.println (Address, BIN);
delay(1000);// put your main code here, to run repeatedly:
}

e.g. 384 is 110000000 binary should be 11000000

Hi thank you Horace and Paul

I'm almost there one more issue, see code below:

int dip1 = 2;
int dip2 = 3;
int dip3 = 4;
int dip4 = 5;
int dip5 = 6;
int dip6 = 7;
int Address;

void setup() {

Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);

}

void loop() {

int Address =(digitalRead(2)<<5) |(digitalRead(3)<<4) |(digitalRead(4)<<3) |(digitalRead(5)<<2) |(digitalRead(6)<<1) |(digitalRead(7)<<0);

Serial.println (Address, BIN);

delay(1000);// put your main code here, to run repeatedly:

}

it would store: 100000 when dip1 is off correct

but

if dip1 is on and dip2 is off it would not store bit 1 as a 0 "010000"

hope you get what I mean

Hi thank you Horace and Paul

I'm almost there one more issue, see code below:

int dip1 = 2;
int dip2 = 3;
int dip3 = 4;
int dip4 = 5;
int dip5 = 6;
int dip6 = 7;
int Address;

void setup() {

Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);

}

void loop() {

int Address =(digitalRead(2)<<5) |(digitalRead(3)<<4) |(digitalRead(4)<<3) |(digitalRead(5)<<2) |(digitalRead(6)<<1) |(digitalRead(7)<<0);

Serial.println (Address, BIN);

delay(1000);// put your main code here, to run repeatedly:

}

it would store: 100000 when dip1 is off correct

but

if dip1 is on and dip2 is off it would not store bit 1 as a 0 "010000"

hope you get what I mean

Welcome

Here is another way : t1060892.ino - Wokwi Arduino and ESP32 Simulator

Hello wccoetzee

Try and check this small sketch to read address pins.

// get all address pins in an array
constexpr int AddressPins[] {7,6,5,4,3,2};
int dipSwitch;

void setup() 
{
  Serial.begin(9600); 
  for (auto AddressPin:AddressPins) pinMode (AddressPin,INPUT_PULLUP); 
  for (auto AddressPin:AddressPins) dipSwitch=dipSwitch<<1, dipSwitch=dipSwitch|!digitalRead(AddressPin);
  Serial.println(dipSwitch,BIN); 
}
void loop() 
{
  
}

Enjoy the day and have fun.

Thanks all,

It's working now I've added a place holder "1" to trigger my Clock to start serial.read.

:+1:

Nice
post you final sketch to help other members too.

Final as Addressing the individual RS 485 Slaves
1 x Master communicate to 64 x Slaves
["1000000" as Address-0 to "1111111" as Address-64}
7 bus = 1st bus clock trigger and bus 2 to 7 as 6 binary

if that makes sense.....

int dip1 = 2;
int dip2 = 3;
int dip3 = 4;
int dip4 = 5;
int dip5 = 6;
int dip6 = 7;
int Address;

void setup() {

Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);

}

void loop() {

int Address =(1<<6| digitalRead(2)<<5) |(digitalRead(3)<<4) |(digitalRead(4)<<3) |(digitalRead(5)<<2) |(digitalRead(6)<<1) |(digitalRead(7)<<0);

Serial.println (Address, BIN);

delay(1000);// put your main code here, to run repeatedly:

}

I'm going to take this in a different direction (unless I missed it above). Pins 2-7 on an Arduino Uno/Nano/Mini, etc are all on Port D so you can do a direct read of that port. The pins correspond to PD2 to PD7 so we can read the entire port and shift it right two bits.

// Read port and skip first two pins
int raw = PIND >> 2;
// Invert reading
int address = (~raw) & 0x3F;

Haven't run it, but it should work.