hi frinds, i wold like to make a long time loop, sumthing like "if" loop that will be finished after one day(to start relay in the same hour in the day). i have tried so many options and everything was failde.
i'm talking about very simple circuit.
any help will be great!
this is the un working sketch
int relay = 2;
int LED1 = 3;
int LED2 = 4;
int LED3 = 5;
int LED4 = 6;
int LED5 = 7;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
pinMode(LED1, OUTPUT);
digitalWrite(LED1, LOW);
pinMode(LED2, OUTPUT);
digitalWrite(LED2, LOW);
pinMode(LED3, OUTPUT);
digitalWrite(LED3, LOW);
pinMode(LED4, OUTPUT);
digitalWrite(LED4, LOW);
pinMode(LED5, OUTPUT);
digitalWrite(LED5, LOW);
}
int n=0;
// the loop function runs over and over again forever
void loop() {
for(int y=0; y<=24; y++)//24 hours
{
for(int x=0; x<=3600; x++) //60 min
{
delay(60000);// 1 min
}
}
digitalWrite(relay, HIGH);
delay(10000);
digitalWrite(relay, LOW);
n++;
if (n == 1){ digitalWrite(LED1, HIGH);}
if (n == 2){ digitalWrite(LED2, HIGH); }
if (n == 3){digitalWrite(LED3, HIGH);}
if (n == 4){ digitalWrite(LED4, HIGH); }
if (n == 5) {digitalWrite(LED5, HIGH);}
if (n == 6) {
n=1;
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
}
}
for(int y=0; y<=24; y++)\\24 hours
Backslashes for (inaccurate) comments?
How did the compiler respond to that?
Please remember to use code tags when posting code.
i add those now bay mistake
AWOL:
Backslashes for (inaccurate) comments?
Did you understand why he said inaccurate?
But anyway, that seems like a complicated way to delay for 24 hours.
What type of parameter does delay() accept? Will the ms in 24 hours fit in that type?
i have no idea, that's the risen i ask
There were links in reply #4 - did you follow them?
Let me put it this way. . .
how many minutes are there in a day?
1440
How many minutes in your sketch's day?
barakweizman:
i wold like to make a long time loop, sumthing like "if" loop that will be finished after one day
That is not how to do that.
When you want to know it is time for bed you don't stay watching the kitchen clock for 24 hrs?
Have a look at how millis() is used to manage timing in Several Things at a Time.
For long time periods it would probably be better to use a Real Time Clock (RTC) module as the Arduino is not a good timekeeper.
...R
thank for all yours answers!
i will explain that again.
all i wanted to do is to:
delay 24 hours
start (some pin) for 10 seconds
all this....over and over again
how can i do it?!
As other mentioned, there are other ways to do it, but IF you want to do this the way that you're doing it, you need to change the timer.
You can do (pseudo code to follow)
//delay 1 seconds 3600 times 24 times.
for y = 0 to 24 //hours in a day
for x = 0 to 3600 //seconds in an hour
delay(1000);
or you can do
//delay 60 seconds 60 times 24 times.
for y = 0 to 24 //hours in a day
for x = 0 to 60 //minutes in an hour
delay(60000) //seconds in a minute
or you can do
//delay 1 second 86400 times
for y = 0 to 86400
delay(1000);
//delay 1 minute 1440 times.
for y = 0 to 1440
delay(60000);
etc etc etc.
But, what you can't do is delay 1 minute 3600 times and then 24 times.
i will try!!! thy!
barakweizman:
thank for all yours answers!i will explain that again.
all i wanted to do is to:
delay 24 hours
start (some pin) for 10 seconds
all this....over and over again
how can i do it?!
Which Arduino board do you have and how accurate do you want this 24 hour sequencing to be? Some
of the boards have accurate timing, some don't.
void loop()
{
delay(86400UL - 10000UL); // number of seconds in a day minus the 10 second delay
digitalWrite(relay, HIGH);
delay(10000);
digitalWrite(relay, LOW);
n++;
if (n == 1){ digitalWrite(LED1, HIGH);}
if (n == 2){ digitalWrite(LED2, HIGH); }
if (n == 3){digitalWrite(LED3, HIGH);}
if (n == 4){ digitalWrite(LED4, HIGH); }
if (n == 5) {digitalWrite(LED5, HIGH);}
if (n == 6) {
n=1;
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
}
}
Note that it is blocking so your Arduino can't do anything else useful.
And please learn to use code tags when posting your code on the forum so it looks like the above.
delay(86400UL - 10000UL);
is this some sort of scale model day?
OOPS.