I am writing a library to operate a linear actuator, let's call the library ActuatorV2. It is composed of two files, a .h and and .cpp files. To create an object, I instantiate the object as follows
Actuator act(pulsePin,directionPin,enablePin);
Within the constructor of the object, four tasks are performed:
1) pin assignment,
2) setting pin modes,
3) setting pulse frequency
4) enabling the actuator.
When performing the fourth task (enabling), I simply set the enablePin to HIGH as follows digitalWrite(enablePin,HIGH); There is absolutely nothing special here, but for some odd reason, when setup() is called in the sketch, the enablePin is set back to LOW. I know this because I have an LED light connected to the enablePin, and I visually monitor it during the operation.
Any idea why this happens?
I am aware that in the documentation of setup() here, it is clearly stated that pinMode() should be placed in setup(). But I have used the approach shown in the MWE before a few months ago, and it was working just fine.
Hardware used
MacBook Pro
Arduino Due
MWE
Sketch
#include <ActuatorV2.h>
// defining actuator pins and properties
const int pulstPin = 4;
const int directionPin = 5;
const int enablePin = 6;
const int pulseFrequency= 500;
// initializing the actuator
Actuator act(pulstPin, directionPin, enablePin, pulseFrequency);
void setup() {
// do nothing
}
void loop() {
// do nothing
}
Library (Actuator.h, Actuator.cpp)
Actuator.h
#ifndef Actuator_h
#define Actuator_h
#include<Arduino.h>
class Actuator{
private:
// private variables
// pins
int _pulsePin;
int _directionPin;
int _enablePin;
// pulse frequency parameters
int _pulseFrequency;
int _pulseDelay = 5; // 2.5 microseconds manufacturer recommendation
int _interpulseDelay;
// private methods
void assignPins(int pulsePin, int directionPin, int enablePin);
void setPinModes();
public:
// constructors
Actuator(int pulsePin, int directionPin, int enablePin, int pulseFrequency);
// public methods
void setPulseFrequencyParameters(int freq);
void enable();
};
#endif
Actuator.cpp
# include<ActuatorV2.h>
# include<Arduino.h>
Actuator::Actuator(int pulsePin, int directionPin, int enablePin, int pulseFrequency){
// 1) pin assignment
assignPins(pulsePin,directionPin,enablePin);
// 2) setting pin modes
setPinModes();
// 3) setting pulse frequency
setPulseFrequencyParameters(pulseFrequency);
// 4) enable actuator
enable();
}
void Actuator::assignPins(int pulsePin, int directionPin, int enablePin){
_pulsePin = pulsePin;
_directionPin = directionPin;
_enablePin = enablePin;
}
void Actuator::setPinModes(){
pinMode(_pulsePin,OUTPUT);
pinMode(_directionPin,OUTPUT);
pinMode(_enablePin,OUTPUT);
}
void Actuator::setPulseFrequencyParameters(int pulseFrequency){
int _pulseFrequency = pulseFrequency;
int minPeriod = 10; // 10 micro-seconds which is 4 X 2.5 (manufacurer recommended pulse width for reliable operation)
/*
---------------------------------------------------------------------
Pulse shape
3.3 V
| |``````````| |``````````|
| | | | |
|__| |_____________________| |__________ time
0 V
\__________/\____________________/
pd ipd
\________________________________/
one pulse
pd: pulse delay. Minimum 3 micro seconds
ipd: inter-pulse delay. Minimum 3 micro seconds
---------------------------------------------------------------------
*/
float period = 1.0/((float) pulseFrequency);
if (period < minPeriod){
_pulseDelay = 5;
_interpulseDelay = 5;
} else {
_pulseDelay = 5;
_interpulseDelay = period - _pulseDelay;
}
}
void Actuator::enable(){
digitalWrite(_enablePin,HIGH);
delay(500); // delay to observe the result if effective.
}