Go Down

Topic: Multiple leds, independent. (Read 4010 times) previous topic - next topic

alanparfitt

For a project (a ghost house -don't ask!) I want to use 8 or so LEDS that come on and go off apparently at random. Sometimes they are all on at once, sometimes none. Some flash, some fade. I think I understand how to program ONE led to do this, but how can I get the arduino multi-tasking, i.e. controlling led 2,3 etc, independently of what it's doing with led 1? Is this what is called multiplexing, or are my wires twisted here? Bear with me please, I am new to this game and can remember when television was black and white, and only available in the better neighbourhoods...

parf

arduinodlb

I'll try to answer what I think you're asking.

1) Multiplexing and Multi-tasking are two very different things.
2) In the computer world, multi-tasking usually refers to the ability of an operating system to run multiple tasks at the same time. This is possible on the Arduino, but requires specialist code which I don't believe you need for your project.

I think what you mean is to control the LEDs independently. This can be done with single-task software in your case I believe, and it's much easier.

My guess is you want something like this:

Code: [Select]

loop()
{
   for (int i=0; i < NUM_LEDS; i++)
   {
        SetLed(i, nextLedState(i));
   }

   Delay(500); // delay 500ms to slow this whole thing down.
}


On the Arduino, the loop() function is called continuously. That is, every time you get to the end of the loop() function and exit, the Arduino calls the loop() function again.

This is just an example of how to can control multiple LEDs at the same time in a simple way. Hopefully you can adapt it to your specific needs.
Do not IM me. I will not respond. Ask questions in the forum.

MichaelMeissner

The general advice in doing multiple things with multiple timeouts is to look at the Blink without Delay example. http://arduino.cc/en/Tutorial/BlinkWithoutDelay

However, the above example leaves something to be desired, in that it doesn't show multiple things going on at a time, and it can be a conceptual block for people to understand things like state machines.  As usual, Nick Gammon wrote an excellent tutorial on how to do this: http://www.gammon.com.au/forum/?id=11411

arduinodlb

#3
Oct 31, 2013, 07:07 pm Last Edit: Oct 31, 2013, 07:14 pm by arduinodlb Reason: 1
Quote

However, the above example leaves something to be desired, in that it doesn't show multiple things going on at a time


Ummm, yes it does. I was trying to keep it simple as a starting point. Each LED could be independently set to whatever value it wants in the nextLedState(i) function. You hopefully don't need state machines for this. A simple table (or possibly a function depending on what he wants his final output to be) may be sufficient. Let's try to keep it simple.

Notwithstanding the rest of your post is helpful, and I'm sure it will be useful to the O.P.
Do not IM me. I will not respond. Ask questions in the forum.

MichaelMeissner


Quote

However, the above example leaves something to be desired, in that it doesn't show multiple things going on at a time


Ummm, yes it does. I was trying to keep it simple as a starting point. Each LED could be independently set to whatever value it wants in the nextLedState(i) function. You don't need state machines for this. A simple table would be sufficient. Let's try to keep it simple.

Notwithstanding the rest of your post is helpful, and I'm sure it will be useful to the O.P.

I dunno, I tend to feel keeping the delay time for each led is a simplified version of a state machine (the state being the time when you need to do something else).  Though it may be a matter of semantics and nits.

While the example does give the basics, over the last year as I've watched these forums, this question keeps coming up, because it isn't obvious for many people, particularly those that aren't programmers, how to get to the next step, where you need to replicate (either by having separate functions/variables, or by using an array of the timeout times, by encapsulating them in a class, having pseudo threads, or other ways) the code for each item having a separate timeout.

arduinodlb

You may be right. My view is if you're new to programming, you are SOOOOO not ready to hear about state machines, especially if they may not be necessary for the task. I think there's a danger in overwhelming people so they freak out and go "I can't do this". GIve them the initial steps, which they hopefully can cope with, then they go "yes, but..." and they're ready for the next step. Plus, they'll understand the answer better at that point. My 2c.
Do not IM me. I will not respond. Ask questions in the forum.

nickgammon

My page which MichaelMeissner has very kindly linked to avoids talking about state machines, and keeps it simple.

Short URL: http://www.gammon.com.au/blink
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

MichaelMeissner

In fact, I had been thinking of writing such an example program, and Nick saved me the trouble.  Hopefully it helped the OP.

alanparfitt

You guys are great.
Suddenly, I feel less alone (which is good) and more competent in setting up what I'm trying to do (which is great). I also feel less stupid, more in control and generally inspired. Thanx everyone, I think I'm on track now!

marco_c

You probably have everything you need, but I have a sketch called MultiBlink in my code repository (link below) that will probably do what you want. The data for blink rates, etc, are held in a data table and you can create sequences for an individual LED.

fungus

#10
Nov 18, 2013, 10:08 am Last Edit: Nov 18, 2013, 10:13 am by fungus Reason: 1

You may be right. My view is if you're new to programming, you are SOOOOO not ready to hear about state machines, especially if they may not be necessary for the task.


Teaching them about "delay()" doesn't help.

"delay()" is the devil's function.
Advanced Arduino

fungus


My page which MichaelMeissner has very kindly linked to avoids talking about state machines, and keeps it simple.

Short URL: http://www.gammon.com.au/blink



That's a good way to think about it...
Advanced Arduino

arduinodlb

#12
Nov 18, 2013, 10:22 am Last Edit: Nov 18, 2013, 11:05 am by arduinodlb Reason: 1


You may be right. My view is if you're new to programming, you are SOOOOO not ready to hear about state machines, especially if they may not be necessary for the task.


Teaching them about "delay()" doesn't help.

"delay()" is the devil's function.



I respectfully disagree. Teaching them about delay() most certainly helps. It's a function one uses all the time, for various reasons so yes, they are going to have to know about it. These are beginners bear in mind. Again, let's keep it simple.
Do not IM me. I will not respond. Ask questions in the forum.

nickgammon

For a simple project where you need to blink an LED for one second, or send a 500 mS pulse to unlock a door, I don't see the problem with delay(). Using other techniques is unnecessarily confusing, in those situations.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up