Pages: [1]   Go Down
Author Topic: stuck on how to loop through two different routines without threading  (Read 931 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have 5 analog input sensors, 3 quadrature encoders (for measuring motor speed), 3 motors, 5 LED lights connected to a 2560 Mega.

LED lights come on randomly, user presses an analog input sensor corresponding to the LED light closest to it and I log that data to a CSV file on an SD card.

At the same time I'm running a routine where I'm monitoring the motors and running them through a routine.

I'm having a hard time figuring out how to run through a loop that monitors the sensors and records data and another loop that runs the motors without it all being in a single function and since Arduino doesn't do threading does anyone have any suggestions?

Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7198
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you use the timer1 library to make your motor etc. monitoring run on a fixed cycle and use the main cycle to run the rest?
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like timer1 is not fully supported on the mega. I'm not sure what limitations that will cause. I'll do more research on it.

Maybe I can try timer3. I'll play with it and report back. Thanks.
Logged

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

Quote
I'm having a hard time figuring out how to run through a loop that monitors the sensors and records data and another loop that runs the motors without it all being in a single function and since Arduino doesn't do threading does anyone have any suggestions?
Just the usual one when one posts on the Programming forum. Post your code!
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7198
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like timer1 is not fully supported on the mega. I'm not sure what limitations that will cause. I'll do more research on it.

Maybe I can try timer3. I'll play with it and report back. Thanks.

I used timer1 library on mega before, only slightly. Where did it say it won't support mega fully?
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Under timer3. http://arduino.cc/playground/Code/Timer1
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is easier if you use classes.  For example, in a program I'm writing now, I've got to poll sensors and an RTC and then make decisions based upon that -- several of these decision objects (called outletObjects in my code) are timers and other things that work asynchronously.  You instantiate the classes as global variables and then link them all to the data object by passing them its address during setup.  In each execution of the main loop, you poll the sensors, record the data to the globalDataObject, and then tell each OutletObject to look at that data and do their thing, one by one.

Basically, it looks something like this:

Code:
globalDataObject; 
poller;
OutletObjectA;
OutletObjectB;
OutletObjectC;

setup()
{
  poller.setDataSource(&globalDataObject);
  OutletObjectA.setDataSource(&globalDataObject);
  OutletObjectB.setDataSource(&globalDataObject);
  OUtletObjectC.setDataSource(&globalDataObject);
}

loop()
{
  poller.pollsensors(); // Looks at all the sensors and records the data to globalDataObject;
  OutletObjectA.tick(); // Tells OutletObject A to do its thing -- it looks at the globalDataObject and, depending on the type of outletObject, does stuff based on that data
  OutletObjectB.tick();
  OutletObjectC.tick();
}



If you encapsulate each function as its own object like this, you get a kind of ghetto multithreading.  If you take care to avoid using things like delay() and extensive looping inside the decision objects, it works pretty well.
Logged

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

Code:
globalDataObject; 
poller;
OutletObjectA;
OutletObjectB;
OutletObjectC;
What are these? They are not creating instances of objects.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm being lazy. smiley  I didn't bother to make up a class names when instantiating 'em, this was just pseudocode for an example.
Logged

Firenze, Italy
Offline Offline
Full Member
***
Karma: 0
Posts: 104
Dislessia portami via
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Let us know if the object oriented approach will work. I'm interessed in Arduino threading too.
Logged

The brightest flame burns quickest

Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm being lazy. smiley  I didn't bother to make up a class names when instantiating 'em, this was just pseudocode for an example.

I'm not that familiar with classes, how would you go about doing it?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Somebody recently advertised a library which lets you declare timers to invoke callbacks at intervals you specify. It doesn't do anything you can't code yourself just polling all your functions at whatever interval you want, but if you're struggling with that then this library might save you a lot of hassle. I'm too lazy to go searching back for the post describing it, but I'm sure you can find it yourself if you do a search.
Logged

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

Pages: [1]   Go Up
Jump to: