Question regarding the delay function

Hello,

I got some problems with the delay function for my Arduino Uno. Basically, I want to measure the voltage difference in the stratosphere and have made the code to do just that. The current problem is I don't know what the delay time should be between each measurement of the voltage difference. This is very important as the delay time between each measurement is when the system can charge itself up. That why I would like help to add a code to make sure there could be a varying delay time. I am looking to have a fixed set of delay times that would then alternate between each other. For example, that could be {10 secs, 30 secs, 60 secs, 600 secs}. Then for each measurement one of the times would be chosen. Then the next time would be chosen for the next measurement. It would then repeat over and over again until the experiment is done

Can anybody help me how that would look?

Thanks

Maybe a lookup table.

Put the delay() periods in an array and iterate through it to get the values
This shows the principle, but I have left plenty for you to do

unsigned long periods[] = {10000, 30000, 60000, 6000000};
byte index = 0;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
}

void loop()
{
  Serial.println(periods[index]); //do whatever you need with the period value
  index++;
  if (index > 3)
  {
    index = 0;
    Serial.println();
  }
}

Better still, use millis() for timing, again with the periods in an array

You have four delays lookup.
You can use a mega arduino with its 5 timers/counters set as overflow or CTC mode interrupts.
Put a flag under each ISR, and when it is true execute the corresponding code on your main loop.
Hope this help.

Thank you all so much for the messages. I will try them all right away!

Yeah, or you could just double or triple the interval each time, if those particular numbers are not special.

abdelhmimas:
You have four delays lookup.
You can use a mega arduino with its 5 timers/counters set as overflow or CTC mode interrupts.
Put a flag under each ISR, and when it is true execute the corresponding code on your main loop.
Hope this help.

Srsly? You recommend timers and interrupts and a Mega to the OP who might not even have a beginners grasp of rudimentary programming?

No wonder people get discouraged. The OP’s problem can be solved, elegantly or crudely, without any of that stuff, which actually might never be necessary for most Arduino hobbyists on these fora.

a7

Why not an analog dial (pot) and map it from10 to 600 seconds. Why all the certain selections?

-jim lee

UKHeliBob:
Put the delay() periods in an array and iterate through it to get the values
This shows the principle, but I have left plenty for you to do

unsigned long periods[] = {10000, 30000, 60000, 6000000};

byte index = 0;

void setup()
{
 Serial.begin(115200);
 while (!Serial);
}

void loop()
{
 Serial.println(periods[index]); //do whatever you need with the period value
 index++;
 if (index > 3)
 {
   index = 0;
   Serial.println();
 }
}






Better still, use millis() for timing, again with the periods in an array

It worked and I got it incorporated into my other program. Thanks again so much for the help!

UKHeliBob:
Put the delay() periods in an array and iterate through it to get the values
This shows the principle, but I have left plenty for you to do

unsigned long periods[] = {10000, 30000, 60000, 6000000};

Is it necessary to append the suffix "UL" with each of the members of the quoted array? If the answer is no, then under what situations we need to do it.

No, it is not necessary.
If you had arithmetic going on, then UL would be advisable.

TheMemberFormerlyKnownAsAWOL:
No, it is not necessary.
If you had arithmetic going on, then UL would be advisable.

Is it correct to say that the constant literals 6000000 would be taken by the compiler as "int" and then the value 0x8D80 instead of 0x5B8D80 for 6000000 would be assigned to "unsigned long period[3]"?

GolamMostafa:
Is it correct to say that the constant literals 6000000 would be taken by the compiler as "int" and then the value 0x8D80 instead of 0x5B8D80 for 6000000 would be assigned to "unsigned long period[3]"?

No, it's incorrect. The compiler reads the data type first, so it "knows" to carry out initialization using the same data type, in this case unsigned long. It will not be taken by the compiler as 'int' for the reason already stated in reply #10, it is a constant not an expression.

aarg:
No, it's incorrect. The compiler reads the data type first, so it "knows" to carry out initialization using the same data type, in this case unsigned long. It will not be taken by the compiler as 'int' for the reason already stated in reply #10, it is a constant not an expression.

Thanks for the clarification.

Just for additional clarity, this expresses the same values but would be wrong:

unsigned long periods[] = {10000, 30000, 30000 + 30000, 6 * 1000000};

you would need at least to change it to

unsigned long periods[] = {10000, 30000, 30000UL + 30000, 6UL * 1000000};

but I often do it this way:

const unsigned long MS_PER_SEC = 1000;
unsigned long periods[] = {10 * MS_PER_SEC, 30 * MS_PER_SEC, 60 * MS_PER_SEC, 6000 * MS_PER_SEC};

See how a clear expression can reveal the typo of an extra '0' that probably wasn't intended? :slight_smile:

aarg:
Just for additional clarity, this expresses the same values but would be wrong:

Verified as follows:

30000 + 30000 ==> gives wrong result. (4294961760;30000+30000=EA60---> EEEEEA60=+429496176) )
30000UL + 30000 ==> gives correct result. (60000; 30000+30000 = EA60 ---> 00000EA60 = +60000)

Again thanks for providing the examples.

alto777:
Srsly? You recommend timers and interrupts and a Mega to the OP who might not even have a beginners grasp of rudimentary programming?

No wonder people get discouraged. The OP’s problem can be solved, elegantly or crudely, without any of that stuff, which actually might never be necessary for most Arduino hobbyists on these fora.

a7

"Never necessary? " seriously?
please see the attached sketch.
the RTC option is still the best in my opinion, since the timing will be more accurate.

diffDelays.c (1.56 KB)

"Never necessary? " seriously?

As a bare fact, yes. I think the majority of Arduino enthusiast/programmers will never write timer code. I do mean never.

aarg:
As a bare fact, yes. I think the majority of Arduino enthusiast/programmers will never write timer code. I do mean never.

Why?

abdelhmimas:
Why?

Because it's relatively complicated, and most applications that require it have libraries available to do the job. Most Arduino users are application (project) focused, rather than software development focused. Many of them don't care very much how things work, they just want to construct a cool project. The whole program of Arduino is to empower those kinds of folks by insulating them from ugly things like timer registers.