Skip to content

Commit

Permalink
(fix): ticket #8 - added failure state for bad pressure sensor, added…
Browse files Browse the repository at this point in the history
… relay shut off override to control functions
  • Loading branch information
mwood77 committed Jan 14, 2022
1 parent 40b2733 commit b976cf5
Showing 1 changed file with 51 additions and 21 deletions.
72 changes: 51 additions & 21 deletions koffie/koffie.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
unsigned int EEPROM_PRESSURE_ADDRESS{0};
int TEMPERATURE_SMOOTHING_HOLDER_POSITION{0};
int TEMPERATURE_SMOOTHING_HOLDER[5]{0, 0, 0, 0, 0};
int ERROR_COUNT{0};
int CURRENT_MODE;
bool PROGRAMMING_MODE = false;
long OLD_ENCODER_POSITION{0};
Expand Down Expand Up @@ -431,8 +432,14 @@
TEMPERATURE_SMOOTHING_HOLDER_POSITION++;
}

drawTemperatures(smoothValues(TEMPERATURE_SMOOTHING_HOLDER));
updatePressure();
if ( ERROR_COUNT >= 20 ) { // Faulty pressure sensor gate
relay.setDutyCyclePercent(0);
relay.setRelayPosition(relayPositionOpen); // change relayPositionOpen to relayPositionClosed if your relay is normally open
drawSensorFailure();
} else {
drawTemperatures(smoothValues(TEMPERATURE_SMOOTHING_HOLDER));
updatePressure();
}

if (PROGRAMMING_MODE == 1) {
toggleLED(CURRENT_MODE);
Expand All @@ -451,29 +458,37 @@
float sensor = analogRead(PRESSURE_SENSOR_INPUT_PIN);
float convertedReading = convertPressureUnits(sensor);

MEASUREMENT_INPUT = convertedReading;
Serial.println(convertedReading - MEASUREMENT_INPUT);

// COMPUTE PID LEVELS
pidControl.Compute();

// APPLY PID COMPUTE TO RELAY
relay.loop();

if (MEASUREMENT_INPUT > SETPOINT) {
relay.setDutyCyclePercent(0);
if ( (convertedReading - MEASUREMENT_INPUT) >= 2 ) {
ERROR_COUNT++;
} else {
relay.setDutyCyclePercent(CONTROL_OUTPUT / 255.0); // Relay library only accepts values between 0 and 1
}
MEASUREMENT_INPUT = convertedReading;

// Serial.print(F("SETPOINT: "));
// Serial.print(SETPOINT); // only displays in BAR in serial monitor
// Serial.print(F(" | MEASUREMENT_INPUT: "));
// Serial.print(MEASUREMENT_INPUT);
// Serial.print(F(" | dutyCycle: "));
// Serial.print(dutyCycle * 100);
// Serial.println(F("%"));
// COMPUTE PID LEVELS
pidControl.Compute();

// APPLY PID COMPUTE TO RELAY
relay.loop();

// @todo - double check relay library handling of when dutyCycle = 0%
if (MEASUREMENT_INPUT > SETPOINT) {
relay.setDutyCyclePercent(0);
relay.setRelayPosition(relayPositionOpen); // change relayPositionOpen to relayPositionClosed if your relay is normally open
} else {
relay.setDutyCyclePercent(CONTROL_OUTPUT / 255.0); // Relay library only accepts values between 0 and 1
}

drawPressure(convertedReading, PROGRAMMING_MODE);
// Serial.print(F("SETPOINT: "));
// Serial.print(SETPOINT); // only displays in BAR in serial monitor
// Serial.print(F(" | MEASUREMENT_INPUT: "));
// Serial.print(MEASUREMENT_INPUT);
// Serial.print(F(" | dutyCycle: "));
// Serial.print(dutyCycle * 100);
// Serial.println(F("%"));

drawPressure(convertedReading, PROGRAMMING_MODE);
}

}

Expand Down Expand Up @@ -630,6 +645,21 @@

}

/**
* Draws an error message when a high number of sensor failures have been detected
*/
static void drawSensorFailure() {
display.clearDisplay();
display.drawRoundRect(10, 10, 100, 50, 4, WHITE);
display.setCursor(40, 24);
display.println(F("P. SENSOR"));
display.setCursor(40, 34);
display.println(F("FAILURE"));
display.setCursor(40, 44);
display.println(F("DETECTED"));
display.display();
}

/**
* Draws current mode as a filled-in rectangle
*/
Expand Down

0 comments on commit b976cf5

Please sign in to comment.