time - millis() problem

I was wondering why for the code below, orientTime doesn't work? When I run this code, my sensor will only execute the part where it printsXYZ data every 500ms, but it won't record the XValue every 3500 and 5000ms.

How should I format it correctly?

  case ORIENT: {
   xl.readXYZTData(XValue, YValue, ZValue, Temperature);
   orientTime = millis();
       if(millis() - orientTime >= 3500) {
           xvalt1 = (XValue);
           Serial.print("xvalt1=");
           Serial.println(xvalt1);
       }
   if(millis() - orientTime >= 5000) {
           xvalt2 = (XValue);
           Serial.print("xvalt2=");
           Serial.println(xvalt2);
       }
    if(millis() - lastPrintSample >= 500) {
       lastPrintSample = millis();
       Serial.print("X=");
           Serial.print(XValue);
           Serial.print("\t Y=");
           Serial.print(YValue);
           Serial.print("\t Z=");
           Serial.println(ZValue);
       }
        }

Post the complete sketch. Use code tags.

Nevermind, found that millis() - orientTime needed to be defined for each if-statement.

found that millis() - orientTime needed to be defined for each if-statement.

Why not do it once then use the result when you need it ?

           xvalt1 = (XValue);
...
           xvalt2 = (XValue);

Variables with the same name and a numeric suffix usually indicates that an array would be useful

I'm not sure how to properly format the code to do this.

I thought my code in the top post would have one timer running that can be accessed by each if-statement.

orientTime = millis();
if (millis() - orientTime >= 3500) {
  xvalt1 = (XValue);
  Serial.print("xvalt1=");
  Serial.println(xvalt1);
}

The condition in that 'if' statement will NEVER be true. Why won't you post the complete sketch?

You say that this

if(millis() - lastPrintSample >= 500) {
       lastPrintSample = millis();
       Serial.print("X=");
           Serial.print(XValue);
           Serial.print("\t Y=");
           Serial.print(YValue);
           Serial.print("\t Z=");
           Serial.println(ZValue);
       }

works,

but that:

   orientTime = millis();
       if(millis() - orientTime >= 3500) {
           xvalt1 = (XValue);
           Serial.print("xvalt1=");
           Serial.println(xvalt1);
       }

Does not.

Look carefully at both codes... what is different between them?

Jacques

Vitesze:

   orientTime = millis();

if(millis() - orientTime >= 3500) {
          xvalt1 = (XValue);
          Serial.print("xvalt1=");
          Serial.println(xvalt1);
      }
  if(millis() - orientTime >= 5000) {
          xvalt2 = (XValue);
          Serial.print("xvalt2=");
          Serial.println(xvalt2);
      }

You are setting a variable to millis() and a few microseconds later checking to see if millis()-variable >= some large number. The millis() counter will not progress more than 1 in that short amount of time. You have to set the variable OUTSIDE the loop and not set it again until the time interval has expired.

Change

  orientTime = millis();
       if(millis() - orientTime >= 3500) {
           xvalt1 = (XValue);
           Serial.print("xvalt1=");
           Serial.println(xvalt1);
       }

to

       if(millis() - orientTime >= 3500) {
           xvalt1 = (XValue);
           Serial.print("xvalt1=");
           Serial.println(xvalt1);
          orientTime = millis();
       }