Hi there, what would be the best way to run a sequence every say 6 hours?
I basically want to read a sensor and output the result to serial or file every x amount of hours.
Thanks
Hi there, what would be the best way to run a sequence every say 6 hours?
I basically want to read a sensor and output the result to serial or file every x amount of hours.
Thanks
See https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay for an example of a periodic activity.
If it is a battery powered system, there other things you should consider to increase battery life.
lloydowen:
Hi there, what would be the best way to run a sequence every say 6 hours?
How exactly 6 hours?
6 hours +/- one hour?
6 hours +/- one minute?
6 hours +/- one second?
With boards in R3 design like "Arduino UNO R3" or "Arduino MEGA2560 R3"you will have to use a RTC (realtime clock) module to get better accuracy than 2 minutes within 6 hours
Exactly. If is a data logging application, he'll probably anyway want a timestamp with data items and will want to handle the situation where the arduino restarts, so integrating an RTC would be good.
The quick and dirty way, if your interval isn't particularly critical and you are connected to reliable external power:
void loop() {
Serial.println("The value you want to send to the PC");
delay(5UL*60UL*60UL*1000UL); // Wait 5 hours
}
The delay() function will wait for up to 49 days, 17 hours, 2 minutes and 47.295 seconds, if necessary. The ceramic resonator in the Arduino UNO isn't extremely accurate so expect the time to drift up to about ten seconds a day.
Sorry for snaching the thread.
I want to do the same. I want to run my loop (driving a servo for X ms) every 12 or 24 hours.
How can I do that with the option to still have a button that will execute the servo rotation beside this hour counter?
Code using millis() works for intervals up to over 49 days:
// constants won't change. Used here to set a pin number :
const int ledPin = 13; // 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 unsigned long interval = 12UL*60UL*60UL*1000UL; // Change the LED every 12 hours
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
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 toggle 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 time you should have toggled the LED
previousMillis += interval;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
johnwasser:
Code using millis() works for intervals up to over 49 days:
Thank you I will try this in my simulator as my Arduino stuff have not jet showed up.
A few questions though.
Can I reset the unsigned long interval constant?
To be able to run the program over 49 days?
Or can I/do I have to declare it as a variable?
Example:
IF (currentMillis - previousMillis >= interval)
My servo movements
unsigned long interval=0
.....and what is "previousMillis += interval" ? I´ve read like 3 guides and watched 2 guides on the Tube.
Moderator edit: please mind your language.
Read this ten times.
currentMillis is the sample taken from a constantly running millis() clock.
previousMillis is the saved time of the last action.
interval is the time between actions.
When you start the Arduino, the millis() clock is 0, and running.
There is no previousMillis yet (it starts as 0).
"if" checks constantly if "currentMillis" is "interval" more than "previousMillis".
If that is true (the "interval" time has passed), some action is taken.
One of the actions is that previousMillis is made the same value as currentMillis.
Then the "interval time passed" is not true anymore, and the process starts again (from a new point).
Leo..
The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing. It may help with understanding the technique.
Because it uses subtraction to test if the interval has elapsed it is not affected by millis() rollover and will run properly indefinitely.
Note that it will only be as accurate as the Arduino 16MHz oscillator so for accurate long term timing an RTC is needed.
...R
Code using millis() works.
I really appreciate your help.
Still can´t figure out why this millis() don´t work, and I don´t consider myself new in programing. I program industrial robots and PLCs for living. But this...
With the program below the programpointer enters the if statement always. Not waiting for 5 seconds.
I wrote this:
#include <Servo.h>
Servo TempServo;
unsigned long previousMillis = 0; **
const unsigned long interval = 5000; **
void setup()
{
** TempServo.attach(13);
}
void loop()
{
** unsigned long currentMillis = millis();
** if (currentMillis - previousMillis >= interval);**
** previousMillis += interval;**
** TempServo.write(179);**
** delay(1200);**
** TempServo.write(1);**
** delay(1200);**
}
Also if the Arduino is drifting in time (10 second /day).
Why not use a simple counter that resets after the IF statement is excecuted? Should be more accurate?
Ex.:
If counter1 = 24 hours
TempServo.write(179);
delay(1200);
TempServo.write(1);
delay(1200);
Or something similar?
if (currentMillis - previousMillis >= interval);
What does the ; do
Suggest you always use { } with the if() function.
.
Untested.
Leo..
#include <Servo.h>
Servo TempServo;
unsigned long previousMillis, currentMillis;
const unsigned long interval = 15000; // interval time
void setup() {
TempServo.attach(13);
}
void loop() {
currentMillis = millis(); // update with every loop
if (currentMillis - previousMillis >= interval); { // test
previousMillis = currentMillis; // new startpoint
TempServo.write(179);
delay(1200);
TempServo.write(1);
delay(1200);
}
}
LarryD:
if (currentMillis - previousMillis >= interval);What does the ; do
Suggest you always use { } with the if() function.
.
Wawa:
Untested.
Leo..
Thank you very much!
I changed the code as shown below and It works.. (Sorry Wawa, did the change before your post).
Why didn´t the compilator say that {} was missing in the if state? Strange.
#include <Servo.h>
Servo TempServo;
unsigned long previousMillis = 0; // will store last time updated
const unsigned long interval = 5000; // Change every 12 hours (12UL60UL60UL*1000UL)
void setup()
{
** TempServo.attach(13);**
}
void loop()
{
** unsigned long currentMillis = millis();**
** if (currentMillis - previousMillis >= interval)**
** {**
** previousMillis += interval;**
** TempServo.write(180);**
** delay(1200);**
** TempServo.write(0);**
** delay(1200);**
** }**
}
if (currentMillis - previousMillis >= interval);
Looks like you are still using the ; at the end of the line.
Now you changed it, BAD.
LarryD:
Now you changed it, BAD.
Yeah, sorry, I pasted the wrong code.
This is the current one and running like a charm:
#include <Servo.h>
Servo TempServo;
unsigned long previousMillis = 0; // will store last time updated
const unsigned long interval = 5000; // Change every 12 hours (12UL60UL60UL*1000UL)
void setup()
{
** TempServo.attach(13);**
}
void loop()
{
** unsigned long currentMillis = millis();**
** if (currentMillis - previousMillis >= interval)**
** {**
** previousMillis += interval;**
** TempServo.write(180);**
** delay(1200);**
** TempServo.write(0);**
** delay(1200);**
** }**
}
My Metro (an Arduino, basically an Arduino Uno) drifts about 60 seconds per 24 hours (24 hours is actually 23:59) which is still several times better than the worst case specification of the resonator used for the clock. If I really cared, I would use an RTC, or a wireless shield to access a timeserver via NTP, or I would compensate for the resonator.
istvanuino:
Why didn´t the compilator say that {} was missing in the if state? Strange.
The line
unsigned long currentMillis = millis();
is perfectly valid code as far as the compiler is concerned. So is
myAge = 21;
The fact that the code is useless or wrong does not concern the compiler.
...R