Pages: [1]   Go Down
Author Topic: Library for generating pulsepatterns  (Read 1377 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Today I posted an experimental class called PulsePattern on the playground for generating [HIGH LOW] pulse patterns with the help of Timer1.
- http://playground.arduino.cc//Main/PulsePattern -

The idea is to have an array of [LOW|HIGH] periods that is used by Timer1 as alarm trigger one by one. When alarm goes of a predefined pin flips from HIGH to LOW or vice versa. Timer1 is encapsulated by an instance of the class called PPGenerator, PP = PulsePattern of course. When all elements of the array are used it restarts at the begin of the array. The array can contain as many elements as you like (restricted by free memory of course)

Elaborated patterns can be placed into a pattern array and "played back" by the timer. PatternPlayer might be a better name? One can change the contents of the array runtime or reinitialize the PPGenerator with another array. A fun feature is using an array with an odd number of entries (try it).

Not implemented
Placing the pattern in an array is fast, but I am thinking of a callback function that returns the next period to the timer. It is much slower due to function call overhead but it can encapsulate the pattern array or calculate the next period e.g. based upon sensor values. e.g. increase frequency or duty cycle when dew point temperature drops.


The sample sketch explains how it can be used.
Code:
//
//    FILE: SOS_demo2.pde
//  AUTHOR: Rob Tillaart
//    DATE: 2012-11-23
//
// PUPROSE: demo of the PulsePattern Library
//          uses timer1
//

#include "PulsePattern.h"

uint16_t SOSpattern[] = { 
  500,500,500,500,500,1500,       // S in morse
  1500,500,1500,500,1500,1500,   // O in morse   
  500,500,500,500,500,1500 };    // S in morse

uint8_t patternSize = 18;
uint8_t startLevel = LOW;

void setup()
{
  Serial.begin(9600);
  Serial.println("Start PulsePattern");

  // as the prescaler = 1024 the periods of the pattern are a
  // few percent less than a millisecond
  PPGenerator.init(13, SOSpattern, patternSize, startLevel, PRESCALE_1024);
  PPGenerator.start();
}

void loop()
{
  // dummy code that does something in the foreground
  Serial.println(millis());
  delay(1000);
}

As always, ideas, comments and remarks are welcome!


Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 129
Posts: 8602
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

uint8_t patternSize = 18;

You could use the old sizeof/sizeof trick here.

PRESCALE_1024

I've done something similar on an LPC and I use two constants

HWTIMER_COUNT_RATE_US
HWTIMER_COUNT_RATE_MS

So the user just has to know if he is counting uS or mS and doesn't have to think about what the prescaler is doing.

______
Rob

Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
uint8_t patternSize = 18;

You could use the old sizeof/sizeof trick here.

True, would be more elegant.
however for a next version I am thinking of a change that uses a begin and end index, so the user can select a part of the whole array.
(Now the user can for instance say size=6 and only blink the letter S.)

Thanks,
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: