I am using a couple different sensors with my Arduino Mini (in this case a temperature sensor) and I want to write some code that causes the void loop to end after two minutes. In other words, I want the program to save two minutes worth of data from the temperature sensor and then shut off. Is there an easy way to do this?
The Arduino is always running. The simplest approach is to put everything in setup() and exit the function after two minutes.
If you want to save power, you can buy an external power switch with digital input, or have the Arduino enter power down sleep mode.
PS: "void" in front of a function name (like loop()) means that the function does not return a value.
Yes. Use the millis() function to record the start time. As you gather the data, check the current value of millis() against the recorded start value. When the current value minus the start time exceeds 2 minutes, stop recording. See the blink without delay example.
I am not sure what "then shut off" means.
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
if (millis() >= 120000UL) { // Arduino has been running 2 minutes from power up.
for ( ; ; ) { // Never exits this loop.
}
}
}
About "void", read halfway this page the answer by Nick Gammon: programming - Why do people complain when I call my functions voids? - Arduino Stack Exchange, "Think of the word function as being there in spirit".
Koepel:
About "void", read halfway this page the answer by Nick Gammon: programming - Why do people complain when I call my functions voids? - Arduino Stack Exchange, "Think of the word function as being there in spirit".
So what I understand from this discussion board is that my error was in calling it a void loop, not in using a void loop in my code. Is this correct?
gp0478:
So what I understand from this discussion board is that my error was in calling it a void loop, not in using a void loop in my code. Is this correct?
You never posted your code.
gp0478:
So what I understand from this discussion board is that my error was in calling it a void loop, not in using a void loop in my code. Is this correct?
I don't know, but call it the function loop(), that is what we do
Do you understand the good solutions provided by the others ?
Koepel:
I don't know, but call it the function loop(), that is what we doDo you understand the good solutions provided by the others ?
Thank you, I understand now! The helpful @Perehama provided me with the code I'm looking for you. Thanks!