#include <TM1637.h>
#include <EEPROM.h>
// Initialize the display module
const int CLK = 12;
const int DIO = 9;
TM1637 tm(CLK, DIO);
const int inPin1 = A0; // Analog input from level sensor
const int pressPin = A1; // Analog input from pressure sensor 1
const int overPPin = A3; // Analog input from pressure sensor 2
const int inPin2 = A2; // Analog input from Override POT
const int faultPin = 2; //Fault Indication
//const int pin3 = 3
const int relayPin = 4; // Output controlling relay
const int overridePin = 5; //Digital input for override switch
const int flasher = 6; //LED
const int zeroPin = 7; // Zeroing pin
const int fillPin = 8;
const int outPin = 10;
const int eepromAddress = 0; // EEPROM address to store the maximum value
int overrideState = 0; //override state on start
int offVal = 920; //Relay off @ (0-1023)
int overPTH = 510; // Overpressure cutoff (0-1023)
int actualValadv;
int maxInputValue = 0; // Maximum input value
float scalingFactor = 0.0049; // Scaling factor for mapping the input value to 0-5V range
unsigned long pressStartTime = 0; // Variable to store the start time of the button press
const unsigned long longPressDuration = 5000; // Long press duration in milliseconds (5 seconds)
void setup() {
pinMode(inPin1, INPUT);
pinMode(fillPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
pinMode(flasher, OUTPUT);
pinMode(faultPin, OUTPUT);
pinMode(overridePin, INPUT_PULLUP);
pinMode(zeroPin, INPUT_PULLUP);
pinMode(outPin, OUTPUT); // Set up digital output pin for 0-5V DC output
//analogReference(EXTERNAL);
Serial.begin(9600);
tm.set(3);// Set the brightness of the display (0 to 7)
// Read the maximum value from EEPROM
maxInputValue = EEPROM.read(eepromAddress);
delay(3000);
}
void loop() {
// Check if manual override switch is on
overrideState = digitalRead(overridePin); // check the state of the override switch
if (overrideState == LOW) { // if switch is on
int overrideVal = analogRead(inPin2);// Read the voltage from the override and output it directly
float inPin2 = (overrideVal * 5) / 1023.0;
analogWrite(outPin, (inPin2 / 5) * 255);
digitalWrite(flasher,HIGH);
}
else {
digitalWrite(flasher,LOW);
// Check if the button is pressed for max value reset
if (digitalRead(zeroPin) == LOW) {
if (pressStartTime == 0) {
// Start the timer for button press
pressStartTime = millis();
}
else if (millis() - pressStartTime >= longPressDuration) {
// Long press duration reached, reset the maximum input value
maxInputValue = analogRead(inPin1)/4;
EEPROM.update(eepromAddress, maxInputValue);
}
}
else {
// Button is not pressed, reset the timer
pressStartTime = 0;
int sensorValue = 0; // Initialize sensor value
float outputVoltage = 0.0; // Initialize output voltage
// Limit the input value to the maximum value
sensorValue = min(sensorValue, (maxInputValue*4));
// Scale the input value based on the maximum value
float outputValue = map(sensorValue, 0, (maxInputValue*4), 0, 1023) * scalingFactor;
int outputbit = outputValue /scalingFactor;
// Convert sensor value to voltage (0-5V range)
outputVoltage = (sensorValue * 5) / 1023.0;
// Write the scaled value to the output pin
analogWrite(outPin, outputValue / 5.0 * 255);
int displayValue = (outputbit);
// Convert the display value to an array of digits
tm.display(0, displayValue / 1000);
tm.display(1, (displayValue / 100) % 10);
tm.display(2,(displayValue / 10) % 10);
tm.display(3,displayValue % 10);
int digitalVal = digitalRead(fillPin);
if (digitalVal == LOW) {
digitalWrite(relayPin, HIGH); // turn relay on
}
else if (outputbit >= offVal) { // if value is greater than or equal to threshold2
digitalWrite(relayPin, LOW); // turn relay off
}
}
}
}