Changing to NOT (!) instead of " = false " has crippled code [SOLVED]

Under the advice of forum members I decided to clean up my code and simplify things in the interests of making it easier to read and use less memory. I reduced my sketch size by 10%.

Unfortunately in changing bools from " xyz = false " to " !xyz " I have introduced program execution problems. I renamed the bool from:

// bool xyz_OFF_flag == false;   changed to:
bool xyz_OFF_flag = true;

if ( input_xyz_state == high  &&  !xyz_OFF_flag )
 {
    strcpy ( xyz_state, "OFF" );
    addHistoricalData( xyz_state, timeStamp_hr, timeStamp_min, timeStamp_sec);
    xyz_OFF_flag;    // set flag to "true" to prevent looking at if statement again
    !xyz_ON_flag ; 
}

This has caused my structure to stop recording change of states in its array.
I have a test UNO and monitor the input and the structure never responds to the change of state on the digital input.
And all I did was redefine the variables from " == false" to " ! ". The structure was not modified in any way. So evidently I am failing to understand the correct use of the negate (!) symbol and how to define the bools properly. Am I overlooking something simple?

1 Like

The snippet is working correctly, because the if clause is always false, regardless of the digital input state.

Now I am confused. I declare the bool to be true by default in the declaration.
If it is declared "true" when it is encountered in an "if" statement with the "!" is it comparing the declared value with the "!" value ?

Or should I not be assigning a true or false value to the bool in its declaration?

Evidently defining the bool as true or false in its initial declaration is incorrect as the struct is now responding to changes of state. Live and learn. :joy:

The β€œ==β€œ does a Boolean comparison of state. The β€œ!” Performs the not function so true flips to false. So !True will always be false.

So, in the if() statement, you have the result of the β€œAnd” given a comparison (β€œ input_xyz_state == high”) and β€œnot-xyz_OFF_flagβ€œ (always xyz_OFF_flag negated).

1 Like

Oh brother! I survived another coding snafu. ...sheesh....

During the declaration, assigning a value β€œ=β€œ is expected so the variable starts in a known state. Doing a comparison β€œ==β€œ on a variable in its declaration will do nothing and leave the variable undefined.

My bad. The declaration was a single "=" so that created some confusion unfortunately when I typed the code in. I still was looking at things "bass akwards" I suppose.

I tried to compile an example with the line that you commented out

bool xyz_OFF_flag == false;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

The result

sketch_apr08b:1:19: error: expected initializer before '==' token
 bool xyz_OFF_flag == false;
                   ^~
exit status 1
expected initializer before '==' token

If you're low on memory, I would definitely not store xyz_state as text but as e.g. a bool (or enum if there are more than two possible values). When you need to display the historical data you can convert the contents of the variable to human readable text.

It was a typo. The double "=" would never pass a compile. If there is a way for me to screw up when writing C I will find it every freakin' time. ...sheesh...

I just made a reply to you by selecting "Reply" in your message block but it still did not attach the response to you specifically. It looks like a generic response to the thread. Why does it do that?

As far as I know it's the behaviour of the forum. I however can see the difference in the notifications; your reply has the little arrow icon indicating it's to me, a reply to other another post in the topic would have the bell icon

If I reply to the last post, I often include e.g. @edthewino if it's to a specific post or quote a part of the last post.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.