Pages: [1]   Go Down
Author Topic: Serial Monitor won't print values inside loop  (Read 364 times)
0 Members and 1 Guest are viewing this topic.
Greece
Offline Offline
Jr. Member
**
Karma: 2
Posts: 51
Ch0c0 All ova'ya Booty
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
RST
232                                   //just the degrees;
233
0
GOING LEFT                     //STUCK FOREVER HERE!;

????
« Last Edit: December 16, 2013, 05:05:53 pm by DeathByCh0c0 » Logged

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

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

Why do you print anonymous values?
Code:
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.
Logged

Greece
Offline Offline
Jr. Member
**
Karma: 2
Posts: 51
Ch0c0 All ova'ya Booty
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 32
Posts: 1388
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12568
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is wrong, but probably not causing your problem:

Code:
         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?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Edison Member
*
Karma: 32
Posts: 1388
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



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

ie
Code:
goal = degree_prev - degree_now

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

Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Try adding debugging prints both sides of that line to see if it's getting stuck there.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Greece
Offline Offline
Jr. Member
**
Karma: 2
Posts: 51
Ch0c0 All ova'ya Booty
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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" ?
Logged

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

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

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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?
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

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

Quote
And have you tried the extra debugging I suggested?
Well, sure. OP added a delay().
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1]   Go Up
Jump to: