Bizarre program behaviour

I am in the middle of writing an intervalometer program for a friends camera.

I have the following code (not complete) : Intervalometer - Pastebin.com

With the Serial.print line on Line 95 left in, the output from the Serial.print line on Line 120 prints correctly.

e.g.

Menu_State: 5

if I comment out Line 95, for some bizarre reason the output from Line 120 is completely screwed up.

e.g.

Menu_State: 5¸¸¸¸¸ô
Menu_State: ¸¸¸¸¸¸ô
Menu_State: ¸¸¸¸¸¸ô
Menu_State: ¸¸¸¸¸¸ô

it is driving me nuts. Can anyone shed any light on this please?

Thanks,

Mike

Line 95? Post your code here.

It's in the link in the post Nick.

I find pastebin more convenient for long programs than an attached .ino file.

And I have no idea what the problem might be. I note that line 120 has MENU_STATE, DEC which is not the same as line 95

@Mike Mc, you have a long program - when did the problem emerge? Can you reproduce it in a short program? Or has it arisen because the program is long?

...R

I added DEC just to see if it makes any difference. It doesn't.

It is a long program but it is not even 50% of flash so it's not a memory issue as far as I can see.

Not tried reproducing it in a short program but i've never come across this problem before when printing to the serial monitor. Just with this program.

I also find that items printed to the OLED display are also corrupted unless I print the same thing first. it's almost like you have to do something prior to doing it within another function for it to work properly. It's totally bizarre.

O … k. That sort of thing can be caused by code scribbling over memory that it doesn't own. When you remove line 95, it rearranges the layout of what's stored in memory, and suddenly you see the previously hidden bug.

Let's see if we can find it for you. Hmm - here's one:

  char myString[9];
    if (MENU_STATE == DELAY)  strcpy(myString, "DELAY") ;
    if (MENU_STATE == SHOTS) strcpy(myString, "EXPOSURES") ;
    if (MENU_STATE == LENGTH)  strcpy(myString, "LENGTH") ;
    if (MENU_STATE == INTERVAL)  strcpy(myString, "INTERVAL") ;

"EXPOSURES" is 10 characters long: 9 characters and a trailing '\0' terminator. You are copying it into an array that's only 9 characters long.

Couldn't find any others with a cursory look, but that's one. There may be others. Not sure why you have myString declared all over the shop - why not have a static scrap area myString[100] defined at the top of the code, and everyone use that? Why use myString at all? Main reason for moving strings around like that is if you are storing your text in progmem, which it doesn't seem you are doing.

I suggest:
declare char myString[100]; at the top, get rid of all those little declarations that allocate juuust enough space on the stack, see if that fixes it.

PS: repeated blocks like this:

    // print exposures
    strcpy(myString, "Exp :");
    u8g.setPrintPos(0, 16);
    u8g.print(myString);
    itoa(myData.shots, myString, 10);
    u8g.setPrintPos(64, 16);
    u8g.print(myString);

belong in a function.

Like I said, the code is only half done so still lots of sorting out to do and tidying up, etc. This is just prototyping.

It looked like it may have been the char array not being long enough that was causing it. I've increased it to 16 and the issue seems to have gone away. Thanks for that.

PaulMurrayCbr:
When you remove line 95, it rearranges the layout of what's stored in memory, and suddenly you see the previously hidden bug.

That is an interesting and useful way of thinking.

...R

If it's short, post it in the message. If it's longer attach it to a post. Don't make people go to external sites.

How to use this forum


Line 95:

  Serial.print("A: MENU_STATE = "); Serial.println(MENU_STATE);

You seem to have a lot of serial prints. I suggest you use the F() macro - you may be running out of RAM. eg.

  Serial.print(F("A: MENU_STATE = ")); Serial.println(MENU_STATE);

He shoots, he scores!

Karma me :slight_smile: