How can I putthe arduino,for exemple,lihting a LED for 10s and remains off 23h59m50s.
I did try with "delay()" but didn't work with bi numbers.
ruineves:
.. bi numbers.
What?
I did try with "delay()" but didn't work with bi numbers.
I look forward to seeing the code you tried.
How accurate does the timing need to be ?
big numbers
You should definately not use delay(). You should use timing with millis() or you should use a library which does that for you. If your problem is "big numbers" then you are probably in need of different data types (eg. long instead of int).
for exemple
digitalWrite(13;HIGH);
delay(10000);
digitalWrite(13;LOW);
delay(86390);
didn't work, only did stop 20seconds even when using long parameters
delay() takes milliseconds as argument, 24 hours is 86400000 milliseconds.
Try:
digitalWrite(ledPin,HIGH);
delay(10000);
digitalWrite(ledPin,LOW);
delay(86390000UL);
// or 1000UL * 60 * 60 * 24 - 10000;
Literal numbers are assumed by the compiler to be ints (signed 16 bit integers that can hold values from -32768 to 32767) adding "UL" forces an Unsigned Long which can hold a tad over 4 billion (4294967295).
Better: ![]()
const byte ledPin = LED_BUILTIN;
void setup()
{
//Serial.begin(9600);
pinMode(ledPin,OUTPUT);
}
void loop()
{
static unsigned long timer = 0;
unsigned long tEnd = 86400000; // 24 hrs
int blinkTime = 10000; // 10 seconds
digitalWrite(ledPin,millis() - timer < blinkTime);
if(millis() - timer > tEnd)
timer += tEnd;
}
ruineves:
for exemple
digitalWrite(13;HIGH);
delay(10000);
digitalWrite(13;LOW);
delay(86390);didn't work, only did stop 20seconds even when using long parameters
// Define some time constants in milliseconds
const unsigned long SECONDS = 1000UL;
const unsigned long MINUTES = 60 * SECONDS;
const unsigned long HOURS = 60 * MINUTES;
const unsigned long DAYS = 24 * HOURS;
// Note: unsigned long is good for about 47 days in milliseconds
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(10 * SECONDS);
digitalWrite(13, LOW);
delay(1 * DAYS - 10 * SECONDS);
}
I have a program that turns my spa on for two hours in every twenty-four hours. It uses millis() and not delay(...), much as johnwasser has shown but using millis(). It drifts slightly but the drift is acceptable. There is a calibration offset for the drift that works for a specific Uno in a specific environment to reduce the drift.
Because of the steps needed to turn the spa on and off, I use a Finite State Machine and a slightly more complex program, but the principles are similar.
Danois90:
You should definately not use delay(). You should use timing with millis() or you should use a library which does that for you. If your problem is "big numbers" then you are probably in need of different data types (eg. long instead of int).
The advice not to use delay() is common here but is somewhat of a knee jerk reaction. If the program is doing nothing else then using delay() is perfectly acceptable.
The problem comes when we have not been told the whole story and/or when the requirement changes. For instance, in this case, if a requirement was added later to respond to an input during the delay() period.
UKHeliBob:
The advice not to use delay() is common here but is somewhat of a knee jerk reaction. If the program is doing nothing else then using delay() is perfectly acceptable.
Agreed, but a delay of nearly 24 hours is silly - OP should (if possible) put the arduino into low power mode instead ![]()