added json, more pump code, bugs
This commit is contained in:
+156
-30
@@ -1,74 +1,62 @@
|
||||
#include "pump.h"
|
||||
|
||||
Pump::Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin, uint8_t errorLedPin) {
|
||||
Pump::Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin, uint8_t ledPin) {
|
||||
this->pumpPin = pumpPin;
|
||||
this->pressurePin = pressurePin;
|
||||
this->flowPin = flowPin;
|
||||
this->errorLedPin = errorLedPin;
|
||||
this->ledPin = ledPin;
|
||||
|
||||
pinMode(this->pumpPin, OUTPUT);
|
||||
pinMode(this->errorLedPin, OUTPUT);
|
||||
pinMode(this->ledPin, OUTPUT);
|
||||
pinMode(this->pressurePin, INPUT);
|
||||
// Hall flow sensor?
|
||||
|
||||
digitalWrite(this->pumpPin, LOW);
|
||||
digitalWrite(this->errorLedPin, LOW);
|
||||
digitalWrite(this->ledPin, LOW);
|
||||
}
|
||||
|
||||
void Pump::loop() {
|
||||
switch (this->state) {
|
||||
case State::Off :
|
||||
|
||||
break;
|
||||
|
||||
case State::On :
|
||||
|
||||
this->timerStandby = false;
|
||||
this->checkSensors();
|
||||
break;
|
||||
|
||||
case State::Timer :
|
||||
|
||||
this->stateTimer();
|
||||
break;
|
||||
|
||||
case State::Standby :
|
||||
|
||||
this->stateStandby();
|
||||
break;
|
||||
|
||||
case State::Error :
|
||||
if (millis() - this->startTime > Pump::blinkTime) {
|
||||
this->startTime = millis();
|
||||
if (this->ledState) {
|
||||
this->ledState = false;
|
||||
digitalWrite(this->errorLedPin, LOW);
|
||||
} else {
|
||||
this->ledState = true;
|
||||
digitalWrite(this->errorLedPin, HIGH);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
this->stateError();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Pump::activate(uint32_t duration) {
|
||||
this->startTime = millis();
|
||||
if (duration) {
|
||||
this->state = State::Timer;
|
||||
this->changeState(State::Timer);
|
||||
this->duration = duration;
|
||||
} else {
|
||||
this->state = State::On;
|
||||
this->changeState(State::On);
|
||||
}
|
||||
}
|
||||
|
||||
void Pump::stop() {
|
||||
if (this->state != State::Error)
|
||||
this->state = State::Off;
|
||||
this->changeState(State::Off);
|
||||
}
|
||||
|
||||
void Pump::clearError() {
|
||||
if (this->state == State::Error)
|
||||
if (this->state == State::Error) {
|
||||
this->state = State::Off;
|
||||
this->changeState(State::Off);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t Pump::getPressure() {
|
||||
@@ -76,13 +64,151 @@ uint16_t Pump::getPressure() {
|
||||
}
|
||||
|
||||
uint32_t Pump::getRunningTime() {
|
||||
return millis() -this->startTime;
|
||||
if (this->state == State::Off)
|
||||
return 0;
|
||||
|
||||
return (millis() - this->startTime) / 1000;
|
||||
}
|
||||
|
||||
uint16_t Pump::getFlow() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Pump::checkSensors() {
|
||||
uint16_t Pump::getStatus() {
|
||||
switch (this->state) {
|
||||
case State::Off :
|
||||
return 0;
|
||||
break;
|
||||
|
||||
case State::On :
|
||||
return 1;
|
||||
break;
|
||||
|
||||
case State::Timer :
|
||||
return 2;
|
||||
break;
|
||||
|
||||
case State::Standby :
|
||||
return 3;
|
||||
break;
|
||||
|
||||
case State::Error :
|
||||
return -1;
|
||||
break;
|
||||
|
||||
default:
|
||||
return -2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool Pump::checkSensors() {
|
||||
if (millis() - this->lastMillisCheckSensor < Pump::delayCheckSensor)
|
||||
return;
|
||||
this->lastMillisCheckSensor = millis();
|
||||
|
||||
}
|
||||
//TODO: read pressure
|
||||
//TODO: read flow
|
||||
|
||||
if (this->flow) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!this->pressure) {
|
||||
this->changeState(State::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pressure > Pump::maxPressure) {
|
||||
if (this->maxPressureStart == 0)
|
||||
this->maxPressureStart = millis();
|
||||
else if (millis() - this->maxPressureStart > Pump::maxMaxPressureTime)
|
||||
this->changeState(State::Standby);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Pump::handlePins(bool state) {
|
||||
digitalWrite(this->pumpPin, state);
|
||||
digitalWrite(this->ledPin, state);
|
||||
if (state) {
|
||||
this->timerStandby = false;
|
||||
this->startTime = millis();
|
||||
this->maxPressureStart = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Pump::changeState(State state) {
|
||||
if (this->state == State::Error)
|
||||
return;
|
||||
|
||||
if (this->state == state)
|
||||
return;
|
||||
|
||||
switch (state) {
|
||||
case State::Off :
|
||||
this->handlePins(false);
|
||||
this->pressure = 0;
|
||||
this->flow = 0;
|
||||
break;
|
||||
|
||||
case State::On :
|
||||
this->handlePins(true);
|
||||
break;
|
||||
|
||||
case State::Timer :
|
||||
this->handlePins(true);
|
||||
break;
|
||||
|
||||
case State::Standby :
|
||||
if (this->state == State::Timer)
|
||||
this->timerStandby = true;
|
||||
this->handlePins(false);
|
||||
break;
|
||||
|
||||
case State::Error :
|
||||
this->handlePins(false);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
this->state = state;
|
||||
}
|
||||
|
||||
void Pump::stateTimer() {
|
||||
if (!this->checkSensors())
|
||||
return;
|
||||
|
||||
if (millis() - this->lastMillisTimer < Pump::delayTimer)
|
||||
return;
|
||||
this->lastMillisTimer = millis();
|
||||
|
||||
uint8_t minutesOn = (millis() - this->startTime) / 1000 / 60;
|
||||
if (minutesOn >= this->duration)
|
||||
this->changeState(State::Off);
|
||||
}
|
||||
|
||||
void Pump::stateStandby() {
|
||||
this->checkSensors();
|
||||
if (this->pressure < Pump::maxPressure / 2) {
|
||||
if (this->timerStandby)
|
||||
this->changeState(State::Timer);
|
||||
else
|
||||
this->changeState(State::On);
|
||||
}
|
||||
}
|
||||
|
||||
void Pump::stateError() {
|
||||
if (millis() - this->startTime > Pump::blinkTime) {
|
||||
this->startTime = millis();
|
||||
if (this->ledState) {
|
||||
this->ledState = false;
|
||||
digitalWrite(this->ledPin, LOW);
|
||||
} else {
|
||||
this->ledState = true;
|
||||
digitalWrite(this->ledPin, HIGH);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+23
-4
@@ -4,7 +4,6 @@
|
||||
|
||||
class Pump {
|
||||
public:
|
||||
static const uint16_t blinkTime = 1000;
|
||||
enum State {
|
||||
Off,
|
||||
On,
|
||||
@@ -13,7 +12,7 @@ class Pump {
|
||||
Error
|
||||
};
|
||||
|
||||
Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin, uint8_t errorLedPin);
|
||||
Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin, uint8_t ledPin);
|
||||
|
||||
void loop();
|
||||
|
||||
@@ -24,20 +23,40 @@ class Pump {
|
||||
uint16_t getPressure();
|
||||
uint32_t getRunningTime();
|
||||
uint16_t getFlow();
|
||||
uint16_t getStatus();
|
||||
|
||||
State getState() { return this->state; }
|
||||
|
||||
private:
|
||||
void checkSensors();
|
||||
static const uint8_t delayCheckSensor = 50;
|
||||
static const uint16_t blinkTime = 1000;
|
||||
static const uint16_t maxPressure = 1000;
|
||||
static const uint16_t maxMaxPressureTime = 30000;
|
||||
static const uint16_t delayTimer = 1000;
|
||||
|
||||
bool checkSensors();
|
||||
void handlePins(bool state);
|
||||
void changeState(State state);
|
||||
void stateTimer();
|
||||
void stateStandby();
|
||||
void stateError();
|
||||
State state = State::Off;
|
||||
|
||||
bool ledState = false;
|
||||
bool timerStandby = false;
|
||||
|
||||
uint8_t pumpPin;
|
||||
uint8_t pressurePin;
|
||||
uint8_t flowPin;
|
||||
uint8_t errorLedPin;
|
||||
uint8_t ledPin;
|
||||
|
||||
uint16_t delayTimer = 1000;
|
||||
uint16_t flow;
|
||||
uint16_t pressure;
|
||||
|
||||
uint32_t startTime;
|
||||
uint32_t duration;
|
||||
uint32_t lastMillisTimer = 0;
|
||||
uint32_t lastMillisCheckSensor = 0;
|
||||
uint32_t maxPressureStart = 0;
|
||||
};
|
||||
Reference in New Issue
Block a user