led new question

Hi everyone I am new to the forum and have a quick question. I am looking to take 2 led's and setting them up on the arduino to start blinking after 4 hours. It must blink 5 times a min for 30 then shut off the led's. I have the standard blink code,but I dont know how to edit it to make it blink both led's for given time... any help would be great. Thanks for all that help.

Think about how you'd do it, if you were switching on and off the LED.
One way would be to look at the clock (millis ()) when you start blinking the LED and noting the time.
Then, each time you've done a blink cycle, look at the clock again and see if 'x' minutes or seconds have elapsed since you started.
If not, carry on blinking.

Remember that over very long times, simple delay loops may accumulate significant error.

(this isn't really a hardware interfacing question)

Well thats what i was planning on doing. Just a 4 hour delay then after that i am not sure where to go. I know I can set the delay to blink after startup to a set time in between blinks, but i am so new to this its just hard to even know where to start from I guess.. Where should I have posted this if its not a interfacing question?

Well, it is a software question; it doesn't matter whether you're blinking a LED or sending serial messages, so it isn't really a hardware question.

If you know how long your blink is, simply divide that into the length of time you want to blink (hopefully you'll get an integer result :slight_smile: ), and wrap the whole lot up in a "for" loop or two.

Five times a minute is once every twelve seconds, so say 11 seconds off, one second on.
Do this 5 times, so that's a minute. Repeat thirty times for the full half-hour.

A good place to start is to outline in English exactly what you want to do, in the sequence you want it done

Delay 4 hours
Turn LED on for 6 seconds (is this what you mean when you say blink 5 times a minute?)
Turn LED off for 6 seconds
Repeat previous two steps 150 times (so total blink time is 150 * 6 second on and off times)
Turn LED off
What (if anything) should happen next?

Once you have each step outlined you can think about the code necessary to implement the steps. You may want to tinker with some shorter values to start with so you can test your code without having to wait four hours for something to start happening.

That'd be 150 * 12 seconds :wink:

it is 150 times for the two steps, each 6 seconds.

so total blink time is 150 * 6 second on and off times)
I added the words in bold to make sure its clear

Oops.
Quite so.
Sorry.

ok so i have been messing around with some code and i have it blinking the two led's but one will go on and do its thing then go off then the next one will come one and do its thing.. how does the code go for the led's to run together? thanks all

One minute’s worth.

for (int i = 0; i < 5; ++i) {
  digitalWrite (LED0, HIGH);
  digitalWrite (LED1, HIGH);
  delay (ONE_SECOND);
  digitalWrite (LED0, LOW);
  digitalWrite (LED1, LOW);
  delay (ONE_SECOND * 11);
}

If you #define ONE_SECOND as something less than 1000, you can speed things up and check all is working.

If you post your code it will be easier to suggest what you need to change

=============================================

int ledPin = 10; // LED connected to digital pin 10 and 6
int ledpin1 = 6;
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(ledpin1,OUTPUT);
}

void loop() // run over and over again
{
digitalWrite(ledpin1,HIGH);
delay(5000);
digitalWrite(ledpin1,LOW);
delay(2000);

digitalWrite(ledPin, HIGH); // sets the LED on
delay(5000); // waits for 5 second
digitalWrite(ledPin, LOW); // sets the LED off
delay(2000); // waits for 2 second

===========================================

If you look at my earlier example, you'll see how to get the LEDs to blink at the same time (OK, not strictly the same time, but I challenge anyone to see the delay with the human eye!).

Your rate would be more like 60 / 7 = 8.5 ish blinks per minute, rather than the five per minute you said earlier.

Ok guys got my code working now. Just one more question, is there any way to shorten it up the code that is? is there any way to not write down every single time i want it to turn on and turn off for given time. i guess i will show you what i mean.

digitalWrite (LED0, LOW);
digitalWrite (LED1, LOW);
delay (10000);
digitalWrite (LED0, HIGH);
digitalWrite (LED1, HIGH);
delay (5000);

digitalWrite (LED0, LOW);
digitalWrite (LED1, LOW);
delay (10000);
digitalWrite (LED0, HIGH);
digitalWrite (LED1, HIGH);
delay (5000);

digitalWrite (LED0, LOW);
digitalWrite (LED1, LOW);
delay (10000);
digitalWrite (LED0, HIGH);
digitalWrite (LED1, HIGH);
delay (5000);

the code above should give me about 45 sec of on off with the delay
well is there a way to compress the code so instead of telling it to turn on then off and so on, into somthing like (turn on and off this many times) then jump to next bit of code.

for (i = 0; i < LOOP_TIMES; ++i) {
 digitalWrite (LED0, LOW);
 digitalWrite (LED1, LOW);
 delay (10000);
 digitalWrite (LED0, HIGH);
 digitalWrite (LED1, HIGH);
 delay (5000);
}

If you change the “10000” and “5000” constants for
#define OFF_TIME 10000
#define ON_TIME 5000

then
delay (ON_TIME);
etc

it makes your program easier to maintain if you need to change timings.