Program hangs unless Serial.print() is used.

I have an interesting problem that I recall encountering in the past, but I cannot remember my solution.

In this function…

void checkForNegativeSlopeEvent()

  if(sensor1Trend[0] <= -8)
    Serial.println("Possible slope");
    negativeSlopeCounter ++;
  if(negativeSlopeCounter >= 10)
    Serial.println("Negative slope detected");
    receiveState = 2; 

If I remove the first print statement the code freezes when it first tries to run this function. Adding the first print statement back in solves the problem.

Is this symptomatic of a problem in another section of the code?

Has anyone else ever observed this behavior?



First thought: How much memory are you using?

Second: are you sure it freezes there? How do you know for sure?

Its a ATMega 328 and the sketch only uses 5kb.

I describe it as frozen because the first if statement condition is easily met when the first print statement is in the code. When the first print statement is removed the condition is never met.

Either the code stops or the sensor is not read and therefore the condition may never be met.

I will admit that I did not try more debugging methods yet but something felt weird so I wanted to reach out. Sometime in the next few days I will try and see if I can trace the problem to the source.

Thanks for your help robtillaart! I would appreciate any insight.

I would appreciate any insight

You will have to post more (all) of your Sketch to get anything more than guesses.

Instead of using Serial.prints as an indicator for frozen or not, add some form of Flashing LED.

The sketch may only use 5kB of flash (program) memory, but we don't know how much RAM is being used, which is why you should post your code.

Ahh, thank you groove! I do not fully understand the relationship between how the code uses flash and ram memory, it sounds like I may have to hit the books.

Here is a link to the code...

I am working on a way to transmit messages through blinking lights. The idea is that a user would visit a website and queue up a message. They would then visit the link on a smartphone and hold the phone screen up to two photo-diodes.

I will try more debugging this weekend and report back. If anyone wants to look through the code and offer any suggestions I would love to hear it.

Thanks everyone!

Why is sensorNTrend an array when you only ever reference the first element?

The shiftArray function is terrible. Draw out on paper what it is doing. You'll see that you don't need tempArray.

You could even consider a circular array implementation so that data in the array never needs to be shifted. Just move an index to the "end" of the array.

(If you don't understand this, never mind).

      tempTrend += 1;

You got something against simplicity?


Hi PaulS,

The sensorNTrend is an array because I need to observe when the trend changes direction. I could actually just create a variable for the last reading but I thought it might be useful to look at those values over a longer span of time.

Ahhh, I see what you mean about my shiftArray function. I needlessly create another integer array when I should just be looping through my array in reverse and shifting data.

I did consider circular array implementation but then I figured it would have been more of a challenge to implement the findTrend algorithm.

As for the varying conventions I use... I plead the fact that I am still relatively new to code and I need to solidify my style ;)

Thanks for getting me thinking and helping me learn!