Trying to sample a value every 10 mS, but I get multiple execution

Using the Blink without delay method, I try to print out something every 10MS.
I get 3 printouts, and that will mess up my plotter later one. Wonder why?
Thanks

/*
  Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;  // the number of the LED pin

// Variables will change:
int ledState = LOW;  // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;  // will store last time LED was updated

// constants won't change:
const long interval = 10;  // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
    Serial.begin(115200);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

  Serial.println("BEFORE:");
}
}

I get
13:07:14.915 -> BEFORE:

13:07:14.915 -> BEFORE:

13:07:14.915 -> BEFORE:

13:07:14.948 -> BEFORE:

13:07:14.948 -> BEFORE:

13:07:14.948 -> BEFORE:

13:07:14.980 -> BEFORE:

13:07:14.980 -> BEFORE:

13:07:14.980 -> BEFORE:

const unsigned long interval = 10;  // interval at which to blink (milliseconds)
unsigned long markTime;

void setup() 
{
  Serial.begin(115200);
markTime = millis();
}

void loop() 
{
  if (millis() - markTime >= interval) 
  {
    markTime = millis();
    Serial.println("BEFORE:");
  }
}
1 Like

I'd've printed the value of millis(), not some silly constant string.

1 Like

Yes. If you print out the millis that the Arduino thinks it is, you may see that when the logger prepends the times, it tags three separate arduino millis() at (for example) '3:07:14.915'

There's about 30ms between those timestamps.

Yes.

Did:

10
16:43:36.065 -> 20
16:43:36.065 -> 30
40
16:43:36.098 -> 50
16:43:36.098 -> 60
70
16:43:36.136 -> 80
16:43:36.136 -> 90
16:43:36.136 -> 100
110
16:43:36.173 -> 120
16:43:36.173 -> 130
16:43:36.173 -> 140

I don't use the timestamp thing. I think it proved worthless to me once. I can't explain why it does not time stamp every line.

If I need times stamps now, Iā€¦ print the value of millis() or maybe the difference in millis() between the last and this line of print.

a7

1 Like

It's giving you the timestamp of when it read the data, not necessarily when the data arrived and certainly not when it was sent.

Or possibly even worse it's giving you the timestamp of when it output the data to the Serial Monitor. Who knows if there's an additional buffering step there.

1 Like

The timestamp granularity is a Windows timestamping aberration; in this case, it seems to tick every 32/33 ms. There was a time when it ticked every 55 ms, don't know how/why/when it changed.
So you're getting the best you can get, barring changing core Windows functionality. Mind you, the LabVIEW platform had 1 mS timestamp granularity for eons, but that was because NI expressly modified something in Windows to give that added granularity.
That's why you get 3 lines with the same timestamp, anyway.

4 Likes

Thanks everyone, it guess the problem was from my Mac. Not arduino.

Right, so not Windows, but rather a Mac aberration. That's okay, too. I'll store that 33 ms increment away for future answers.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.