Go Down

Topic: Serial Monitor won't print values inside loop (Read 468 times) previous topic - next topic

DeathByCh0c0

Dec 16, 2013, 11:03 pm Last Edit: Dec 16, 2013, 11:05 pm by DeathByCh0c0 Reason: 1
Hello,

The Serial Monitor stops printing values inside a "do....while" loop.

I'm very new into programming , take a look at my code:

Code: [Select]

#include <Wire.h>
#include <HMC6352.h>

 
void setup()
{
 Wire.begin();
 Serial.begin(9600);
 
 delay(100);
 Serial.println("RST");
 delay(100);
}
int degree_prev;
int degree_now;
int goal;

void loop()
{
 HMC6352.Wake();
 degree_prev=HMC6352.GetHeading();
 HMC6352.Sleep();
 delay(100);
 Serial.println(degree_prev);
 do {    
        HMC6352.Wake();
        degree_now=HMC6352.GetHeading();
        HMC6352.Sleep();
        delay(100);
        if (degree_prev-degree_now>=0) goal = degree_prev - degree_now;
        else if (degree_prev-degree_now < 0)goal = degree_prev + 360 - abs(degree_now);        
        Serial.println(degree_now);
        Serial.println(goal);
        Serial.println("GOING LEFT");
 }while (goal<90);
 Serial.println("HOORAY 90 DEGREE ALL DONE!");
 delay(1000);
 
}


The ONLY thing printed in the Serial Monitor is this:
Code: [Select]

RST
232                                   //just the degrees;
233
0
GOING LEFT                     //STUCK FOREVER HERE!;


????

PaulS

Why do you keep putting the thing (whatever it is) to sleep?

Why do you print anonymous values?
Code: [Select]
Serial.print("degree_prev: ");
Serial.println(degree_prev);

removes all ambiguity about what you are printing.

I hate do/while loops, because they iterate at least once, even if the end condition is true. A while loop is almost always better, since the condition is checked first AND is stated at the top of the block.

DeathByCh0c0

Quote
(whatever it is)
HMC6352 is a compass module;

Tried it with a while loop , still stuck.
The sleep mode was unnecessary but removing it still doesn't fix the issue.

BTW, im not using any kind of interrupt;

KeithRB

I don't think this is your problem, but you don't want an else if with only two cases, an else will do fine. I think two that braces are better style when using else-ifs.

PeterH

This is wrong, but probably not causing your problem:

Code: [Select]

         if (degree_prev-degree_now>=0) goal = degree_prev - degree_now;
         else if (degree_prev-degree_now < 0)goal = degree_prev + 360 - abs(degree_now);   


There are two problems: the second 'if' is redundant since it must be true if the first condition is false; if there had been any situation where both conditions were false then goal would not be set.

There's no obvious way for the code to get stuck so I suggest you need to look for non-obvious ways. To do that, add print statements before and after each function/method call in the loop and see whether execution is blocking somewhere. Add a delay(100) or similar after each print statement to allow time for the serial output to be sent, in case something is blocking with interrupts disabled, or crashing in a way that kills the serial output.

Aside from the issue of why the code is not executing, the logic associated with goal looks pretty dodgy. What's it intended to do?
I only provide help via the forum - please do not contact me for private consultancy.

KeithRB



You can simplify things by calculating prev - now and adding 360 if it less than zero.

ie
Code: [Select]

goal = degree_prev - degree_now

goal = (goal >= 0) ? (goal) : (goal + 360);


majenko

Could it be that the second call to
Code: [Select]
         degree_now=HMC6352.GetHeading();
is not returning?

Try adding debugging prints both sides of that line to see if it's getting stuck there.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

DeathByCh0c0

Tried with a delay before the end loop but it still gets stuck , im a noob,does the baud rate affect the output of the serial monitor ? If yes in what way exactly ?

Slightly off topic:
Quote
This is wrong, but probably not causing your problem:

Code: [Select]
if (degree_prev-degree_now>=0) goal = degree_prev - degree_now;
         else if (degree_prev-degree_now < 0)goal = degree_prev + 360 - abs(degree_now);


You mean the general logic behind it or just the redundant "if" ?

PaulS

Quote
Tried with a delay before the end loop but it still gets stuck

Only, now it takes longer to get stuck. You can make it take longer by increasing the delay().

Not that doing that makes any sense.

Quote
does the baud rate affect the output of the serial monitor ?

Not if both ends are using the same speed. If they are, the baud rate only affects how long it takes to transfer a given amount of data.

majenko


Tried with a delay before the end loop but it still gets stuck , im a noob,does the baud rate affect the output of the serial monitor ? If yes in what way exactly ?

Slightly off topic:
Quote
This is wrong, but probably not causing your problem:

Code: [Select]
if (degree_prev-degree_now>=0) goal = degree_prev - degree_now;
         else if (degree_prev-degree_now < 0)goal = degree_prev + 360 - abs(degree_now);


You mean the general logic behind it or just the redundant "if" ?

The redundant if.  Rewrite, and re-lay it out more neatly, as:
Code: [Select]

if (degree_prev-degree_now>=0) {
  goal = degree_prev - degree_now;
} else {
  goal = degree_prev + 360 - abs(degree_now);
}

It can't be anything other than greater/equal to 0, or less than 0.  If it's not greater than or equal to, then what can it be but less than?

And have you tried the extra debugging I suggested?
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

PaulS

Quote
And have you tried the extra debugging I suggested?

Well, sure. OP added a delay().

majenko


Quote
And have you tried the extra debugging I suggested?

Well, sure. OP added a delay().

delay() is the same as wrapping the function I pointed out in serial prints?  He must live in some alternative universe...
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Go Up