arduino loop time

Hi all,

I have an arduino code and I assume it outputs 250 no of samples for 1 sec. How can I ensure that arduino code runs for 1s and outputs 250 samples. Pls help.

Samples? Please explain as if we don’t already know what you are talking about.

Perhaps you should post your code between tags.

reva23:
Hi all,

I have an arduino code and I assume it outputs 250 no of samples for 1 sec. How can I ensure that arduino code runs for 1s and outputs 250 samples. Pls help.

It would help if you posted the code. (As Chris also suggested.) It needs to be placed between code tags using the </> button in the “Reply” window. (Not necessarily visible in the “Quick Reply” window, depending on your settings.)

You could use ‘millis()’-based timing to take a sample every 4mS, increment a counter after each reading, then wrap it in an ‘if’ statement that only takes a sample if the counter is less than 250.

reva23:
Hi all,

I have an arduino code and I assume it outputs 250 no of samples for 1 sec. How can I ensure that arduino code runs for 1s and outputs 250 samples. Pls help.

Your sampling method could make 250 Hz data impossible, but that is very unlikely.

250 samples / second = 1 sample every 4 milliseconds. Lot of time, 64000000 cpu cycles in 4 ms.

Use the Blink Without Delay technique to get and report a sample every 4 ms instead of blinking a led slow enough to see it blink.

Do you wonder what the Blink Without Delay technique is?

And how exactly exact does the timing of the reads need to be?

reva23:
I have an arduino code and I assume it outputs 250 no of samples for 1 sec. How can I ensure that arduino code runs for 1s and outputs 250 samples. Pls help.

The bottleneck of your application might be either "sampling fast enough" or "sending to output fast enough".

You make sure by debugging your application and watch how fast everything actually is.

So what?

You could check the timing like this

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  Serial.println(F("Loop Timing"));
}

void loop() {
  static unsigned long lastSecond;
  static unsigned long loopsPerSecond;
  unsigned long topLoop = millis();
  loopsPerSecond++;

  //

  if (topLoop - lastSecond > 1000) {
    lastSecond = topLoop;
    Serial.print(loopsPerSecond);
    Serial.print(F(" lps "));
    unsigned long nanosPerLoop = 1000000000L / loopsPerSecond;
    Serial.print(nanosPerLoop);
    Serial.print(F(" nS "));
    Serial.print(nanosPerLoop / 65);
    Serial.println(F(" cycles"));
    loopsPerSecond = 0;
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  }
}
    Serial.print(nanosPerLoop);
    Serial.print(F(" ns "));

Fixed that for you.
Nanosiemens (nS) is a unit of conductivity, not a unit of time.

oqibidipo:

    Serial.print(nanosPerLoop);

Serial.print(F(" ns "));



Fixed that for you.
Nanosiemens (nS) is a unit of conductivity, not a unit of time.

Wow, that will help the OT a lot more than my code.

I just had to let the steam out at some point... Really aggravating to see this all over from people who should know better.

Yes, totally off topic.

reva23:
I have an arduino code ...

We, however, do not (have your code).

How to use this forum

Please use code tags.

Read this before posting a programming question

Is this your code?

Attached is the code

_112.ino (391 Bytes)

reva23:
Attached is the code

Don’t attach it, post it inside code tags like this:

....code

Short code like yours, posted inside a message, can be.

byte pin1 = A0;
int val1 = 0;
unsigned long looptime = 0;
int loops = 0;
unsigned long start, finished, elapsed;

void setup(){
  Serial.begin(9600);
}

void loop()
{
  unsigned long currentMillis = millis();

  for ( int i=0; i <250; i++)
  {
    val1 = analogRead(pin1);
    Serial.println(val1);
  }
  if(currentMillis - looptime >= 1000)
  {
    looptime = looptime +1000;
    loops++;
    delay(500);
  }
}
for ( int i=0; i <250; i++)
  {
    val1 = analogRead(pin1);
    Serial.println(val1);
  }

Doing 250 Serial prints, in one second at 9600 baud, is probably not feasible.

At 1.04 µs per byte (1 / 960), and assuming you are outputting 4 digits plus cr/lf, I estimate that 250 of them will take 1.563 seconds.

oqibidipo:
Fixed that for you.
Nanosiemens (nS) is a unit of conductivity, not a unit of time.

I used to do that, but after being told off on another forum, got it right. :slight_smile:

Also, remember that 1000 is "k" and not "K", however a million is "M" (not "m"). Kind of confusing.

After all, in computing we try to be precise.