Arduino & thread

I've googled a little about arduino's thread, and I found the protothread

Did somebody try it?
What do you tink about the ptTrhead?

This is a typical case of: If you need this feature, it might help you. If you can live without it, you're better off without.

On regular PC and more powerful controllers, threads have become the standard way to approach problems because they work quite well on them. But on an Arduino, concurrency and multi-threading are in general a bad idea, except in the few rare cases where they are exactly the needed solution.

So the more interesting questions are:

  • Why do you think you need them?
  • Do you have one of the rare cases where they're a good idea?

Korman

Well... i've a RTC (DS1307), and needs to be "refreshed" to have the real time, so it would be "cleaner" if a thread is dedicate to time update ...
However, it is safer if done in sequence, but ... a thread take away the task of the refresh by functions / user

Well... i've a RTC (DS1307), and needs to be "refreshed" to have the real time, so it would be "cleaner" if a thread is dedicate to time update ...
However, it is safer if done in sequence, but ... a thread take away the task of the refresh by functions / user

Notice how nobody else seems to have had this issue...

You have the wrong idea about what you need.
Look at the 'blink without delay' example and consider just creating a function for grabbing the time every so often.

Threads are evil. Avoid them.

For one thing you don't have a heap of memory or program space to be implementing threads. For another, a simple check in a loop will probably suffice, failing that use an interrupt.

Threads introduce their own problems, like concurrent access to the same memory address, so they need to be used in conjunction with locking of parts of your program so this doesn't happen. Some processors have interlocked-increment instructions to help with managing this. I doubt that the Atmega has such things.

Unfortunately I need something that is precise enough, which is guaranteed by the RTC, which also manages the day which comes in handy.

probably no one has had this problem because no one has needed such a thing;)
or maybe my programmer mindset leads me to things rather insane XD

But I saw very "cute " a thread that masked the update time, perhaps with a mutex, just to ensure proper access time, so that each cycle, the thread can get in a couple of seconds to sleep leaving the processor time to advance the other threads (one would have to devote to light sockets, while listening to another user input)
Then, again, will be the distorted mentality of the programmer XD

p.s. are you tolking about http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay ?
how accurate millis()? millis() - Arduino Reference

This number will overflow (go back to zero), after approximately 50 days.

This is a significant problem for the application you want to achieve

Unfortunately I need something that is precise enough, which is guaranteed by the RTC

You're using a DS1307 so those two comments can't go together - the DS1307 is not that precise.

Ok I'm not quite understanding what you want to do. Do you want to do something at a precise interval?

Brig:
or maybe my programmer mindset leads me to things rather insane XD

Perhaps.

Brig:

This number will overflow (go back to zero), after approximately 50 days.

This is a significant problem for the application you want to achieve

That can be worked around. If the time now is less than the last time you took a couple of minutes ago, you got the wrap-around. Then you compensate for it.

I don't see how threads are solving a problem. If the timer is not accurate, the threads won't make it accurate. If the clock is not accurate, the threads won't make it accurate.

In any case you could get a GPS module, which (assuming it gets a signal) will always give you an accurate time.

Failing that, get a temperature-compensated RTC.

... one would have to devote to light sockets ...

Your requirement to turn the lights on and off automatically must be a lot more precise than mine.

Failing that, get a temperature-compensated RTC.

In theory the GPS could be rather less accurate than this - the DS3231 is quite nice but in most applications, completely unnecessary (it does have the crystal and other guff inside it though which makes it rather nice)

I know the problem of counting can be avoided, but I would complicate the code a bit, also to take account of the day etc etc ... of course everything can be done, but I am for the simple things XD

you say you use millis and "caring" for RTC? well ... This brings me to save a class, but I should make myself a library (at least in c) to do something decent (unfortunately, the programmer mentality compels me to do something decent in all cases, trying to ensure stability and maintenance of the code )

Now, it is not something that I need to specify the milliseconds, because the latency of the links, when requested to do when I receive the RTC is already a gap of time ...

Basically I need to run a timer that can be set as desired (not in code) and make that arduino is dedicated to the control of the temporal conditions / environment to turn on / off the jacks

Ok now I'm completely and utterly confused as to what you're trying to do.

Brig:
... the programmer mentality compels me to do something decent in all cases, trying to ensure stability and maintenance of the code ...

Exactly. Which is why you avoid threads.

My problem is to manage an aquarium. To do this I need to have control over the sockets (on / off).
The decision to turn on / off is due to time or (not &) environmental conditions (eg temperature).
I must therefore have a watch that allows me to assess whether or not it is time to turn on / off a wall.

Currently I have based my project on RTC, which is updated by a method call in the code.
I do not like this, it lets to the programmer the responsibility to ensure that the time is updated (which is needed both with millis () and RTC).
I wanted to introduce a thread to raise the programmer from this responsibility (update the time without calling a method)

From what I understand, you are not advised to use a thread, and suggested to do with millis () what I did with RTC (leaving the code sequence)

p.s. of course I carry around with thread the mutex XD

Brig:
Currently I have based my project on RTC, which is updated by a method call in the code.
I do not like this, it lets to the programmer the responsibility to ensure that the time is updated (which is needed both with millis () and RTC).
I wanted to introduce a thread to raise the programmer from this responsibility (update the time without calling a method)

This thread, it won't be written by a programmer, right?

Look, you are making this way, way too complicated.

This aquarium isn't rocket science. You have a RTC which, once set, tells you the time to within a few seconds for the next ten years.

In your main loop you check the time (and the temperature), and turn things on and off. That's it.

come, by which we understand each other:)
I know that the thread should write a program, but it seems more correct and beautiful than seeing a RTC.refresh () inside the loop ()
then a thread is more difficult to forget that a line in the loop ().
Then I could put the launch of the thread in the constructor of the RTC, thus making it even more invisible to the user of the library:)

Nick, now I've used the RTC; I can also use the millis(), and as you said, no need for a missile base;)
Just wanted to separate the RTC update the code;) (loop () or other methods)

My problem is to manage an aquarium.

An aquarium... is that it?

You really are making this much too complicated.

but it seems more correct and beautiful

You sure you shouldn't be doing art?

It's about function not form - when it's stuck on the chip - nobody's gonna see it again so who cares how it's written as long as it works well.

Well I just found this topic and have no idea what timing accuracy and threads and mutexs can possibly have to do with controlling a socket or two for an aquarium?

Currently I have based my project on RTC, which is updated by a method call in the code.

So read the time and the temp and make a decision.

I do not like this, it lets to the programmer the responsibility to ensure that the time is updated

Isn't that the point of an RTC, it does all the updating, you just read the time.

Sorry Brig, I'm as confused as it appears everyone else is :slight_smile:


Rob

The fact is that I do not know how to explain (better than I did) XD

I know what does RTC etc etc ... "my problem" was to ensure that "no one" should clearly read the values ??of RTC and update the variables, but to entrust this task to a thread, working with a mutex, guaranteed variable date and did not require a dedicated line of code updating of the variables (RTC.refresh ())

This is how I would have done if I had to deal with this problem for a PC ...
then it is not a mortal thing, because for now it works sequentially ... But I wanted to use the thread .. this can be said of my problem ...

but if not recommended the use of thread ... I'll try to go as I have done so far, certainly no one dies XD (because then I would have liked, if possible, keep the source of my libraries, but distribute the objects of the same)

or maybe the problem is that I use a translator who can translate phrases incorrectly, or confuse the concepts ... Unfortunately, I have a really rusty English and we will get an eternity to make speeches in English XD

Some sentences have been hard to understand but the underlying concept of why you can't just have a 'dedicated line of code' for refreshing the RTC is confusing us all.