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.
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.
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.
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".
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.
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.
// 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.