Going no where - fast. Program works, then doesn't.

As much of a claimed mess it is, I have a program working on my Arduino R3 board.

I add a bit of code so if it has to wait more than 9 seconds it displays the "correct" time on the display.

1 x 7 segment display.

So if it is 10, it will flash 1 then quickly the 0.

Below 10 is easy......

So the code works.

It runs, and does all it should.

I add this bit of code:

      if (delay_time > 9)

Originally it was simple the "else" statement - but without the condition.

Now, I upload the code to the Arduino, it doesn't say anything is wrong with the code......

I run it, and it reboots as soon as I start the main (well, second main) part of the code.

Its all there...... There are no external dependencies or stuff like that. It simply tests/checks if "delay_time" is greater than 9 and does the first block of code if it is, or the second block if it isn't.

I am getting a lot of this kind of stuff happening recently.

I have code working, I change something in a similar way to how I have done above and the code just won't run.
It just reboots the Arduino every time it gets to that piece of code.

Another example is that I have "serial.print( )" stuff here and there to tell me where it is at.
Not too much. But enough to keep track of things.

I add a bit of code and when it gets to one of these "serial.print( )" bits, a whole lot of gibberish is printed on the screen.
As though I haven't closed one of the " on a line.

I go over it and all the " have their respective " at the other end of the line.

I take out the OTHER few lines of code and it works as expected.

Anyone, please?

I’ve seen you around enough that I know you know that we can’t solve your problem from this one little snippet. There’s obviously nothing wrong in the snippet. That’s why it is confusing you. So the issue must be elsewhere in the code.

You’ve been told before. Post the whole code. How you think anyone can debug your code while only being able to see this one snippet is beyond me.

These sorts of things are typically caused by trying to get too much current out of your Arduino, or using too much memory. However, it is hard to tell without a schematic and with just a snippet of code.

Please properly post ALL of your code and a schematic or wiring diagram or something.

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

Well, the code isn’t small. It is attached in zip format.

My “style” has improved from when I started writing code.
Comments are used a lot and I am really trying to standardise how they are done.

Wiring… Oh boy. Ok. No, that isn’t a sign that it is problematic.
It is just it is … not documented.

I have pictures of the completed board with overlays of what is what on the board.

Would they suffice?

Attached is the WORKING version.
And a few pictures of overlays of what does what.
Text files describing the pictures.

There is a 12V DC 8 A supply.
When running the voltage drops from 11.9 to 11.6v measured on main 12 v supply rail.
(Originally it dropped to 10v but that was with a 5 A supply.)

All the controls (outputs) are done from the I2C expander chip.
The only input is on the Arduino Pin 6.
The I2C from the Arduino to the I2C expander is done to the “extra” I2C pins as the original I2C pins are for the RTC.

The “5v on the board” powers the I2C expander board, and any/all TTL (5v) chips.
Otherwise the chips are all CMOS and run on 12v.
There is a TTL - CMOS level converter chip on the board.
All this is done on a Rasberry Pi. So sorry the “drawings” are not as good as may be expected.

Bus colours.txt (188 Bytes)

V11cV2-170202a.zip (17.9 KB)

Circuit Colour codes.txt (437 Bytes)

How much memory are you using? Random reboots usually indicate that you've run out of memory. You can save a ton of SRAM memory by using the F() macro on any constant strings longer than 3-4 characters.

Well, on compile, it isn't anywhere near the limit.

What is the "F() macro"?
(Don't worry, going to research it... but that doesn't mean much if I don't understand it.)

As best I could I tried to ........

#define Thing value


constant int name number

Ok, did the research of F() macro.

“Shame on me”…

But I plead innocence/ignorance (?)

When I compile the code I get this at the bottom of the screen.

See attached.

(Sorry I can’t get a screen shot with the menu showing. But I have Arduino UNO selected as the board.)

If it says (paraphrasing the attachment a bit)
Used 16k of 32k available…

What do you think I care about 16680 to (say) 17034…

If it says there is 32k available…

WHO (not me, remember I told the IDE it is an Arduino UNO) tells the IDE that the Uno has 32k when really it has 16k?

I think that is the problem…


I am really sorry for “wasting your time” if this is the case. Honestly. I agree I am stupid, but that is abusing the privilege.

But I shall say in my defence: Garbage in, garbage out. I did the best I could to set up the IDE.

Oh god, that is an ancient version of the IDE!

We're up to 1.8.1 now. The new versions tell you how much SRAM is used up by globals and constants (this doesn't count memory allocated dynamically, or used for local variable, but it gives you some guidance). The old version you have only shows how much flash you've used. Flash, you can use up to the very maximum, but SRAM, you need to keep some free for local variables and the stack (and any dynamic memory allocation you may need)

The Uno has 32k of flash, minus 512 bytes for the bootloader.

There were older arduino boards that used the 168, not the 328, and those only have 16k of flash.

Unexpected reboots of the microcontroller are typically caused by:

  • Running out of memory
  • Reading or writing off the end of an array (remember they're 0-indexed, a 5 element array does not have an element 5, it has elements 0, 1, 2, 3, and 4)
  • Power supply problems (drawing too much current, or noise on the power supply from inductive loads)

Well, alas I don't know if there is a "newer" version for the Ras Pi.

I did a recent sudo apt-get update && sudo apt-get upgrade

It is just the PC (windoze ones) are just too POWER hungry. If it was winter, I wouldn't mind, but not the middle of summer. It is too hot to justify the heat.

The UNO I have is a brand spanker (new) and is a R3 board with the extra I2C pins.

So it has......????? 32k memory?

Dunno if you have looked at the code, but I guess I shall have to go in and make a few functions as there are a few places where the same code is duplicated over and over with only small changes, which could be done by functions.....

And I have spent the last half hour changing 90% of the serial.print( to serial.print(F( (excusing any capitols needed).

(Brain failure)



I sat down and re-wrote a few bits of code and knocked it down from 16,755 to 16,536 and it works.

So 16 "k" seems to be a factor in there somewhere.

Ok, 16K is 16368 and my code is 16536. But if the code is 16755 and it reboots....... Something is going on.

Using the F() macro should make enough of a difference that if RAM is the problem, that will probably fix it.

Using the F() macro reduces SRAM use.

Turning repeated code into function calls just reduces flash usage - and you say you're only using around 16k of flash, so you don't need to do this - Unos have 32k of flash, 2k of SRAM.

It sounds like the part of the code you changed happened to have the bug in it. The total code size isn't the problem - you can use every last byte of the max flash size reported by the IDE (for the Uno, it's 32k minus 512 bytes, ie, 32256) - it's SRAM not flash that you need to keep some of free. The Uno has 32k, and if you were using some weirdo board that was labled an uno but didn't have the right chip, the IDE would recognize this and not upload to it.


Well, the “problem” with what you said is that the code worked…

I put in about 7 lines of code (see original post) and it rebooted.

Ok, I kinda did do two things at once so can’t really identify what was the problem.

The serial.print(" being changed to serial.print(F("
and the functions.

I may need to check the size of stuff printed and see if there is a problem there…

Please don’t take this as me being difficult or argumentative with you. I am just still not getting my head around the bigger picture.

(Quickly, on “functions”)

Ok, so I have something like:

hour = get_hour_time();

//Obviously hour can be anything from 0 to 23.

void BCD_PRINT(int value)
  //  what ever


And it displays the value (flashing the display if it is greater than 9).

I have a “bug” that there is only 1 digit to display the value.
So if value > 9 I split it up and display the “tens” then the “units”.
If it is < 10, it displays the value with a leading ZERO.

So, 11 is shown as 1 1
10 is displayed as 1 0
9 is displayed as 0 9

Sometimes ok, sometimes not.

Is it “legal” to do it this way:

void BCD_PRINT(int value, int format)


And use “format” to control if the leading zero is displayed?
(Yeah, it only needs to be Boolean, but I am simplifying it just now.

Sure that is legal, although you might want parameters that are byte, byte or even byte, boolean instead of int, int.


You keep talking about 32k when they talk about memory. 32k is the program space. That's not what they're talking about. They're talking about RAM. You only get 2k of that.

Yeah, ok, sorry.

But if the IDE isn't showing me how much "RAM" I am using in the code and it is only 16k of 32 available, can you see where I am getting stuck?

AFAIK there is no newer version for the R Pi. So I am kinda stuck there too.

I compiled your code with IDE 1.6.6. Next time please provide links to the libraries that you use, there are a few DS3231 libraries; I found the RinkyDink one that matches your constructor.

DS3231 rtc(SDA, SCL);

I found the Centipede on the macetech site (I think it's http://macetech.com/Centipede.zip but I might have followed another link) and the ClickButton on github.

With those libraries, this is the memory usage result.

Sketch uses 17,214 bytes (53%) of program storage space. Maximum is 32,256 bytes.
Global variables use [color=red]2,038 bytes (99%)[/color] of dynamic memory, leaving 10 bytes for local variables. Maximum is 2,048 bytes.
[color=red]Low memory available, stability problems may occur.[/color]

No way that that will ever work reliably on an Uno. You will need to bring your RAM usage down to around 1400 bytes to get rid of the warning. And that is still no guarantee.

Get another Arduino that has more memory. With compatible signal levels and pinout, I think a Mega sounds right.

Hey, thanks very much for the info.

(Re: Libraries. Ok, sorry. Lesson learnt.)

I'll check the new libraries to the ones I have and see if it helps.

On "variables"..... I have a few lines in the "debug section" where they are only "drawing lines" of ----- or ====.
As that is used a bit, can I do something like (and PLEASE excuse the BASIC analogy..... That is what I had when I was doing programming.

and when I am "printing" messages, I do something like:

(done in the setup)
J$ = "------"

print J$
print message
print J$

That would save a LOT of "wasted" space - yes?

Am I at least starting to get the jist of how to do things better?

If you really want to save space, put that string in progmem.


Wow! As you were typing that, I was about to ask the same question about progmem......

How weird is that??!!


(warning, when I preview it, the formatting is crap. the "code" blocks are WAY too big. I tried to fix it, but can't.)

Here's where I am stuck now.......

Reading the stuff on the link about prgmem.....


This is the line in my code:
(in the startup part)

const char THICKHEADER[] PROGMEM = ("===========================================");

and it compiles ok.

That is going to replace this line:


So, it SHOULD be easy.... But for what ever reason: It isn't.

1 - why do I need the at the end of the name?
2 - going on the name, I put this in the code:


and I get this error:

ZZ_ALL_Debug_stuff.ino: In function ‘void DEBUG_WATER(int)’:
ZZ_ALL_Debug_stuff.ino:8:32: error: expected primary-expression before ‘]’ token


That's all I see.
I declared a variable and called it THICKHEADER and when I try to print it, it complains something about "expected primary-expression before ]....

I didn't want the things in the name. "IT" insisted!