samedi 3 février 2018

c++ if-statement evaluates inside block which is false

I am writing a program for a word clock of mine, and one of my functions check the current hour and pushes a certain byte to my shift-register depending on the current hour. If the current minute is in the interval [28:57], it increases the value of the hour by 1, to account for the difference in hour used ( 8:40 becomes "TWENTY TO NINE", whereas 8:20 is "TWENTY PAST EIGHT"). Additionally, if the hour-value is increased from 12 to 13, it should roll back to 1, as the clock only uses 12-hour mode. The below snippet shows the relevant part of my code, executed at 6:20.

//Hours. If current minute in [28:57], hours number is increased by one
//Additionally, if 13, reset to 1

Serial.println(currHour);

if(28 <= currMin && currMin <= 57) {
    currHour += 1;
    if (currHour == 13) {
       currHour = 1;
    };
    Serial.println("currMin in [28:57], currHour increased by one");
    Serial.print("currHour is now: ");
    Serial.println(currHour);
} else {
    Serial.print("currMin not in [28:57], currHour is still: ");
    Serial.println(currHour);
};

Serial.println(currHour);

The above code prints the value stored in currHour before and after the if-statement evaluates, and the following is output to my serial monitor:

6
currMin not in [28:57], currHour is still: 6
1

If I comment out the if-statement checking whether currHour==13, I get the following output:

6
currMin not in [28:57], currHour is still: 6
6

All of the above code is inside the function void checkHour(int currHour, int currMin);

The arguments for this function, currHour and currMin, are two integers returned by my RTC-module. Normally, I call the function through the variables returned from my RTC: checkHour(rtcHour, rtcMin);

If I manually enter either of the values, the problem goes away and the function evaluates correctly. That is, both of the following statements work correctly:

checkHour(6, rtcMin);

checkHour(rtcHour, 20);

Only when I use both variables returned from my RTC do I encounter problems.

I am completely at a loss here. I suspect it may have something to do with wrong referencing/dereferencing of variables, as that is usually the cause of my problems. But I cannot understand why that would cause the if-statement to evaluate true inside of a false statement.

Aucun commentaire:

Enregistrer un commentaire