I am having an issue with my breadboard setup not responding the same as the simulator for a small part of the code.
Below is a link to the simulator.
For the moment I will just refer to the sequence of operation for the part that is not working. I have also mentioned sensors for test purposes I am using potentiometers.
Operation works as follows-
If the level sensor reads <90% full scale and the pressure sensor reads <50% full scale the push button can be used to energize the relay. The tank will begin to fill and the level sensor will increase once it reaches 90% the relay will turn off, if at any time the pressure sensor is =>50% the relay will be de-energized.
As you can see in the below simulator it all works as intended. When I physically test the exact same setup on the bench the setting for the pressure cut off and the level seem to be acting against one another or together. For example the relay is dropping out at when the level is ~2.4v (490) and the pressure is 0v if i change the level value is seems to have little to no effect, if I change the pressure value this seems to then allow for a different cut out point. but it gets to a point where the values overlap. I know this is not the best explanation but it is also very hard to describe. at first I thought it may be a problem with the board, so i swapped to another but it did exactly the same thing. I am leaning towards an issue with my IF, ELSE blocks and how I have set them up. Any suggestions?
#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
}
}
}