Multiple Things at Same Time

Been playing with Arduino, but suspect this may not be possible:

I want to have one led flicker, two motors doing different things, and a servo doing something else all at the same time. Imagine a scene for a trainset where a building's light is flickering, little cars are pulled on a wire down the street, and a servo does something else. Is this possible with Arduino at the same time?

I have researched the Parallax Propeller and with the multiple cores this may be a better fit before I get too deep into learning Arduino.

Is this possible with Arduino at the same time?

Yes of course it is.
All these things are very slow, so they are done one after the other so that they look like they are happening at the same time.

Grumpy_Mike:

Is this possible with Arduino at the same time?

Yes of course it is.
All these things are very slow, so they are done one after the other so that they look like they are happening at the same time.

Is there a special way to program this? I've played with "for loops" but while it is in the loop flickering the light, it won't move the cars at the same time. Is there some special command to get it to do one pass through the light loop, then one pass through the car loop, and come back to the light loop to resume the fade? In a way, incrementing through all the separate loops at the same time? Hope that makes sense..

Is there a special way to program this?

Yes, look at the blink without delay example that came with the IDE for ideas, and break each of your actions into state changes and waits.
Do not use "delay ()".

The important thing to remember, as Mike said, is that all these mechanical devices move at glacial rate when your processor executes an instruction in the time it takes a beam of light to travel 20 metres.

Play around with the blink without delay example; use it to vary on/off times, or to fade one LED up whilst another is fading down, whilst a third is simply blinking.
If you find a delay() in your code, you've done it wrong.

Thanks for pointing me in the right direction. I'll rexamine those tutorials!

AWOL's guidance on avoiding delay() is the best advice you'll hear.

One way to accomplish what you're after is to use the Run library:
GitHub - billroy/run: Arduino function scheduler library: runs your C function at a specified interval, like the Bitlash run command -- there's an example of blinking two LEDs there.

After some practice you start thinking in terms of the pattern of breaking your program into parts that can happily run periodically.

-br

billroy:
AWOL's guidance on avoiding delay() is the best advice you'll hear.

One way to accomplish what you're after is to use the Run library:
GitHub - billroy/run: Arduino function scheduler library: runs your C function at a specified interval, like the Bitlash run command -- there's an example of blinking two LEDs there.

After some practice you start thinking in terms of the pattern of breaking your program into parts that can happily run periodically.

-br

The only problem I see is, for example, in flickering the light. It will need certain delays in it to get the proper flicker effect, so this will slow it down enough to stop the cars and other activities on the train layout. If I was going to just turn things on or off, I could see this working, but do have more control of speeds etc. I don't understand how it can work. That's why I had thought maybe a Propeller with the multiple cores since I'm not heavily invested in Arduino at this point.

Seems like no matter what technique, I'd be stuck with:
Flicker light by using a loop to fade it up and down
Exit loop and move the car
Enter another loop to manipulate the servo

Seems like no matter what technique, I'd be stuck with ....

No you are stuck with non of those.

It will need certain delays in it to get the proper flicker effect, so this will slow it down enough to stop the cars and other activities on the train layout.

No it won't if you program it correctly. The secret is a state machine of which the blink without delay is a very simple example.

Okay, I'll play with that and see if I can get it to work. Thanks for the info.

Take care with the servo lib from the playground as it uses the same pins as the motor shields, if you use it you can't use servos and motors. Of course you can control a servo with code which is very similar to "blink without delay".

Mark

Flicker light by using a loop to fade it up and down

Don't try to use a for loop to fade the LED. Take advantage of the fact that loop() already loops. Decide, on each pass through loop(), whether it is time to change the PWM value. If it is, change it, write it to the pin, and note when you did it.