user67876: I have the following code (I realise String isn't very efficient, but this sketch doesn't have to be)
debugOutput gets called every loop, but I only want to spam the serial bus (to debug) if the status changes.
However, it runs once, despite changes to the pins being made.
String prevState = "";
void debugOutput() {
  String s = "";
  //s = s + digitalRead(6) ? "1" : "0";
  //s = s + digitalRead(5) ? "1" : "0";
  s = s + digitalRead(4) ? "1" : "0";
  s = s + digitalRead(3) ? "1" : "0";
  s = s + digitalRead(2) ? "1" : "0";
  if(!prevState.equals(s)) {
    Serial.println("State Change: "+s);
    //strcpy(s, prevState);
    prevState = s;
  }
}

My suspicion is it was because of the pointers being the same, but I declare a new string every time this function runs. Just in case, I tried to swap this to strcpy, which isn't valid since I'm using String instead of character arrays.
What can I do, I'm really confused.
This is the Serial output, btw
State Change: 1 So it's clear to me that my string appending isn't even working as expected.

user60431: There is this thing called "operator precedence" that defines the hierarchy of operators.
The ?: will be calculated after the +. That's why
s = s + digitalRead(2) ? "1" : "0";

actually means
s = (s + digitalRead(2)) ? "1" : "0";

But you want
s = s + (digitalRead(2) ? "1" : "0");


67876: <p>I have the following code (I realise String isn't very efficient, but this sketch doesn't have to be)</p>
<p>debugOutput gets called every loop, but I only want to spam the serial bus (to debug) if the status changes.</p>
<p>However, it runs once, despite changes to the pins being made.</p>
<pre><code>String prevState = &quot;&quot;;
void debugOutput() {
  String s = &quot;&quot;;
  //s = s + digitalRead(6) ? &quot;1&quot; : &quot;0&quot;;
  //s = s + digitalRead(5) ? &quot;1&quot; : &quot;0&quot;;
  s = s + digitalRead(4) ? &quot;1&quot; : &quot;0&quot;;
  s = s + digitalRead(3) ? &quot;1&quot; : &quot;0&quot;;
  s = s + digitalRead(2) ? &quot;1&quot; : &quot;0&quot;;
  if(!prevState.equals(s)) {
    Serial.println(&quot;State Change: &quot;+s);
    //strcpy(s, prevState);
    prevState = s;
  }
}
</code></pre>
<p>My suspicion is it was because of the pointers being the same, but I declare a new string every time this function runs. Just in case, I tried to swap this to strcpy, which isn't valid since I'm using String instead of character arrays.</p>
<p>What can I do, I'm really confused.</p>
<p>This is the Serial output, btw</p>
<p><code>State Change: 1</code> So it's clear to me that my string appending isn't even working as expected.</p>
 60431: <p>There is this thing called &quot;operator precedence&quot; that defines the hierarchy of operators.</p>
<p>The <code>?:</code> will be calculated after the <code>+</code>. That's why</p>
<pre><code>s = s + digitalRead(2) ? &quot;1&quot; : &quot;0&quot;;
</code></pre>
<p>actually means</p>
<pre><code>s = (s + digitalRead(2)) ? &quot;1&quot; : &quot;0&quot;;
</code></pre>
<p>But you want</p>
<pre><code>s = s + (digitalRead(2) ? &quot;1&quot; : &quot;0&quot;);
</code></pre>
