I will be maintaining my libraries here:
http://bit.ly/pATDBi

I am the lead developer for libraries 
that ship with the Wiring distribution. 
As per version 1.0 -
Wiring will support Arduino boards.
You are welcome to check it out!
http://wiring.org.co/download/
TimedAction Library for Arduino
Author:  Alexander Brevig
Contact: alexanderbrevig@gmail.com


Navigation


Current version

1.5 2009-10-25: Added setInterval()


History

1.5 2009-10-25: Added setInterval()
1.4 2009-05-06: Added reset()
1.3 2009-04-16: Added disable() and enable(), requested by: ryno
1.2 2009-04-13: Added a constructor
1.1 2009-04-08: Added an example that demonstrates three arduino examples at once
1.0 2009-03-23: Initial Release


Description

TimedAction is a library for the Arduino.

It is created to help hide the mechanics of how to implement Protothreading and general millis() timing. It is sutied for those actions that needs to happen approximately every x milliseconds.


Download, install and import

Download here: Attach:TimedAction-1_6.zip

Put the TimedAction folder in "hardware\libraries\".

In the Arduino IDE, create a new sketch (or open one) and

select from the menubar "Sketch->Import Library->TimedAction".
Once the library is imported, an "#incude <TimedAction.h>" line will appear at the top of your sketch.


Creation

TimedAction(unsigned int interval,void (*function)())
TimedAction(unsigned long preDelay,unsigned int interval,void (*function)())

TimedAction timedAction = TimedAction(1000,blink);

Instanciates a TimedAction object that will trigger void blink() every second, if checked.

Note: The preDelay might be useful if you want to make sure that you have a connection to, for instance an network server, before the TimedAction starts triggering.


Methods

void check()

Will execute the function it is refered to. In this case, the void blink(), if interval has been met

void enable()

Will enable "thread" execution

void disable()

Will disable "thread" execution

void reset()

Will reset the time relative to the TimedAction

void setInterval( unsigned int newInterval )

Change the interval of this TimedAction


Example

BlinkAction

  1. #include <TimedAction.h>
  2.  
  3. //this initializes a TimedAction class that will change the state of an LED every second.
  4. TimedAction timedAction = TimedAction(1000,blink);
  5.  
  6. //pin / state variables
  7. #define ledPin 13
  8. boolean ledState = false;
  9.  
  10.  
  11. void setup(){
  12.   pinMode(ledPin,OUTPUT);
  13.   digitalWrite(ledPin,ledState);
  14. }
  15.  
  16. void loop(){
  17.   timedAction.check();
  18. }
  19.  
  20. void blink(){
  21.   ledState ? ledState=false : ledState=true;
  22.   digitalWrite(ledPin,ledState);
  23. }
  24.  
  25.  

ThreeExamplesAtOnce

  1. /*
  2. ||
  3. || @file ThreeExamplesAtOnce.pde
  4. || @version 1.0
  5. || @author Alexander Brevig
  6. || @contact alexanderbrevig@gmail.com
  7. ||
  8. || @description
  9. || | This sketch blinks an LED as Blink
  10. || |             sets a led on or off according to serial buffer as PhysicalPixel
  11. || |             prints the ascii table as ASCIITable
  12. || #
  13. ||
  14. */
  15.  
  16. #include <TimedAction.h>
  17.  
  18. //this initializes a TimedAction object that will change the state of an LED every second.
  19. TimedAction blinkAction                 =       TimedAction(1000,blink);
  20. //this initializes a TimedAction object that will change the state of an LED
  21. //according to the serial buffer contents, every 50 milliseconds
  22. TimedAction physicalPixelAction =       TimedAction(50,physicalPixel);
  23. //this initializes a TimedAction object that will write tha ascii table to the serial every ten seconds
  24. TimedAction asciiTableAction    =       TimedAction(10000,asciiTable);
  25.  
  26. //pin / state variables
  27. #define ledPin 13
  28. #define physicalPin 12
  29. boolean ledState = false;
  30.  
  31.  
  32. void setup(){
  33.   pinMode(ledPin,OUTPUT);
  34.   digitalWrite(ledPin,ledState);
  35.   pinMode(physicalPin, OUTPUT);
  36.   Serial.begin(9600);
  37. }
  38.  
  39. void loop(){
  40.   blinkAction.check(); //trigger every second
  41.   physicalPixelAction.check(); //trigger every 50 millisecond
  42.   asciiTableAction.check(); //trigger every 10 second
  43. }
  44.  
  45. //[url=http://arduino.cc/en/Tutorial/Blink]Examples->Digital->Blink[/url]
  46. void blink(){
  47.   ledState ? ledState=false : ledState=true;
  48.   digitalWrite(ledPin,ledState);
  49. }
  50.  
  51. //[url=http://arduino.cc/en/Tutorial/PhysicalPixel]Examples->Digital->PhysicalPixel[/url]
  52. void physicalPixel()
  53. {
  54.   if (Serial.available()) {
  55.     byte val = Serial.read();
  56.     if (val == 'H') {
  57.       digitalWrite(physicalPin, HIGH);
  58.     }
  59.     if (val == 'L') {
  60.       digitalWrite(physicalPin, LOW);
  61.     }
  62.   }
  63. }
  64.  
  65. //[url=http://arduino.cc/en/Tutorial/ASCIITable]Examples->Digital->ASCIITable[/url]
  66. void asciiTable()
  67. {
  68.   byte number = 33; // first visible character '!' is #33
  69.   // print until we have printed last visible character '~' #126 ...
  70.   while(number <= 126) {
  71.     Serial.print(number, BYTE);    // prints value unaltered, first will be '!'
  72.  
  73.     Serial.print(", dec: ");
  74.     Serial.print(number);          // prints value as string in decimal (base 10)
  75.     // Serial.print(number, DEC);  // this also works
  76.  
  77.     Serial.print(", hex: ");
  78.     Serial.print(number, HEX);     // prints value as string in hexadecimal (base 16)
  79.  
  80.     Serial.print(", oct: ");
  81.     Serial.print(number, OCT);     // prints value as string in octal (base 8)
  82.  
  83.     Serial.print(", bin: ");
  84.     Serial.println(number, BIN);   // prints value as string in binary (base 2)
  85.                                                                          // also prints ending line break
  86.     number++; // to the next character
  87.   }
  88.   asciiTableAction.disable();
  89. }
  90.  


FAQ

How can I use multiple TimedActions?

TimedAction is a class. Therefore to make multiple TimedActions you must create an instance for each of them. In the example BlinkAction above, a TimedAction instance (TimedAction timedAction) is bound to the void blink() function, and set to trigger every second, if checked.

TimedAction timedAction = TimedAction(1000,blink);

To add a TimedAction to an additional function (void updateLcd() or instance) and set to trigger every 200 millisecond, you could create the following instance timedAction2:

TimedAction timedAction2 = TimedAction(NO_PREDELAY,200,updateLcd);

And now it's just to keep updating those two instances:

//update instances and possibly fire funcitons
  timedAction1.check();
  timedAction2.check();

See ThreeExamplesAtOnce for an example on how to use multiple TimedActions.

How can I use long intervals?

For timers longer than about a minute, you will need to change the type of all variable and parameter definitions for intervl or interval from unsigned int to unsigned long (2 in the .cpp and 3 in the .h).


Information about this page

Part of AlphaBeta Libraries.
Last Modified: July 27, 2011, at 12:07 PM
By: AlphaBeta

Share