"Ticks" Library

dimanche 13 octobre 2013
par  Finizi
popularité : 27%

A library for counting impulses and calculate speeds or frequencies.


 Presentation of the "Ticks" Library

The ’Ticks’ Library for Arduino allow to manage sensors like anemometers or flowmeters.
This kind of device regularly sends impulses, not necessarily very frequent, and counts on a certain period of time to deduce the frequency, then a wind speed or a water flow.


 "Ticks" Library contents

Like all Arduino libraries, the "Ticks" library consists of a header file (Ticks.h) and a code file (Ticks.cpp), all in the "Ticks" folder.

.

Constructor

Ticks myTicks(numInterrupt, pinFlowMeter, period);

The constructor provides access to the functions of the "Ticks" library. Constructor needs the 3 following parameters :

  • numInterrupt  : This is the interrupt number to use. On Arduino Uno, there is the choice between 0 (attached to pin 2) or 1 (attached to pin 3).
  • pinTicks  : The PIN number to use. With Arduino Uno, two pins are available : Pin 2 for interrupt n° 0 and pin 3 for interrupt n° 1. With Arduino Mega 2560, there are 4 more interrupts : 2 (pin 21), 3 (pin 20), 4 (pin 19) & 5 (pin 18).
  • period  : Base period, in milliseconds. If value is 1000, it will be possible to have, in addition to the instantaneous frequency, the average frequencies for 1, 5 and 25 seconds. If value is 2000, the average frequencies for 2, 10 and 50 seconds will be available.

.

The functions of the library

The "Ticks" library exposes the following functions :

  • (void) begin()  : To initialize the class Ticks, begin to count the Ticks in order to calculate the frequency.
  • (void) operate()  : For each iteration of loop(), allows to update frequency, and if the conditions are met, the averages frequencies.
  • (int) currentTicks()  : Returns the number of Ticks counted since the beginning. At the end of 65535 ticks, the counter restarts at 0.
  • (float) instantTickRate()  : Gives the instantaneous frequency calculated between each call to the operate()function. If each iteration of the loop() function takes 100 ms, this frequency will be calculated over the period. It must be understood that over a short period, it can not be any tick, which will give a frequency to 0. Similarly, a tick detected in this time will give a frequency of 10 Hz.
  • (float) TickRate1Period()  : Gives the frequency (Ticks per second) or Hz measured on the last periods.
  • (float) TickRate5Period()  : Gives the frequency (Ticks per second) or Hz measured over the last 5 periods.
  • (float) TickRate25Period()  : Gives the frequency (Ticks per second) or Hz measured over the last 25 periods per block of 5 periods.

.

Download and installation

The latest version of the library is available here. Extract the archive and place the "Ticks" directory in the "Arduino/libraries/" directory.
Load Arduino software. The library should now be present in the menu "/ skecth/Import Library /".

The code is also available on GitHub.


 Example of use

The example of use deals the recovery of data from a basic flowmeter, in this case the YF-S201. It then displays :

  • the instant number of ticks per second, here measured on a half a second (delay(500) in the loop).
  • the average number of ticks per second to 1 second
  • the average number of ticks per second for 5 seconds
  • the average number of ticks per second on 25 seconds
  • the average flow on 25 seconds in liter per minute

And here is the code for the example distributed with the library :

  1.  
  2. /*
  3. **
  4. ** FlowMeter
  5. **
  6. ** FlowMeter read sample
  7. **
  8. ** http://www.domoenergytics.com
  9. ** This example code is in the public domain.
  10. */
  11.  
  12. #include "Ticks.h"
  13.  
  14. const byte pinFlowMeter = 2;    // data pin for FlowMeter sensor
  15. const byte numInterrupt = 0;    // Interrupt number
  16. const int period = 1000;        // Base periode, in milliseconds
  17.  
  18. const float CoefLitMin = 7.4;
  19.  
  20. // Instantiate the class for counting ticks
  21. Ticks myTicks(numInterrupt, pinFlowMeter, period);
  22.  
  23. void setup() {
  24.   Serial.begin(115200);    // Initializes serial communication with PC terminal
  25.   myTicks.begin();         // Initializes the class for counting ticks
  26. }
  27.  
  28. <doc21>
  29. void loop() {
  30.  
  31.   myTicks.operate();       // Count
  32.  
  33.   Serial.print("t : ");
  34.   Serial.print(myTicks.instantTickRate());
  35.   Serial.print("  1s : ");
  36.   Serial.print(myTicks.TickRate1Period());
  37.   Serial.print("  5S : ");
  38.   Serial.print(myTicks.TickRate5Period());
  39.   Serial.print("  25s : ");
  40.   Serial.print(myTicks.TickRate25Period());
  41.   Serial.print(" ==> ");
  42.   Serial.print(CoefLitMin * myTicks.TickRate25Period(), 2);
  43.   Serial.print(" liter/minute");
  44.   Serial.println(" !");
  45.  
  46.   delay(500);
  47. }
PNG - 53.4 ko