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