Pages: [1] 2   Go Down
Author Topic: More complex, complete examples that do several things at once  (Read 1119 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I absolutely love the way Arduino makes it easy to interact with the physical world. No matter what, you find easy to reproduce examples that do what you want in a few lines.

What I am struggling now is doning several things at once, e.g.

Reading sensors values, transfering them to the serial port, displaying stuff on a LCD display with a minimal UI, receiving stuff from the computer to display on that LCD, reading out sensors ...

Each by itself is trivial, but once you want to do several things at once, there is no more waiting with delay(),
you have to take care of being "on time" for e.g. serial communications, do several things at once, but without
a real operating system to help out.

Are there "best practice" examples of more complete applications that show how to handle doing several things at once? I am sure what I want to do can be done, it is the complexity of programs that makes it hard for me.

I would love to see how other people do stuff like this, especially if it includes UIs and serial interaction with the computer.


TIA
/ralph
Logged

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

You might get some ideas on end-to-end control from Bitlash (http://bitlash.net) and Bitlash Commander (https://github.com/billroy/bitlash-commander)

Bitlash is a scripting language for arduino that makes it easy to do a lot of little things at once.  Bitlash Commander is a web interface toolkit that lets you put a web control panel on your Arduino project without too much fiddling with the serial and web plumbing.

Good luck with your project.

-br
Logged

France
Online Online
God Member
*****
Karma: 34
Posts: 977
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello and welcome,

Have a look at the "Blink without delay" example, it's a good start smiley-wink
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3350
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I told you to cook breakfast and answer the door if the doorbell rings, you wouldn't let your mom waiting outside for 30 minutes while you finish cooking the Bacon. You're given a stopwatch (millis()) and a pen and paper (variables); You should be able to multitask using those tools.
Logged

Online Online
Faraday Member
**
Karma: 80
Posts: 3785
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bacon never takes that long to cook, though...
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17254
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes your paradigm of having to do many things at once is just a personal mental roadblock. An arduino can execute instructions very fast and can make it seem as if it is doing many things at once even though it's just doing one thing at a time.

 So you have a list of independent tasks that have to be performed. You just have the main loop() function work it's way done that list, each task could be just a user written function that knows how to accomplish that one task. The only requirement is that any one task is not allowed to just stop and wait or loop testing for something before doing something. If it's not time for that task to do something it just returns control back to the main loop and next time the task is called from the main loop it checks if it's now time to do something or not. The arduino functions millis() gives you a free running counter that value you can use to measure passing time intervals.

Lefty
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3350
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bacon never takes that long to cook, though...

You haven't met my wife...
Logged

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

Thanks a lot for the responses, very helpful.

I really would love to see a more complete example of a real use-case doing LCD (or some other form of) UI, serial and some other things at once. In principle I know how it should be done, but like with the simpler examples, seeing how others do it (and do it well, unlike my own first exeriments) would help me, I think.

Are there no examples of larger projects with complete sourcecode to be examined by newbies?

It is also interesting to me how people organize code, what conventions they invent, etc.

Also it would interest me, how much people fit into their Arduinos

Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Using interrupts would be a simple way to achieve that.
Logged

Online Online
Faraday Member
**
Karma: 80
Posts: 3785
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My advice here (the programming, not cooking bacon) is to chart the program first. There are a number of ways to chart programs including the simple flowchart (which I learnt when learning Fortran) and the structure chart (which I learnt when learning Cobol).

Many many coding problems stem from us diving into the code without having thought through what it is we're trying to do, and how. And the more complex it is, the more fraught with danger it is....

Then once it's charted, maybe even flesh it out further on paper in pseudo-code. Pseudo code lets you write down the logic of the program without getting tied up in the nuances of the syntax.

And make a "coding to-do list" by mapping out the sketch (to use Arduino-speak) by typing in a heap of comments showing the structure of the code and that will remind you to code each part and not forget stuff.

I ALWAYS* do it that way....

Jim

* just kidding
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
   This is not a great example as the code has 'evolved' rather than follow a plan but it does update an LCD Every second, respond to new lap times instantly, plays an audio count down while responding to laps and updating the LCD, operates in a menu, lap review or recording mode etc. It does so through the two mechanisms already mentioned - interrupts for laps and time/timestamps for screen and audio updates.

PM Me your email address for a zip file of the code - like I say its not text book perfect but is an example of a very usable project that has been rapidly built/evolved on Arduino -

http://rcarduino.blogspot.com/2012/07/lap-timer-build-along-part-one.html

Duane B

rcarduino.blogspot.com
Logged


UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12520
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Using interrupts would be a simple way to achieve that.

Strongly disagree. Using interrupts is a very poor and unscaleable way to manage concurrent activities. Retrolefty has already explained how to write non-blocking code that enables a sketch to manage multiple concurrent activities in a simple structure which scales up very well. Using interrupts does not help with this problem, and just introduces a whole new layer of complexity to be dealt with.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Faraday Member
**
Karma: 23
Posts: 3469
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You have to choose what you need and for what reasons. Interrupts are often required for precise timing. Non blocking code is required for optimal resource utilization. Depending on what you want to achieve you may have to do both. For non blocking code it is often a good idea to think about state engines. Here is an example that uses both a state engine AND interrupts for different purposes: http://blog.blinkenlight.net/experiments/measurements/flexible-sweep/.
Logged

Check out my experiments http://blog.blinkenlight.net

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

Quote
Bacon never takes that long to cook, though...
If you do it right, it does. I cook bacon on a rack in the oven on 350 degrees (F, for those on the other side of the pond). No grease splatter, the the bacon always comes out crisp. It takes a bit longer than a frying pan, but, then cleanup doesn't take all day, either.
Logged

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 792
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I absolutely love the way Arduino makes it easy to interact with the physical world. No matter what, you find easy to reproduce examples that do what you want in a few lines.

What I am struggling now is doning several things at once, e.g.

TIA
/ralph

Check out the "Timer One" library: http://playground.arduino.cc/Code/Timer1

It allows you to run code at a defined repetition rate WHILE running other code.

It's useful if you want to do something at precise time intervals WHILE doing other things in the background (and don't want to worry how much time your other code uses).

All it really does is implement an ISR, but it's done with the high level library so all you do is specify the repeat rate and tell it which function to attach to (rather than complicated bit-level coding that would otherwise be required).

Hope this is of some help to you.
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Pages: [1] 2   Go Up
Jump to: