user48930: 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;
}


user48930: As @jsotola noted the solution was simple:
int DipSwitchI2C::read()
{
    unsigned int address = 1;

    Wire.requestFrom(_address, 1);
    if (Wire.available())
    {
        address = Wire.read();
    }
    Wire.endTransmission();

    return address;
 }


48930: <p>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.</p>

<p>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.</p>

<p>Schematic :</p>

<p><a href="https://i.stack.imgur.com/nMjJ9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nMjJ9.png" alt="enter image description here"></a></p>

<p>Code</p>

<pre><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 &lt; 8; i++)
    {
        address += (value &amp; (1&lt;&lt;i)) &gt; 0;
    }

    return address;
}
</code></pre>
 48930: <p>As @jsotola noted the solution was simple:</p>

<pre><code>int DipSwitchI2C::read()
{
    unsigned int address = 1;

    Wire.requestFrom(_address, 1);
    if (Wire.available())
    {
        address = Wire.read();
    }
    Wire.endTransmission();

    return address;
 }
</code></pre>
