user85292: I'm trying to make a continuity tester at work where I connect the input and output of a harness into a test fixture powered by an Arduino. This tester tests whether the position of the connects are pinned correctly.
Here is the diagram of my initial idea:

From one end of the connector (the output in the diagram), I want to shift in a binary that is in the power of 2. For instance, shifting in 0001, check for continuity by reading the 75HC165 (parallel in, serial out), then clear the register and then shift in 0010, and then 0100 and so on. For a correctly pinned harness, the write and read binary subtraction will be zero.
Here is my loop that attempts to perform the above:
void loop() {
  for (int i = 1; i < 4; i++) {
    reset_SIPO_register();
    shift_SIPO(pow(2,i));
    delay(2000);
    shift_SIPO(0x00));
    check_continuity(); // not implemented yet
  }
}

void reset_SIPO_register() {
  digitalWrite(reset_pin, LOW);
  digitalWrite(reset_pin, HIGH);
}

void shift_SIPO(int sequence) {
  digitalWrite(latch_pin, LOW);
  shiftOut(data_pin, clock_pin, MSBFIRST, sequence);
  digitalWrite(latch_pin, HIGH);
}

I am testing this concept on a breadboard with LEDs and the above kind of works. However, the LED is lit up one at a time and they don't turn off, not following the binary.
Is there something I'm missing here? thanks!

user7508: Do not use pow(). This is a function that works with floating point
numbers. It is roughly equivalent (for y > 0) to
double pow(double x, double y)
{
    return exp(y * log(x));
}

As all transcendental functions, its implementation is prone to rounding
errors. It is not even guaranteed to give you the “correctly” rounded
version of the exact (i.e. infinite precision) result. In fact, very few
floating point functions give this guarantee.
As an example, if you compute pow(2, i) for i = 3, you get
7.9999980926513671875. This is 4 ULPs smaller than the exact value.
Passing this number to shift_SIPO() implicitly converts it to an
int, and this conversion always rounds to zero. Thus you get 7, i.e.
0b0111.
What you want is an integer, exact implementation of pow(2, i). It
turns out this is quite simple: Take the integer 1 in binary, then shift
it i times to the left. That is
shift_SIPO(1 << i);


85292: <p>I'm trying to make a continuity tester at work where I connect the input and output of a harness into a test fixture powered by an Arduino. This tester tests whether the position of the connects are pinned correctly.</p>
<p>Here is the diagram of my initial idea:</p>
<p><a href="https://i.stack.imgur.com/NKgVm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NKgVm.png" alt="enter image description here" /></a></p>
<p>From one end of the connector (the output in the diagram), I want to shift in a binary that is in the power of 2. For instance, shifting in <code>0001</code>, check for continuity by reading the 75HC165 (parallel in, serial out), then clear the register and then shift in <code>0010</code>, and then <code>0100</code> and so on. For a correctly pinned harness, the write and read binary subtraction will be zero.</p>
<p>Here is my loop that attempts to perform the above:</p>
<pre><code>void loop() {
  for (int i = 1; i &lt; 4; i++) {
    reset_SIPO_register();
    shift_SIPO(pow(2,i));
    delay(2000);
    shift_SIPO(0x00));
    check_continuity(); // not implemented yet
  }
}

void reset_SIPO_register() {
  digitalWrite(reset_pin, LOW);
  digitalWrite(reset_pin, HIGH);
}

void shift_SIPO(int sequence) {
  digitalWrite(latch_pin, LOW);
  shiftOut(data_pin, clock_pin, MSBFIRST, sequence);
  digitalWrite(latch_pin, HIGH);
}
</code></pre>
<p>I am testing this concept on a breadboard with LEDs and the above kind of works. However, the LED is lit up one at a time and they don't turn off, not following the binary.</p>
<p>Is there something I'm missing here? thanks!</p>
 7508: <p>Do not use <code>pow()</code>. This is a function that works with floating point
numbers. It is roughly equivalent (for y &gt; 0) to</p>
<pre class="lang-cpp prettyprint-override"><code>double pow(double x, double y)
{
    return exp(y * log(x));
}
</code></pre>
<p>As all transcendental functions, its implementation is prone to rounding
errors. It is not even guaranteed to give you the “correctly” rounded
version of the exact (i.e. infinite precision) result. In fact, very few
floating point functions give this guarantee.</p>
<p>As an example, if you compute <code>pow(2, i)</code> for i = 3, you get
7.9999980926513671875. This is 4 <a href="https://en.wikipedia.org/wiki/Unit_in_the_last_place" rel="nofollow noreferrer">ULPs</a> smaller than the exact value.
Passing this number to <code>shift_SIPO()</code> implicitly converts it to an
<code>int</code>, and this conversion always rounds to zero. Thus you get 7, i.e.
<code>0b0111</code>.</p>
<p>What you want is an integer, exact implementation of <code>pow(2, i)</code>. It
turns out this is quite simple: Take the integer 1 in binary, then shift
it <code>i</code> times to the left. That is</p>
<pre class="lang-cpp prettyprint-override"><code>shift_SIPO(1 &lt;&lt; i);
</code></pre>
