Using millis() to mark intervals on sensor data

i'm trying to mark out intervals on some sensor data to be plotted on a graph.

the "metronome" code seems to work but when i add the read.data it never gets to the second interval (or third ; interval =2 ).

can someone help point out my mistake ?

int sensorPin = A3;    // select the input pin for the sensor
int sensorValue = 0;  // variable to store the value coming from the sensor

unsigned long currentMillis = 0;
long interval = 1000;         // interval for time (milliseconds)
int timeX = 0;               // time axis unit
  
void setup() {
  Serial.begin(9600);  
}

void loop() {

  sensorValue = analogRead(sensorPin);    
  Serial.println(sensorValue);
  delay(100);
  
  currentMillis = millis();
  if (currentMillis % interval == 0) {  
    Serial.print("interval : ");        // PRINT every 1 interval
    Serial.println(timeX);              
    timeX++;
  }
  
  
}

So you have this big-ass delay(100) in there. You have other code, which will take non-zero time to execute. Maybe it takes a tenth of a millisecond. So each loop is going to take 100.1 milliseconds. Ten of them take 1001 milliseconds.

Now you divide by 1000 and take the remainder - you get 1, not zero as your code expects. So the if() statement is not true on the second interval. It's going to take a long time to get that ==0 to be true.

Read Nick Gammon's excellent page for the right way to do it.

ahh, it was crazy to expect a modulo of exactly 0 on a millis reading, eh ?

that’s why one has to use startMillis.

okay thanks, i just had to follow UKheliBob’s guide at the top of the section page.

that's why one has to use startMillis

I don't think that is what MorganS is saying.

TKall:
I don't think that is what MorganS is saying.

no, not exactly - but the "Nick Gammon's excellent page" was dealing with an overflow issue.

You should use millis() with code like this

if (millis() - previousTime >= interval) {
   previousTime += interval;
   // other code
}

If you need to refer to millis() several times in loop() then save it to currentMillis so that all the tests use the same time.

See how millis() is used to manage timing in Several things at a time.

...R

Robin2:
If you need to refer to millis() several times in loop() then save it to currentMillis so that all the tests use the same time.

yes, i have followed UKheliBob's thread - the code is now like this;

int sensorPin = A3;    // select the input pin for the sensor
int sensorValue = 0;  // variable to store the value coming from the sensor

unsigned long currentMillis;
unsigned long startMillis;   
long interval = 200;         // interval for time (milliseconds)
int timeX = 0;              // time axis unit
  
void setup() {
  Serial.begin(9600);
  
  startMillis = millis ();    // this is the metronome
  Serial.print("Time interval of readings : ");       
  Serial.print(interval);
  Serial.println(" milliseconds");       
}

void loop() {
  
  sensorValue = analogRead(sensorPin);    
  currentMillis = millis();
if (currentMillis - startMillis >= interval)
  {
    Serial.print(timeX);             
    Serial.print("\tanalogRead() = ");
    Serial.println(sensorValue);       
    timeX++;
    startMillis = currentMillis;
  }
    
}

This

startMillis = currentMillis;

can lead to the accumulation of small errors compared with the alternative of

startMillis += interval;

For an extensive discussion see Several Things at a Time

...R