Set loop condition (while,do...while)

Excuse me!
I want to control LED on/off by HMC5883L.
First LED is OFF delay 10 seconds.
And then LED is ON, and check sensor X angle at same time.
When X is between 80 and 90 ,LED is OFF.
If the conditions are false.
The sensor is constantly detected until the conditions are true.

Please help.
Thank you

My code is as follows:

#include <Wire.h>
#include <HMC5883L.h>
HMC5883L compass;
void setup()
{
Serial.begin(9600);
Wire.begin();
compass = HMC5883L();
compass.SetScale(1.3);
compass.SetMeasurementMode(Measurement_Continuous);
pinMode(8, OUTPUT);
}
void loop()
{
MagnetometerRaw raw = compass.ReadRawAxis();
MagnetometerScaled scaled = compass.ReadScaledAxis();
float yHeading = atan2(scaled.ZAxis, scaled.XAxis);

if(yHeading < 0) yHeading += 2PI;
if(yHeading > 2
PI) yHeading -= 2*PI;
int yDegrees = yHeading * 180/M_PI;
digitalWrite(8, HIGH);

do{
digitalWrite(8, LOW);
MagnetometerRaw raw = compass.ReadRawAxis();
MagnetometerScaled scaled = compass.ReadScaledAxis();
float yHeading = atan2(scaled.ZAxis, scaled.XAxis);

if(yHeading < 0) yHeading += 2PI;
if(yHeading > 2
PI) yHeading -= 2*PI;

int yDegrees = yHeading * 180/M_PI;

Serial.print(yDegrees);
Serial.print(",");

}
while((yDegrees > 200) && (yDegrees < 210));

digitalWrite(8, HIGH);
delay(3000);

}

1) Use the "Insert Code" button for code (makes it look pretty/doesn't take up as much space). 2) Do you have a question, is there a specific problem you have experienced? 3) It is not advised that you use delay(), see the BlinkWithoutDelay example in the Arduino Learning section. (And you say delay of 10 seconds, but delay(3000) is 3000ms, = 3 seconds) 4) Why do you have the exact same code before your do {} while () function and then repeat it? That is like a do do while. I don't see the purpose in repeating this part twice in a row. 5) If I understand you correctly you want the LED to only go off when the sensor's "X angle" is between 80 and 90 degrees, why then is the first line of our do {} while () immediately turn the LED off with no logic?

Mostly just need 2) answered.

I sorry, My code is wrong.
I correct it.
Thank cypherrage.

My code is as follows after correct:

#include <Wire.h> 
#include <HMC5883L.h> 
HMC5883L compass; 
void setup() 
{ 
  Serial.begin(9600); 
  Wire.begin(); 
  compass = HMC5883L(); 
  compass.SetScale(1.3); 
  compass.SetMeasurementMode(Measurement_Continuous); 
  pinMode(8, OUTPUT);
} 
void loop() 
{ 
  MagnetometerRaw raw = compass.ReadRawAxis(); 
  MagnetometerScaled scaled = compass.ReadScaledAxis(); 
  float yHeading = atan2(scaled.ZAxis, scaled.XAxis); 

  if(yHeading < 0) yHeading += 2*PI; 
  if(yHeading > 2*PI) yHeading -= 2*PI; 
  int yDegrees = yHeading * 180/M_PI;
  digitalWrite(8, LOW);
  delay(10000);
 
  
 
  do{ 
  digitalWrite(8, HIGH); 
  MagnetometerRaw raw = compass.ReadRawAxis(); 
  MagnetometerScaled scaled = compass.ReadScaledAxis(); 
  float yHeading = atan2(scaled.ZAxis, scaled.XAxis); 

  if(yHeading < 0) yHeading += 2*PI; 
  if(yHeading > 2*PI) yHeading -= 2*PI; 

  int yDegrees = yHeading * 180/M_PI;
 
  Serial.print(yDegrees); 
  Serial.print(","); 
  
  } 
  while((yDegrees > 200) &&  (yDegrees < 210));
  
  digitalWrite(8, LOW);
  delay(3000);
 
   
}

David, could you provide a problem to which people might assist you with? So far you have simply told us what your code should do, but not stated what you need help with.

Dear cypherrage

my code is not my need. it can't " LED is ON, and check sensor X angle at same time. When X is between 80 and 90 ,LED is OFF. If the conditions are false. The sensor is constantly detected until the conditions are true."

but I don't know where is wrong.

Thank U.

davidwang928: If the conditions are false. The sensor is constantly detected until the conditions are true.

What conditions?

davidwang928: but I don't know where is wrong.

Your delay() statements are the problem. Remove them. See:

http://arduino.cc/en/Tutorial/BlinkWithoutDelay

You still have the exact same thing written twice. What your program does is: 1) Check sensors 2) Turn LED OFF 3) Wait 10 seconds 4) Turn LED ON 5) Check sensors.

I advise also that you take the digitalWrite(8,HIGH) out of the 'do while' loop, there is no need for it to be repeated every single time.

Also, you say you want it to check sensor X angle, but you have it checking the Y angle, and your condition is when Y is not between 200 and 210 (that will break the loop)

Sorry,l don't check my code,l think my question and wait tell you.

dear cypherrage

The program begins by two LED is lit, then an LED light an LED is not lit while checking angles between 80 and 90, has been to wait 10 seconds before the correct angle to the next section of the program and went back to the beginning of two LED lights as a cycle in which angle to use while or while do to write better.

You do not need a ‘do while’ loop because of the inherent properties of the loop() function (it is meant to loop already).

You are also reading the Y axis value and not the X axis value, I changed your code up quite a bit and cleaned up some unnecessary bits.

#include <Wire.h> 
#include <HMC5883L.h> 
HMC5883L compass; 
void setup() 
{ 
  Serial.begin(9600); 
  Wire.begin(); 
  compass = HMC5883L(); 
  compass.SetScale(1.3); 
  compass.SetMeasurementMode(Measurement_Continuous); 
  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);
  delay(10000);
  digitalWrite(8, HIGH);
} 
void loop() 
{    
  MagnetometerRaw raw = compass.ReadRawAxis(); 
  MagnetometerScaled scaled = compass.ReadScaledAxis(); 
  float xHeading = atan2(scaled.YAxis, scaled.ZAxis); 

  if(xHeading < 0) yHeading += 2*PI; 
  if(xHeading > 2*PI) yHeading -= 2*PI; 

  int xDegrees = xHeading * 180/M_PI;
 
  Serial.print(xDegrees); 
  Serial.print(", "); 
  
  if ((xDegrees >= 80) && (xDegrees <= 90)) {
    digitalWrite(8, LOW);
  } else {
    digitalWrite(8, HIGH);
  }   
}

Notice, the LED is set based on that if statement at the end, not some ‘do while’. This probably isn’t even the best code for this, but maybe if you can help us more, we can help you.