[VicPiMakers Projects] Make class out of functions

Mark G. vpm at palaceofretention.ca
Mon Nov 16 23:40:13 EST 2020


Hi George,

I've managed get two lights blinking at different rates
on an Arduino Uno.  I changed the timer test algorithm
to something I found on the internet, which made more
sense to me.

The code is in three separate files.  They are all in the
same folder for arduino purposes.  I may try and make a
library in the arduino sense, that can be used in other
projects (as you desired).  Should I see if I can?

Fixes: I added an #include <Arduino.h> statement into action_timer.h
to solve the missing millis() problem.  Here is the example
in three files.  I am using Arduino 1.8.5 on a macos 10.14
system.

Note: you didn't need to add the parameter in the is_action_time()
member function.  It gets the period via the constructor
in the call to new (also changed from previous version).


On 2020-11-15 9:37 p.m., George Bowden wrote:
> Here is my functions version after fixing a few bugs.  Still misses some 
> blinks.
> On Sun, Nov 15, 2020 at 8:09 PM Mark G. <vpm at palaceofretention.ca 
> <mailto:vpm at palaceofretention.ca>> wrote:
> 
>     Quick update:
> 
>     You probably want to add unsigned in front of
>     all the long ints, or adjust the type as needed
>     (all over, including  the "const" values).
> 
> 
> 
-------------- next part --------------

/* Definitions for an action timer class.  
 * Mark G. 2020-11-15
 */

#include "action_timer.h"

bool ActionTimer::is_action_time() 
{
  bool status = false;
  
  unsigned long this_millis = millis();

  if((this_millis - last_action_millis) > period) {
    last_action_millis = this_millis;
    Serial.print("this_millis=");
    Serial.println(this_millis);
    status = true;
  }
  else {
    status = false;
  }
  return status;
}

-------------- next part --------------
#ifndef ACTION_TIMER_H
#define ACTION_TIMER_H

#include <Arduino.h>

/* Declarations for action timer class.  
 * Mark G. 2020-11-15
 */

const unsigned long int default_millis = 1000; // 1 sec

class ActionTimer {
  public:

    ActionTimer() { period = default_millis; }
    ActionTimer(unsigned long int period_millis) { period = period_millis; }

    bool is_action_time();

    void set_period(unsigned long int p) { period = p; };
    unsigned long int get_period() { return period; }

  private:
    unsigned long int period = 0;  // milliseconds
    unsigned long int last_action_millis = 0;

};

#endif
-------------- next part --------------

#include <string.h>
#include <stdint.h>

#include "action_timer.h"

char cstr[20];
String mytemp=" ";
int adcValue=0;
int avgDenominator = 100;

int ledState2 = 0;
int led12 = 12;
int ledState12 = 0;

const unsigned long int milliseconds_per_minute = 60000;
unsigned long int post_period_minutes = 5; 
unsigned long int post_period = post_period_minutes * milliseconds_per_minute;

unsigned long int blink_period = 500;  // milliseconds

ActionTimer* poster;
ActionTimer* blinker;

void setup() {
  // put your setup code here, to run once:
  
  post_period = 2000;

  poster = new ActionTimer(post_period);
  blinker = new ActionTimer(blink_period);
 
  pinMode(LED_BUILTIN, OUTPUT); 
  pinMode(led12, OUTPUT);
  digitalWrite(led12,0);

  Serial.begin(115200);
  Serial.println(" "); 
  Serial.println("codefile=action_timer");
  //Serial connection
}
void loop() {

  if (poster->is_action_time()) {
    ledState12=(ledState12+1) % 2;
    Serial.print("post millis=");
    Serial.println(millis());
    digitalWrite(led12, ledState12);
  }
  if (blinker->is_action_time()) {
    ledState2=(ledState2+1) % 2;
    Serial.print("blink millis=");
    Serial.println(millis());
    digitalWrite(LED_BUILTIN, ledState2);
  }
}




More information about the Projects mailing list