Program logic does not seem correct

I have the following bit of code in my program. I'm using a char variable to hold the state of some input buttons. Using the PC serial port to debug.

char newbuttons; // holds button state if it changed
int newbuttons2; // integer version of buttons

// TEST CODE!
newbuttons2=newbuttons;
if (newbuttons2>0)
{
PC.print("New Buttons = ");
PC.println(newbuttons, DEC);
PC.print("New Buttons (int) =");
PC.println(newbuttons2, DEC);
}
// END TEST CODE

This code is part of a larger loop that repeats.

When I run this it continually prints 'New Buttons = 0 & New Buttons (int) = 0'

How is this even possible, when this code is only supposed to execute when newbuttons2 is > zero?

BTW, I also tried newbuttons != 0, newbuttons>0 and newbuttons2!=0 with same result.

Also tried it with HEX instead of DEC, same result

This is just one bit of a much larger program, but I can't get this little section to function correctly.

Any thoughts? Compiles and executes without any other obvious errors except this bit...

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Rather than use char, use bool to store you button states, TRUE or FALSE, 1 or 0.

Tom... :slight_smile:

Sorry for the formatting, but this is such a short piece of code I didn't think it mattered all that much. I'm not sure why anyone would need to see the whole program (5000 lines long), since this little snippet does not in any way depend on what came before or afterwards. " if X is not zero, print X" should never print a zero unless there is a bug in the compiler.

You don't need to post 5000 lines of code. Just create a small but complete program that demonstrates the problem

char newbuttons;  // holds button state if it changed
int newbuttons2;  // integer version of buttons

Why the need for 2 variables ?

  if (newbuttons2>0)

What do you see if you print the variable being tested before you test it ?

Hi,
What char are you giving "newbuttons"
What model Arduino does it "run" on.
Have you just written that bit of code on its own, compiled and run it?

PC.print... Looks like you are using Software Serial.

Can you please tell us your electronics, programming, Arduino, hardware experience?

Thanks.. Tom... :slight_smile:

Hi,
I added some debugging prints.
The if is only true for non zero values, IDE 1.8.5, UNO.

char newbuttons;  // holds button state if it changed
int newbuttons2;  // integer version of buttons

void setup() {
  Serial.begin(9600);
  //newbuttons = "A";
  //newbuttons = 0;
}
void loop() {
  // TEST CODE!
  newbuttons2 = newbuttons;
  Serial.print("Before if...  newbuttons = ");
  Serial.println(newbuttons, DEC);
  Serial.print("Before if.....newbuttons2 (int) =");
  Serial.println(newbuttons2, DEC);
  Serial.println("=======");
  if (newbuttons2 > 0)
  {
    Serial.print("Inside if...  newbuttons = ");
    Serial.println(newbuttons, DEC);
    Serial.print("Inside if.....newbuttons2 (int) =");
    Serial.println(newbuttons2, DEC);
    Serial.println("=======");
  }
  delay(1000);
}
// END TEST CODE

With both newbuttons= commented out , newbuttons is zero and only before if.. prints.
With newbuttons = "A", if is now true and char A = int 21 before and after if... prints.

Tom... :slight_smile:

cdavis2747:
Sorry for the formatting, but this is such a short piece of code I didn't think it mattered all that much. I'm not sure why anyone would need to see the whole program (5000 lines long), since this little snippet does not in any way depend on what came before or afterwards. " if X is not zero, print X" should never print a zero unless there is a bug in the compiler.

As previously stated, we don't need to see all 5000 lines of code. Nobody would look at that much anyway. But, given what you posted, the only answer available is: "You Code Doesn't Work Because It Won't Compile".

You need to post a short, complete program that actually compiles AND demonstrates the problem you're having. In other words, a Minimal, Complete, and Verifiable Example (MCVE).

Because of this:

newbuttons2=newbuttons;

newbuttons2 now equals the ascii value of whatever character is in newbuttons. Maybe you wanted atoi()?

Ok, thanks. I stand corrected.

So, for those who are interested, here's what I discovered via trail and error:

I declared the variable newbuttons as a char within the function being run.

If I test newbuttons in an if statement immediately after it is declared, it tests as being greater than zero in the 'if' statement, but it prints as zero.

if I first initialize it (newbuttons=0;) it then tests as zero and prints as zero.

Not sure if this is a bug or the expected behavior. I'm using a Teensy 3.5 processor, not an Arduino.

Thanks for the help!

Chuck

cdavis2747:
Not sure if this is a bug or the expected behavior.

Since you've provided no compile-able code that demonstrates the issue, nobody on the forum can know either.

I apologize for "wasting everybody's time" . I have been trying for two days to develop a short code example that demonstrated the problem, without success. Every simplified example I have written, and those provided by members of the forum all work without error. If I had some code that demonstrated the problem I would have posted it by now.

As it turns out, it appears that testing an uninitialized local variable in a called function was the cause of the issue. However, it appears that it is difficult to reproduce this problem in a simple program. My suspicion is that it has something to do with what happens to be in the uninitialized memory when the function is called, but it is very hard to reproduce that.

Sorry to say, I didn't find the sarcastic remarks and simplistic solutions to be very helpful. I would have thought that seeing the statement

if (x!=0) println(x, DEC);

printing out ZERO would have been enough to generate some interest or curiosity, regardless of where it appeared in a program.

I would have thought that seeing the statement

if (x!=0) println(x, DEC);

printing out ZERO would have been enough to generate some interest or curiosity, regardless of where it appeared in a program.

I can't seem to find that line of code earlier in this thread

// TEST CODE!
  newbuttons2=newbuttons;

   PC.print("New Buttons = ");
   PC.println(newbuttons2);                
   PC.println(newbuttons2, DEC);

// what is the value of newbuttons2 ? 

// should be  if (newbuttons2) 
// the ">0" is unnecessary 

  if (newbuttons2>0)
    {
      PC.print("New Buttons = ");
      PC.println(newbuttons, DEC);
      PC.print("New Buttons (int) =");
      PC.println(newbuttons2, DEC);
    }
// END TEST CODE/code]

UKHeliBob:
I can't seem to find that line of code earlier in this thread

It wasn't, but the actual code was exactly equivalent. I just simplified it to make the point that it is logically inconsistent. if x is zero it shouldn't print anything and if x is not zero it should print a non-zero value. Seems to be something subtle in the compilation that gives this result when a non-initialized value is used.

I'm sure there's a good technical explanation for this behavior, but to the naked eye it looks like a "can't happen"! Anyway, I'm past it at this point and the system is running OK.

Chuck