Hey guys, I'm a newbie to Arduino and I would like to know how do I change the blinking LED code to make 2 LEDs blink at the same time. I use Windows Vista and an Arduino Uno, I have some jumper wires, a breadboard, resistors, LEDs and of course the Arduino but all I want is the change in the code I think I can handle the Hardware part myself.
I would like to know how do I change the blinking LED code to make 2 LEDs blink at the same time.
You can't.
You can make one turn on, then a few (hundred) nanoseconds later, the other one will come on (or go off). If that is acceptable, simply define another led pin, and add two more digitalWrite() calls - one to turn the 2nd pin on and one to turn it off.
use a common transistor on a pin out, digital pin out to base on the transistor (10k in series), collector + 5v out on arduino, and emitter to a 300 ohm resistor, wire both grounds (short wire) of LED to the resistor (gnd) and + on a digital out pin from your arduino..
both LED's will come on "identically" i say that, but how long does it takes for the electrons to flow one LED will light up quicker, probably the closest /first LED it reaches .. but i'm sure it's quicker than Arduino outputting on 2 pins.
Direct port manipulation would do it for for you I think, but I rather suspect that the requirement is not as stringent as stated and PaulS' solution will be satisfactory.
If you connect them in series I think they will come on at roughly the same time - of course you need to make sure your eye is an equal distance from both LEDs to avoid any propagation delay issues.
Once you change the requirements to have two LEDs flash concurrently but not identically, the problem gets much more interesting and the solution gets more useful.
If you just want 2 LED's blinking "at the same time" not "at exactly the very same instant" then, Paul S (Repy #1) is explaining the easiest solution. You'll see them both blink "at the same time". Just try it, it's free!
Do not over complicate things, specially for newbies, like me.
This can be done by connecting a couple or more LED's in series while using pin 13 as an output. Just make sure the output power is still enough to make all connected LED's lit. :.
how do I change the blinking LED code to make 2 LEDs blink at the same time
Do you want them to both go on and off with the same frequency or each with their own, different, frequency ?
The answers so far explain how to do the first option. Look at the BlinkWithoutDelay example in the IDE to see how to blink an LED without using delay() which is the first step along the way to blinking 2 (or more) LEDs at different frequencies.
I know its been a very long time since anyone contributed to this post but in the interest of not duplicating posts and the fact that this one comes up first in some Google searches on the subject I feel compelled to add to it. This is a modification of the oneOnAtATime() sketch that is available in the libraries. I've created two methods, posted below:
int ledPins[] = {2,3,4,5,6,7,8,9};
void setup()
{
for(int i = 0; i < 8; i++)
{
pinMode(ledPins*,OUTPUT);*
* for(int i = 0; i <= 7; i++)* * {* * int offLED = i - 2;* * if(i == 0)* * {* * offLED = 7;* * }* * if(i == 1)* * {* * offLED = 6;* * }* _ digitalWrite(ledPins*, HIGH); digitalWrite(ledPins[offLED], LOW); delay(delayTime); } }* The two methods produce slightly different results, personally I like the MathMethod() sketch but to each his (or her) own. Enjoy!_
UKHeliBob:
Do you want them to both go on and off with the same frequency or each with their own, different, frequency ?
The answers so far explain how to do the first option. Look at the BlinkWithoutDelay example in the IDE to see how to blink an LED without using delay() which is the first step along the way to blinking 2 (or more) LEDs at different frequencies.
Blinking at different frequencies is one of those things that's easy with a state machine framework:
The reason the code turned into italics, was the [i] in the ledPins[i] in the code. Because it's what turned italics on, the [i] is no longer there. So just editing the post and adding code tags, will not mean the sketch is correct: the [i] as in ledPins [i] is missing.
PeterH:
If you connect them in series I think they will come on at roughly the same time - of course you need to make sure your eye is an equal distance from both LEDs to avoid any propagation delay issues.
Once you change the requirements to have two LEDs flash concurrently but not identically, the problem gets much more interesting and the solution gets more useful.
I love this reply, I think I've come to the right forum:
"you wanted the same time.... see, and the speed of light, it takes time to travel you see, so the led further away..." that's rich