0

When I assign a global var to a certain value inside an if-block it has been changed to another value in the next statement. Somehow the address of the var is overwritten by some other process. It also does not stop at 1 value, this happens to the next vars that are assigned another value too. The vars are not defined in a row but should be close. I already installed a newer version of Arduino IDE, no change. Example:

menuActive=1;
Serial.print(menuActive);

gives results in the range 40 to 57 as far as I have seen till now. Further actions assign always the SAME value to next vars I assign after this. Anyone some idea what could cause this? Am I low on memory for local vars? This is compile message:

Sketch uses 21790 bytes (70%) of program storage space. Maximum is 30720 bytes.
Global variables use 1454 bytes (70%) of dynamic memory, leaving 594 bytes for local variables. Maximum is 2048 bytes.

edit:

As I already said, it is 21k of code. But I show you the part of the code that mishaves:

while ((digitalRead(rotSwPin) == 0) && lpress <= 200) {
    delay(10);
    lpress++;
    if (lpress > 200) {
        menuActive++;
        menuLvl=0;
        menuPos=0;
        subPos=0;
    }
}

Normally I use menuActive=1. Within the braces, it is still 1, out of that it changes. Maybe change from byte to bool.

11
  • 1
    Sounds like a buffer overflow to me.
    – Majenko
    Commented Oct 17, 2018 at 14:44
  • 5
    or show your code and someone spots the problem
    – Juraj
    Commented Oct 17, 2018 at 15:10
  • 2
    If your program corrupts memory (which seems to be the case here), the part of the code that misbehaves can be quite far from the part that does the memory corruption. There is nothing wrong with the code you are showing here. Your problem lies in some part of the code you are not showing. Nobody will be able to help you without seeing the bug. Commented Oct 18, 2018 at 10:14
  • 1
    I found the problem. I could see that the false value is the character code of the minute I am displaying. I display hours and minutes and there was a declaration that was 3 characters short for the string I got as a result for hours added to minutes. Yes it was memory corruption. So be careful to dimension enough places for your strings including 1 extra for 0.
    – hoekbrwr
    Commented Oct 18, 2018 at 10:54
  • 2
    @hoekbrwr You might want to post that as an answer, rather than a comment. Commented Oct 18, 2018 at 19:52

1 Answer 1

1

I found the problem. I could see that the false value is the character code of the minute I am displaying. I display hours and minutes and there was a declaration that was 3 characters short for the string I got as a result for hours added to minutes. Yes, it was memory corruption. So be very careful to dimension enough places for your C-strings including 1 extra for 0. If you are not so short on RAM then a few more characters will do no harm, the end of the string will be the character before the 0 in memory. When strange things happen start looking at this matter!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.