rest void loop

hi guys

I am writing a code for HMC588L sensor and it is work successfully, but the problem is I want to print in serial monitor the value once when for example the value still above 200 for more than 5 seconds. after that I want to rest the process without rest the arduino which means I want to from the code when ever the value still above 200 for seonds print once. so far I can print the value once and if I want to reprint I need to rest the arduino manually. here u can see my code so far and if anyone can help me I will be thankful.

unsigned long time;
// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>

// Store our compass as an object.
HMC5883L compass;

// Record any errors that may occur in the compass.
int error = 0;
int q =0;
int x = 0;

// Out setup routine, here we will configure the microcontroller and compass.
void setup()

// Initialize the serial port.

//Serial.println(“Starting the I2C interface.”);
Wire.begin(); // Start the I2C interface.

//Serial.println(“Constructing new HMC5883L”);
compass = HMC5883L(); // Construct a new HMC5883 compass.
//The implementation of the class is provided in the library

// Now we have an istance of the class!
//Let’s initializate it…

//Serial.println(“Setting scale to +/- 1.3 Ga”);
error = compass.SetScale(1.3); // Set the scale of the compass to 1.3Ga
if(error != 0){ // If there is an error, print it out.
error =0;

//Serial.println(“Setting measurement mode to continous.”);
error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
if(error != 0) {// If there is an error, print it out.
Serial.println(compass.GetErrorText(error)); //Todo: Error handling for this method in .h and .cpp
// Our main program loop.
void loop()
// Retrieve the raw values from the magnetometer (not scaled).
MagnetometerRaw raw = compass.ReadRawAxis();
// Retrieve the scaled values from the magnetometer (scaled to the configured scale).
MagnetometerScaled scaled = compass.ReadScaledAxis();

// Values are accessed like so:
int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

// Calculate heading when the magnetometer is level, then correct for signs of axis.
// Atan2() automatically check the correct formula taking care of the quadrant you are in
float heading = atan2(scaled.YAxis, scaled.XAxis);

// Once you have your heading, you must then add your ‘Declination Angle’,
// which is the ‘Error’ of the magnetic field in your location. Mine is 0.0404
// Find yours here:

// If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
//float declinationAngle = 0.0404;
//heading += declinationAngle;

// Correct for when signs are reversed.
if(heading < 0)
heading += 2*PI;

// Check for wrap due to addition of declination.
if(heading > 2PI)
heading -= 2

// Convert radians to degrees for readability.
float headingDegrees = heading * 180/M_PI;

// Output the data via the serial port.
//Output(raw, scaled, heading, headingDegrees);

// By default the HMC5883L reads the data 15 time per second (15Hz)
// However since we have a long serial out (104ms at 9600) we will let
// it run at its natural speed.

int x= abs (scaled.ZAxis);

while ( x > 220 )
time = millis();

delay (1000);
if ( q < 5);
else if ( q == 5)
// while ( x > 220)
// {
// q++;
// delay (500);
// if ( q < 10 );
// else {
// Serial.println(x);
// delay (1000000);
// break;
// }
// break;
// }


How would YOU do what you are asking the Arduino to do? If you can define those steps, writing the code it trivial.

You will be given a pencil and some paper (variables in the Arduino) and a watch with just a (milli)second hand (millis()).'

What would you do?

I did not get ur point can u fix the code for me??

Then you read this first aka use code tags.

But coding starts with precisely explaining what you want. One step every time. If you can do that coding is way easier.

I did not get ur point can u fix the code for me??

I suspect that your career as a programmer is about to come to an end.

If you can not explain how you would do something, there is no way you can teach someone else, or something else, how to do that same thing. THAT is what coding is all about - teaching a computer how to do something.