more improvements
This commit is contained in:
+48
-11
@@ -1,5 +1,10 @@
|
||||
#include "pump.h"
|
||||
|
||||
volatile uint32_t interruptCounter = 0;
|
||||
void flowISR() {
|
||||
interruptCounter++;
|
||||
}
|
||||
|
||||
Pump::Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin, uint8_t ledPin) {
|
||||
this->pumpPin = pumpPin;
|
||||
this->pressurePin = pressurePin;
|
||||
@@ -9,7 +14,8 @@ Pump::Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin, uint8_t ledPin
|
||||
pinMode(this->pumpPin, OUTPUT);
|
||||
pinMode(this->ledPin, OUTPUT);
|
||||
pinMode(this->pressurePin, INPUT);
|
||||
// Hall flow sensor?
|
||||
attachInterrupt(digitalPinToInterrupt(this->flowPin), flowISR, RISING);
|
||||
this->lastMillisReadFlow = millis();
|
||||
|
||||
digitalWrite(this->pumpPin, LOW);
|
||||
digitalWrite(this->ledPin, LOW);
|
||||
@@ -34,6 +40,12 @@ void Pump::loop() {
|
||||
this->stateStandby();
|
||||
break;
|
||||
|
||||
case State::PreError : {
|
||||
bool res = this->checkSensorsValues();
|
||||
this->statePreError(res);
|
||||
}
|
||||
break;
|
||||
|
||||
case State::Error :
|
||||
this->stateError();
|
||||
break;
|
||||
@@ -81,15 +93,24 @@ uint32_t Pump::getRemainingTime() const {
|
||||
}
|
||||
|
||||
void Pump::readSensorValues() {
|
||||
if (millis() - this->lastMillisReadSensor < Pump::delayReadSensor)
|
||||
return;
|
||||
this->lastMillisReadSensor = millis();
|
||||
if (millis() - this->lastMillisReadPressure > Pump::delayReadPressure) {
|
||||
this->pressure = analogRead(this->pressurePin);
|
||||
// this->pressure = 12;
|
||||
this->lastMillisReadPressure = millis();
|
||||
}
|
||||
|
||||
// Flussimpulseigenschaften: F=(8xQ-4)±5%, F ist die Frequenz, Q ist L/Min.
|
||||
if (millis() - this->lastMillisReadFlow > Pump::delayReadFlow) {
|
||||
uint32_t edgesDelta = interruptCounter;
|
||||
interruptCounter = 0;
|
||||
this->lastMillisReadFlow = millis();
|
||||
|
||||
//TODO: read pressure
|
||||
this->pressure = 12;
|
||||
//TODO: read flow
|
||||
this->flow = 13;
|
||||
uint32_t timeDelta = millis() - this->lastMillisReadFlow;
|
||||
if (timeDelta > Pump::delayReadFlow + 50)
|
||||
Serial.println(F("Flow resualt may be inaccurate"));
|
||||
this->flow = (edgesDelta + 4) / 8;
|
||||
// this->flow = 13;
|
||||
}
|
||||
}
|
||||
|
||||
bool Pump::checkSensorsValues() {
|
||||
@@ -97,12 +118,12 @@ bool Pump::checkSensorsValues() {
|
||||
return true;
|
||||
this->lastMillisCheckSensor = millis();
|
||||
|
||||
if (this->flow) {
|
||||
if (this->flow > 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!this->pressure) {
|
||||
this->changeState(State::Error);
|
||||
if (this->pressure < Pump::minPressure) {
|
||||
this->changeState(State::PreError);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -158,6 +179,11 @@ void Pump::changeState(State state) {
|
||||
this->handlePins(false);
|
||||
break;
|
||||
|
||||
case State::PreError :
|
||||
this->lastStatePreError = this->state;
|
||||
this->preErrorStartTime = millis();
|
||||
break;
|
||||
|
||||
case State::OnStupid :
|
||||
this->handlePins(true);
|
||||
break;
|
||||
@@ -203,3 +229,14 @@ void Pump::stateError() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Pump::statePreError(bool checkSensor) {
|
||||
if (checkSensor) {
|
||||
this->changeState(this->lastStatePreError);
|
||||
return;
|
||||
}
|
||||
|
||||
if (millis() - this->preErrorStartTime > Pump::maxPreErrorTime) {
|
||||
this->changeState(State::Error);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user