Pages: [1]   Go Down
Author Topic: Multiple Things at Same Time  (Read 540 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 626
Posts: 34123
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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..
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26278
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
« Last Edit: November 28, 2012, 11:01:40 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
https://github.com/billroy/run -- 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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
https://github.com/billroy/run -- 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
« Last Edit: November 28, 2012, 11:20:05 am by ekscden » Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 626
Posts: 34123
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Seems like no matter what technique, I'd be stuck with ....
No you are stuck with non of those.

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2298
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: [1]   Go Up
Jump to: