I’ve been wrestling with this for days. The jumping off point is this thread. I figured the next logical step after getting the class running was to put it into a library.
I’ve read the tutorials. I’ve watched YT videos. I’ve read the technical definitions of .h/.cpp files (got quickly lost in arcana). I’m missing it.
The main file:
#include <Bounce2.h>
#include <PLCtimer.h>
#define BUTTON_PIN 8 // to enable the timer
#define LED_PIN 13
#define RESET_PIN 9
// Instantiate a Bounce object
Bounce debouncer = Bounce();
// create a class called 'PLCtimer'
//---------------
// Make and define timer object(s)
//-----------------
//
// Create timer with a preset value and timer type
PLCtimer activityTimer(2000UL, TON);
void setup() {
// Configure pushbuttons with an internal pull-up :
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RESET_PIN, INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer.attach(BUTTON_PIN);
debouncer.interval(5); // interval in ms
//Setup the LED :
pinMode(LED_PIN, OUTPUT);
//
Serial.begin(230400); // open serial monitor comms.
}
void loop() {
static int count;
activityTimer.update(); // update timer status
if (activityTimer.getOSRise()) {
count++;
Serial.println(count);
}
activityTimer.setEN( digitalRead(BUTTON_PIN));
activityTimer.setRes( !digitalRead(RESET_PIN));
if (activityTimer.getDn())
digitalWrite(LED_PIN, HIGH); // tell when timer is done
else digitalWrite(LED_PIN, LOW); // tell when timer is not done
} //end of loop
The .cpp file:
#include "Arduino.h"
#include "PLCtimer.h"
// create a class called 'PLCtimer'
PLCtimer::PLCtimer {
public:
/*
===== Access functions for timer status bits
*/
void setEN(bool en) {
_Enable = en;
}
byte getEN() {
return _Enable;
}
bool getres() {
return _Reset;
}
void setres(bool res) {
_Reset = res;
}
bool getDn() {
return _Done;
}
bool getTt() {
return _TimerRunning;
}
bool getIntv() {
return _TimerRunning;
}
bool getOSRise() {
return _OSRise;
}
bool getOSFall() {
return _OSFall;
}
unsigned long currentMillis;
// Function to operate timers created under PLCtimer
// ----------------------------------------
// Update timer accumulated value & condition
// flag bits 'tt' (timer timing) and 'dn' (done) based
// on 'res', 'en', and 'type' variables.
// Function returns boolean status of done bit, 'dn'
// ===========================
boolean update() {
currentMillis = millis();
if (_Enable) { // timer is enabled to run
_TimeDelta = currentMillis - _LastMillis;
_LastMillis = currentMillis;
_Accumulator = _Accumulator + _TimeDelta;
if (_Accumulator >= _Preset) { // timer done?
_Accumulator = _Preset;
_Done = true;
}
}
else { // timer not enabled
_LastMillis = currentMillis;
}
/*
----- Time to reset?
*/
if (_Reset == true or (!_Enable and !_Type == RTO)
or (_Type == OSGEN and _OSRise)) {
_Done = false; // show timer as not done
_Accumulator = 0; // reset accumulated value to zero
}
/*
----- Generate a positive going one-shot pulse on timer done 0-1 transition
*/
_OSRise = (_Done and _OSRSetup);
_OSRSetup = !_Done;
/*
----- and another positive going one-shot pulse on timer done 1-0 transition
*/
_OSFall = (!_Done and _OSFSetup);
_OSFSetup = _Done;
/*
----- condition the timer timing bit
*/
if (_Enable and !_Done and !_Reset) {//experiment to simplify tt logic
_TimerRunning = true;
}
else _TimerRunning = false;
/*
if (_Reset) _TimerRunning = false;
if (!_Enable) _TimerRunning = false;
if (_Enable and _Done) _TimerRunning = false;
*/
return _Done;
}; // end of update Timer operation
private:
unsigned long _Preset;
unsigned long _Accumulator = 0;
unsigned long _LastMillis = 0;
byte _Done: 1;
byte _TimerRunning: 1;
byte _OSRise: 1;
byte _OSFall: 1;
byte _Type: 2; // bit fields for timer status
byte _OSRSetup: 1;
bool _OSFSetup: 1;
bool _Enable: 1;
byte _Reset: 1;
unsigned long _TimeDelta;
/*
User-added error handler from pert @ Arduino.cc, post #13
thread http://forum.arduino.cc/index.php?topic=559655.new#new
*/
void negativeError( void ) __attribute__((error("Negative timer preset not allowed!")));
void negativePresetCheck(int number) {
if (number < 0) {
negativeError();
}
}
}; // end of class PLC timer
The .h file:
// header file
#ifndef PLCTIMER_H
#define PLCTIMER_H
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define TON 0 // accumulates time only when enabled, reset otherwise
/* The TON mode will also function as an interval timer
by accessing the tt bit instead of the dn bit.
*/
#define RTO 1 // accumulates time when enabled. Value is retained
/* when enable is false. */
#define OSGEN 2 // self-resetting one-shot generator - when enabled
/* outputs a pulse and resets automatically after */
/* reaching preset
*/ /*
All types:
> produce a positive-going one-scan pulse 'os' upon reaching
preset value.
> respond to a reset command by setting accumulated value to
zero and resetting the done and tt bits.
> set the done bit upon reaching preset.
*/
class PLCtimer {
public:
// constructor - permits setting of variables upon instantiation
// PLCtimer(unsigned long , byte type)
// {
// _Preset = pre, _Type = type;
// negativePresetCheck(pre); // preset validation
// };
// error message
void negativeError() __attribute__(());
void negativePresetCheck(int);
/*
===== Access methods for timer status bits
*/
void setEN(bool );
bool getEN();
void setRes(bool);
bool getDn();
bool getTt();
bool getIntv();
bool getOSRise();
bool getOSFall();
bool update();
private:
unsigned long _Preset;
unsigned long _Accumulator;
unsigned long _LastMillis;
bool _Type: 1; // bit fields for timer status
bool _Done;
bool _Enabled;
bool _TimerRunning;
bool _Reset;
bool _OSRise;
bool _OSFall;
bool _OSRSetup;
bool _OSFSetup;
unsigned long _TimeDelta;
};
#endif
The error message:
PLCtimer.cpp:6: error: ‘PLCtimer::PLCtimer’ names the constructor, not the type
PLCtimer::PLCtimer {
^
I commented out the preset validation to reduce possible complications. Don’t know if it helped.
Are the #defines in the right place?
What am I doing wrong?
Thanks.