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...
Multiplexing and Multi-tasking are two very different things.
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:
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.
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.
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.
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.
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!
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.
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.
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.
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.
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.