IIRC there are undefined side-effects with a statement like this:
ButtonPressed = ButtonPressed++;
The value of (ButtonPressed++) can be just ButtonPressed and then the increment is performed so that if ButtonPressed is initially zero, the statement will assign zero to ButtonPressed and then the increment will be performed.
As Coding Badly and UKHeliBob have pointed out, ButtonPressed++; is a much safer way to do it.
Or you can use ButtonPressed += 1;
or ButtonPressed = ButtonPressed + 1;
In this statement
ButtonPressed = ButtonPressed++;
The value of ButtonPressed is evaluated (ready for the assignment),
then ButtonPressed will be incremented (due to the ++)
Finally, the assignment happens.
The nett result is that ButtonPressed will come out of it all with the same value that it started with.
{
Serial.begin(115200);
int test = 0;
test = test++;
Serial.println(test);
test = test++;
Serial.println(test);
}
void loop()
{
}
I get
1
2
as the output
I take your word for it as I don't have any hardware to test. Looks like I was wrong. So it appears the assignment is happening BEFORE the ++, thus giving the ++ the opportunity to still work after the assignment has happened.
Which means that the OPs original code should work OK.
UKHeliBob:
Try this
...
...
I get
1
2
as the output
Tried it ... worked a few times. Tried other code, then tried this again ... 0, 0 as output.
Erased Due, cycled USB power, restarted IDE, upload ... still 0, 0 as output.
has Undefined Behaviour in C & C++. The reason for this is the concept of Sequence Points, the location of sequence points (in this clause there is only one, at the end of the evaluation of the entire expression), and clause 6.5#2 of the C99 spec, which states *"*Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.".
This expression tries to assign a value to i twice within a single sequence point, so all bets are off as to the behaviour.
Thank you everyone. I did not have this problem with the Chipkit Uno MPIDE. Only encountered it with Arduino. I'll have to change things around a bit. As to the 1e2 . The program had larger numbers i.e. 10e3, 20e6, so I just kept the same format.
Again Thanks.