[VicPiMakers Projects] Make class out of functions
Mark G.
vpm at palaceofretention.ca
Sun Nov 15 19:13:46 PST 2020
Hi George,
Here's a quick outline of an ActionTimer class. What
do you make of it?
Mark
On 2020-11-15 6:07 p.m., George Bowden wrote:
> I have two "identical" functions for time slicing the Arduino loop. One
> function is called to see if it is time to toggle the LED every second,
> and the other "identical" function is called to see if it is time to
> send a post to the website every few minutes. These functions seem good
> candidates for a single C++ class, and I'm looking for some
> advice, never having coded a class before. Maybe it's a coding
> challenge? I'm using Arduinos millis() which increments continuously and
> the modulus of that, with a different period value passed to the
> functions, to determine when to act. When the modulus remainder is
> smaller than the last time through the loop, it is time to take action.
> (the vertical line in the stairs)
>
> image.png
>
>
>
> Each instance of the class needs its own static variable for the value
> of mod'ed millis() last time through the loop. I would like to write a
> class which I instantiate with NEW for each different period, .... I
> would like to put these in a library that I could include in any of my
> projects that need time sliced
>
> Here is how I have coded it as functions:
>
> unsigned long lastPostMillis=0; // the value of the modulus last time
> through the loop.
> unsigned long lastBlinkMillis=0;
>
> bool isPostTime(unsigned long int period){
> if(millis() % period > lastPostMillis) {
> lastPostMillis = millis()% period;
> return true;
> }
> else {
> lastPostMillis = millis() % period;
> return false;
> }
> }
> bool isBlinkTime(unsigned long int period){
> int blinkPeriod = 500;
> if(millis() % blinkPeriod > lastBlinkMillis) {
> lastBlinkMillis = millis()% period;
> return true;
> }
> else {
> lastBlinkMillis = millis()% period;
> return false;
> }
> }
> void setup() {...
> }
> void loop() {
> if (isPostTime(minutesBetweenPosts*60000)) { //time to send a Post to
> the website
> ...
> if(isBlinkTime(500)){
> toggle builtin led}
> } // end loop
> --
> George Bowden, 250-893-7423
> gtbowdeng at gmail.com
> <mailto:gtbowdeng at gmail.com>
>
-------------- next part --------------
#include "action_timer.h"
const long int milliseconds_per_minute = 60000;
const long int post_period_minutes = 5;
const long int post_period = post_period_minutes * milliseconds_per_minute;
const long int blink_period = 500; // milliseconds
ActionTimer poster(post_period);
ActionTimer blinker(blink_period);
void setup() {...
}
void loop() {
if (blinker.is_action_time()) {
toggle built in led;
}
if (poster.is_action_time()) {
post action;
}
} // end loop
-------------- next part --------------
#ifndef ACTION_TIMER_H
#define ACTION_TIMER_H
/* Declarations for action timer class.
* Mark G. 2020-11-15
*/
const long int default_millis = 1000; // 1 sec
class ActionTimer {
public:
ActionTimer() { period = default_millis; }
ActionTimer(long int period_millis) { period = period_millis; }
bool is_action_time();
void set_period(long int p) { period = p; };
long int get_period() { return period; }
private:
long int period = 0; // milliseconds
long int last_action_millis = 0;
};
#endif
-------------- next part --------------
/* Definitions for an action timer class.
* Mark G. 2020-11-15
*/
#include "action_timer.h"
bool ActionTimer::is_action_time()
{
if(millis() % period > last_action_millis) {
last_action_millis = millis() % period;
return true;
}
else {
last_action_millis = millis() % period;
return false;
}
}
More information about the Projects
mailing list