GY-63_MS5611/libraries/PERIPUMP/PERIPUMP.cpp

123 lines
1.7 KiB
C++
Raw Normal View History

2022-10-14 14:14:47 -04:00
//
// FILE: PERIPUMP.cpp
// AUTHOR: Rob Tillaart
2022-10-15 09:38:12 -04:00
// VERSION: 0.1.1
2022-10-14 14:14:47 -04:00
// DATE: 2022-10-13
// PURPOSE: Arduino library for peristaltic pump
#include "PERIPUMP.h"
// CONSTRUCTOR
PERIPUMP::PERIPUMP(uint8_t pumpPin)
{
_pin = pumpPin;
_percentage = 0;
_sumTime = 0;
}
2022-10-15 09:38:12 -04:00
void PERIPUMP::begin(float percentage)
2022-10-14 14:14:47 -04:00
{
_myServo.attach(_pin);
resetRunTime();
2022-10-15 09:38:12 -04:00
setPercentage(percentage);
2022-10-14 14:14:47 -04:00
}
void PERIPUMP::stop()
{
2022-10-15 09:38:12 -04:00
setPercentage(0);
}
void PERIPUMP::forward()
{
setPercentage(100);
}
void PERIPUMP::backward()
{
setPercentage(-100);
2022-10-14 14:14:47 -04:00
}
2022-10-15 09:38:12 -04:00
// the worker.
2022-10-14 14:14:47 -04:00
void PERIPUMP::setPercentage(float percentage)
{
// weighted runtime ?
// _sumTime += (millis() - _start) * abs(_percentage);
2022-10-15 09:38:12 -04:00
_percentage = constrain(percentage, -100.0, 100.0);
uint32_t now = millis();
2022-10-14 14:14:47 -04:00
uint16_t ms = 0;
if (_percentage == 0)
{
2022-10-15 09:38:12 -04:00
if (_start != 0) _sumTime += (now - _start);
// middle position is stop
ms = 0;
_start = 0;
2022-10-14 14:14:47 -04:00
}
else if (_percentage > 0)
{
2022-10-15 09:38:12 -04:00
ms = 500 + 5 * _percentage;
if (_start == 0) _start = now;
2022-10-14 14:14:47 -04:00
}
else if (_percentage < 0)
{
2022-10-15 09:38:12 -04:00
ms = -500 + 5 * _percentage;
if (_start == 0) _start = now;
2022-10-14 14:14:47 -04:00
}
2022-10-15 09:38:12 -04:00
if (_invert) _myServo.writeMicroseconds(1500 - ms);
else _myServo.writeMicroseconds(1500 + ms);
2022-10-14 14:14:47 -04:00
}
float PERIPUMP::getPercentage()
{
return _percentage;
}
2022-10-15 09:38:12 -04:00
void PERIPUMP::setInvert(bool flag)
{
_invert = flag;
}
bool PERIPUMP::getInvert()
{
return _invert;
}
2022-10-14 14:14:47 -04:00
//////////////////////////////////////////////////////
//
// DURATION
//
float PERIPUMP::getRunTime()
{
float seconds = _sumTime;
if (_start != 0) seconds += (millis() - _start);
return seconds * 0.001;
}
float PERIPUMP::resetRunTime()
{
float s = getRunTime();
_sumTime = 0;
_start = 0;
return s;
}
// -- END OF FILE --