0

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.

asked Aug 27, 2020 at 13:23
7
  • Why are you using a String for this? Just save the pin states and compare them and if any have changed send a message. Take out the intermediate step of creating a String from that data and compare the data directly. Commented Aug 27, 2020 at 13:32
  • 1
    Why using a String? Anyway making a copy si as simple as prevState = s; And check for inequality is simply prevState != s. Commented Aug 27, 2020 at 13:32
  • This was intended to be quick and dirty debug code that wouldn't be in the final thing, I was attempting to do what I thought was easy and quick for me at the time, but when you need to start debugging your debugging code, you start to get frustrated. Commented Aug 27, 2020 at 13:42
  • I've added an edit as I realized I missed including what my serial output was. Commented Aug 27, 2020 at 13:45
  • Yeah, operator priority is kinda need to know (or you have to use paretheses). Even += operator would let you avoid stuff like adding result of digital with s and then use it as bool value for ?: :D Commented Aug 27, 2020 at 13:51

2 Answers 2

3

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");
answered Aug 27, 2020 at 14:17
1
  • To make this a more useful answer, you should really mention how the types are ignored / coerced. I've awarded you the answer regardless though. I was aware of operator precedence, but not how it interacted with the types under C. Commented Aug 27, 2020 at 19:40
0

I'm not sure why string appending using + was failing, but changing it from

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

to

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

fixed my issue.

answered Aug 27, 2020 at 13:49
2
  • I won't be accepting this as the answer, even though that it is AN answer, in order to award the answer points to someone that explains what was wrong. Commented Aug 27, 2020 at 13:50
  • first of all you shouldn't use String for this. you can store numbers, compare numbers, print numbers. digitalRead returns a number Commented Aug 27, 2020 at 14:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.