#include <Wire.h>
#include <SparkFun_MS5803_I2C.h>
MS5803 sensor(ADDRESS_HIGH);
// Create variables to store results
float temperature_c;
double pressure_abs;
float pressurePSI; // variable to hold pressure in PSI
// Define LED pins
const byte led1Pin = 2; // Assign to LED-1
const byte led2Pin = 3; // Assign to LED-2
const byte led3Pin = 4; // Assign to LED-3
const byte led4Pin = 5; // Assign to LED-4
const byte buttonPin = 6; // Assign to the button
const byte upTrendPotPin = 32; // Potentiometer pins for up trend set points
const byte downTrendPotPin = 33; // Potentiometer pins for down trend set points
// Calibration variables
const unsigned long calibrationDuration = 5000; // 5 seconds
byte upTrendCount = 0; // variable to increase count when pressure changes
byte downTrendCount = 0; // variable to increase count when pressure changes
byte minUpTrendPoints = 2; // Minimum points to find a trend
byte maxUpTrendPoints = 12; // maximum points to find a trend
byte minDownTrendPoints = 2; // minimum points to find a trend
byte maxDownTrendPoints = 12; // Maximum points to find a trend
byte pUpPoint, pDownPoint = 2; // variables to hold previous min and max trend points
int maxAnalogResolution = 4095; // maximum analog resolution value (for arduino it's 1023, for esp32 it's 4095)
// Trend detection variables
bool noTrend = false;
bool noTrendLedState = LOW; // last led state for NO Trend
unsigned long trendStartTime;
unsigned long noTrendTime = millis();
int minThreshold = 0; // variable to hold minimum threshold pressure value
int maxThreshold = 0; // variable to hold maximum threshold pressure value
const int trendThresholdUp = 0; // Adjust this threshold
const int trendThresholdDown = 0; // Adjust this threshold
const int trendChangeCount = 4; // Number of consecutive data changes needed for a trend
void setup() {
// Start your preferred I2C object
Wire.begin();
// Initialize Serial Monitor
Serial.begin(115200);
// Retrieve calibration constants for conversion math.
sensor.reset();
sensor.begin();
// Set LED pins as OUTPUT
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
pinMode(upTrendPotPin, INPUT);
pinMode(downTrendPotPin, INPUT);
// Set button pin as INPUT
pinMode(buttonPin, INPUT_PULLUP);
// Start calibration Mode
unsigned long cTime = millis(); // Note current time
unsigned long ledTime = millis();
bool ledState = HIGH;
Serial.println("Starting Calibration Mode");
while ((millis() - cTime) < calibrationDuration) { // start calibration for 5 seconds = 5000 milliseconds
pressure_abs = sensor.getPressure(ADC_4096); // Read pressure from the sensor in mbar.
pressurePSI = pressure_abs * 0.0145038; // convert pressure into PSI
if (pressurePSI < minThreshold) {
minThreshold = pressurePSI; // Update minimum pressure threshold
} else if (pressurePSI > maxThreshold) {
maxThreshold = pressurePSI; // Update maximum pressure threshold
}
if ((millis() - ledTime) > 300) {
digitalWrite(led1Pin, ledState);
ledState = !ledState; // revert led state
ledTime = millis();
}
delay(150); /// wait for 150 milliseconds before taking next reading
}
digitalWrite(led1Pin, LOW); // Turn off LED-1
Serial.print("Min Pressure Threshold:");
Serial.print(minThreshold);
Serial.print(" | ");
Serial.print("Max Pressure Threshold:");
Serial.println(maxThreshold);
}
void loop() {
// Read potentiometer to get up/down trend point values
byte readUpPot = map(analogRead(upTrendPotPin), 0, maxAnalogResolution, minUpTrendPoints, maxUpTrendPoints);
byte readDownPot = map(analogRead(downTrendPotPin), 0, maxAnalogResolution, minDownTrendPoints, maxDownTrendPoints);
if (pUpPoint != readUpPot) { // If pot has rotated change the set point values
pUpPoint = readUpPot;
Serial.print("Up Trend Points value:");
Serial.println(pUpPoint);
}
if (pDownPoint != readDownPot) { // If pot has rotated change the set point values
pDownPoint = readUpPot;
Serial.print("Down Trend Points value:");
Serial.println(pDownPoint);
}
// Read temperature from the sensor in deg C.
temperature_c = sensor.getTemperature(CELSIUS, ADC_512);
// Read pressure from the sensor in mbar.
pressure_abs = sensor.getPressure(ADC_4096);
pressurePSI = pressure_abs * 0.0145038; // convert pressure into PSI
// Report values via UART
Serial.print("Temperature C = ");
Serial.print(temperature_c);
Serial.print(" | Pressure abs (mbar) = ");
Serial.print(pressure_abs);
Serial.print(" | Pressure abs (PSI) = ");
Serial.println(pressurePSI);
upTrendCount, downTrendCount;
if ((pressurePSI > maxThreshold) && (upTrendCount < pUpPoint)) { // Check if pressure is greater than maxThrehsold
upTrendCount += 1; // increase up trend count with 1
} else if ((pressurePSI < minThreshold) && (downTrendCount < pDownPoint)) { // Check if pressure is less than min threshold
downTrendCount += 1; // increase up trend count with 1
} else if ((pressurePSI >= minThreshold) && (pressurePSI <= maxThreshold)) { // Check if pressure is in normal range
upTrendCount = 0; // Reset to zero
downTrendCount = 0; // Reset to zero
digitalWrite(led2Pin, LOW); // Turn off LED-2
digitalWrite(led3Pin, LOW); // Turn off LED-3
if ((millis() - noTrendTime) > 300) {
digitalWrite(led4Pin, noTrendLedState); // Turn off LED-4
noTrendLedState = !noTrendLedState;
noTrendTime = millis();
}
}
if (upTrendCount >= pUpPoint) { // Check if uptrend has beed detected
digitalWrite(led2Pin, HIGH); // Turn On LED 2 when up trend has found
}
if (downTrendCount >= pDownPoint) { // Check if Dwon trend has beed detected
digitalWrite(led3Pin, HIGH); // Turn On LED 3 when up trend has found
}
// Check the button state
if (digitalRead(buttonPin) == LOW) {
upTrendCount = 0; // Reset to zero
downTrendCount = 0; // Reset to zero
// Button is pressed, reset and restart testing process
digitalWrite(led2Pin, LOW); // Turn off LED-2
digitalWrite(led3Pin, LOW); // Turn off LED-3
digitalWrite(led4Pin, LOW); // Turn off LED-4
}
delay(500);
}