#include <TM1637.h>
// Initialize the display module
const int CLK = 8;
const int DIO = 9;
TM1637 tm(CLK, DIO);
const int inPin1 = A0; // Analog input from level sensor
const int inPin2 = A1; // Analog input from Override POT
const int relayPin = 4; // Output controlling relay
const int overPPin = A2; // Analog input from pressure sensor
const int overridePin = 5; //Digital input for override switch
const int fillPin = 2; //Pushbutton for fill cmd
const int outPin = 10; //0-5v pwm output to external device.
int overrideState = 0; //override state on start
//int static_variable = 500;
//int onVal = 100; //Relay on @ .7v (0-1023)
int offVal = 920; //Relay off @ (0-1023)
int overPTH = 510; // Overpressure cutoff (0-1023)
int actualValadv;
void setup() {
// Set up analog input pins
pinMode(inPin1, INPUT);
pinMode(fillPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
pinMode(overridePin, 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)
}
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);
}
else {
// Output the DP reading as a voltage on the digital output pin using PWM
int sensorValue = 0; // Initialize sensor value
float outputVoltage = 0.0; // Initialize output voltage
// Take 16 samples and calculate the average
for (int i = 0; i < 16; i++) {
sensorValue += analogRead(inPin1);
delay(5); // Delay between samples
}
sensorValue /= 16;
// Convert sensor value to voltage (0-5V range)
outputVoltage = (sensorValue * 5) / 1023.0;
// Output voltage to PWM output pin 10
analogWrite(outPin, outputVoltage * 255.0 / 5.0);
// Print voltage to serial monitor
//Serial.print("Input voltage = ");
//Serial.print(sensorValue * 5 / 1023.0);
//Serial.print("V, Output voltage = ");
//Serial.print(outputVoltage);
//Serial.println("V");
//delay(100); // Delay between readings
int displayValue = (sensorValue);
// 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);
//Avereges the level sensor reading
float relayCtrl = 0.0;
for (int i = 0; i < 16; i++) { // Take 16 readings and average them
relayCtrl += analogRead(inPin1);
delayMicroseconds(50); // Wait between readings to reduce noise
}
relayCtrl /= 16.0;
if (digitalVal == LOW) {
digitalWrite(relayPin, HIGH); // turn relay on
} else if (relayCtrl >= offVal) { // if value is greater than or equal to threshold2
digitalWrite(relayPin, LOW); // turn relay off
}
float pVal = 0.0;
for (int i = 0; i < 16; i++) { // Take 16 readings and average them
pVal += analogRead(overPPin);
delayMicroseconds(50); // Wait between readings to reduce noise
}
pVal /= 16.0;
if (pVal >= overPTH) //if pressure increases over threshold
digitalWrite(relayPin, LOW); //turn relay off
}
}
}Level Sensor
Pressure Sensor
Override
Fill Button