Hi
I feel stupid asking this but im very new to arduino electronics
how do I blink multiple LEDs? :-/
I think I have the correct wiring but there may be a problem with the coding.
Is there anything anyone could point me towards that could help?
Hi
I feel stupid asking this but im very new to arduino electronics
how do I blink multiple LEDs? :-/
I think I have the correct wiring but there may be a problem with the coding.
Is there anything anyone could point me towards that could help?
It really depends on how you will be wiring your LEDs. If you are wiring the LEDS in some series/parallel combination then having them all blink on and off is the same software as blinking a single LED. If you wish to control which LEDs blink at different times then you have to assign LEDs to different digital output pins and command them with software. An important thing when controlling LEDs is to be sure you have some form of current limiting so that you don't draw to much current from a output pin. This is usually done with resistor(s).
If you show us your wiring it will be more clear what you are trying to accomplish.
Lefty
Thanks
Well as i'm new to arduinos and the program environment they use I am just playing around to get used to how they work.
I am trying to program each LED to blink independently and at different rates.
I am using the pins 2, 3 and 4 as LED outputs going into a breadboard, the current then goes through resistors which pass to the LED's. I have wired the ground from each LED to the V1 row which the ground wire from the arduino is also connected.
Heres the code I am working on:
int ledPins[] = {
2, 3, 4 }; // An array of pin numbers to which LED's are attached
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pins as an output:
pinMode(ledPins [2, 3, 4], OUTPUT);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
digitalWrite(ledPins [2], HIGH); // set the LED on
delay(300); // wait for a second
digitalWrite(ledPins [2], LOW); // set the LED off
delay(300); // wait for a second
digitalWrite(ledPins [3], HIGH);
delay(600);
digitalWrite(ledPins [3], LOW);
delay(600);
digitalWrite(ledPins [4], HIGH);
delay(1000);
digitalWrite(ledPins [4], LOW);
delay(1000);
}
The code seems to verify with no errors and the L light on my arduino is lit but the LEDs do not blink.
Well, lots of things not quite right there.
First, when posting code, please use the #(Code) button in the editor.
ledPins has only three entries, but you've got odd constructs like:
" pinMode(ledPins [2, 3, 4], OUTPUT); " (I'm surprised this compiled!)
and
"digitalWrite(ledPins [3], LOW);" (The largest subscript of "ledPins" is 2)
Try:
int ledPins[] = {
2, 3, 4 }; // An array of pin numbers to which LED's are attached
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pins as an output:
for (int i = 0; i < sizeof (ledPins) / sizeof (ledPins [0]); ++i)
pinMode(ledPins [i], OUTPUT);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
digitalWrite(ledPins [0], HIGH); // set the LED on
delay(300); // wait for a second
digitalWrite(ledPins [0], LOW); // set the LED off
delay(300); // wait for a second
digitalWrite(ledPins [1], HIGH);
delay(600);
digitalWrite(ledPins [1], LOW);
delay(600);
digitalWrite(ledPins [2], HIGH);
delay(1000);
digitalWrite(ledPins [2], LOW);
delay(1000);
}
Just FYI, the comma operator in C evaluates each of the operands and returns the value of the last one. So [2, 3, 4] evaluates to the same as [4].
Andrew
good to know stuff, I would have never guess that a comma did that in C (cause except for narrow focus stuff like arduino i am c-tarded)
Here's a good page about the comma and why you might want to use it.
http://www.eskimo.com/~scs/cclass/int/sx4db.html
I can't help thinking that people who are new to the Arduino and have never done any programming of any sort might do better to play around with something like Processing first. Otherwise you're trying to learn programming at the same time as wondering if you've plugged the right wires in the right place.
Andrew
maybe, but Im just getting my head wrapped around processing lol, but Ive scripted for fun for many years, I forget how much trouble I had back in the early days sometimes
The biggest error is during the delay function. That blocks anything else from happening look at the code in:-
Thanks for the helpful posts, I tried the code and it worked straight away. I think my biggest hurdle is getting my head around the programming as i've had experience with electronics before and other programming languages such as supercollider and max/MSP but arduino programming is new to me.
Would like to ask more questions but I think i'll try and figure out some stuff before I ask so I don't waste your time! lol
really appreciate the support and I hope to post more soon.
regards
Hi again I have been trying to make sense of the bit of code you sent me
i.e. for (int i = 0; i < sizeof (ledPins) / sizeof (ledPins [0]); ++i)
pinMode(ledPins , OUTPUT);
I cant find a tutorial on it and it would really help if i understood what it means.
If someone could explain it to me that would be a great help!
Regards
I assume you mean this:
sizeof (ledPins) / sizeof (ledPins [0])
bit? (the rest is just a plain "for" loop).
"sizeof" returns the number of bytes in its argument.
"ledPins" is an array of "int"s, in this case there are three of them, and on the Arduino, an "int" is two bytes long, so "sizeof(ledPins)" returns 6.
Now "ledPins[0]" is the first element of the array "ledPins", so it is an "int", so "sizeof (ledPins[0])" returns 2.
6 / 2 = 3, the number of elements in "ledPins".
Imagine you'd ported your code to an architecture where an "int" is 32 bits (four bytes) long. The operation still returns 3.
Hey all, I am new to arduino as well as processing and I am trying to do something similar but with the blink w/out delay as the base code. How might I employ these same three pins 2,3,4 to work with blink without delay? Would I need to declare different intervals? or simply just copy the loop 2 more times and play with the interval? This is about as far as I am...
int ledPin2 = 2;
int ledPin3 = 3
int ledPin4 = 4// LED connected to digital pin 13
int value = LOW; // previous value of the LED
long previousMillis = 0; // will store last time LED was updated
long interval = 1000; // interval at which to blink (milliseconds)
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as 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 blink the LED; that is, is the difference
// between the current time and last time we blinked the LED bigger than
// the interval at which we want to blink the LED.
if (millis() - previousMillis > interval) {
previousMillis = millis(); // remember the last time we blinked the LED
// if the LED is off turn it on and vice-versa.
if (value == LOW)
value = HIGH;
else
value = LOW;
digitalWrite(ledPin2, value);
}
}
Thanks guys