I have a large amount of sensor boards that I need to be able to configure setting unique addresses. Each sensor is attached to it's own arduino, I'm using a 8 position DIP switch for setting the address.
I have tried diferent approaches. When I switch On and OFF I get random values. 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.
Schematic :
Code
#include "DipSwitchI2C.h"
DipSwitchI2C::DipSwitchI2C(int address)
{
_address = address;
}
void DipSwitchI2C::begin()
{
Wire.begin();
}
int DipSwitchI2C::read()
{
unsigned int address = 0;
unsigned int value = 0;
Wire.requestFrom(_address, 1);
if (Wire.available())
{
value = Wire.read();
}
Wire.endTransmission();
for (int i = 0; i < 8; i++)
{
address += (value & (1<<i)) > 0;
}
return address;
}

without being able to get the proper byte data– jsotola Aug 20 '18 at 23:48Wire.read()at this linevalue = Wire.read();.... you may be surprised – jsotola Aug 21 '18 at 00:25