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
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.
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.
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.
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.
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.
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.