Pages: [1]   Go Down
Author Topic: Program hangs unless Serial.print() is used.  (Read 949 times)
0 Members and 1 Guest are viewing this topic.
San Jose, California
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Teaching you to fish. Metaphorically speaking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

In this function....

Code:
void checkForNegativeSlopeEvent()
{
  Serial.println(sensor1Trend[0]);

  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?

Thanks

-Miles
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 220
Posts: 13836
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First thought: How much memory are you using?

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

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

San Jose, California
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Teaching you to fish. Metaphorically speaking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 209
Posts: 13015
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would appreciate any insight
You will have to post more (all) of your Sketch to get anything more than guesses.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Per Arduino ad Astra

San Jose, California
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Teaching you to fish. Metaphorically speaking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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... http://www.am13.com/arduino/SerialLights.pde

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!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50039
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).

Code:
     tempTrend += 1;
You got something against simplicity?
Code:
     tempTrend++;
Logged

San Jose, California
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Teaching you to fish. Metaphorically speaking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-wink

Thanks for getting me thinking and helping me learn!
Logged

Pages: [1]   Go Up
Jump to: