Hello as I am new to the forum and coding, I hope I do this correctly. What I am trying to accomplish is I have two capacitive sensors as inputs to turn a relay on or off to allow a valve to fill a tank. The valve is spring loaded so once the relay is shut off the valve will close. One sensor will be set near bottom of tank and the other near the top. I'm also using a display so that anyone can see if the system is currently filling or considered full. Here is my current code. Thanks in advance!
#include <Wire.h> // deals with I2C connections
#include <LiquidCrystal_I2C.h> // activates the LCD I2C library
// Define pins for Valves
const int fillvalve = 22;
// Define pins for sensors
const int lowlevelsensor = 50;
const int filledlevelsensor = 52;
const int overfilledlevelsensor = 48;
int filling = 0; //varialbe to store read value
int full = 0; //variable to store read value
// OBJECT CONFIGURATIONS
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
filling = digitalRead(lowlevelsensor);
full = digitalRead(filledlevelsensor);
Serial.begin(9600);
// LCD Initialization
lcd.backlight();
lcd.init();
lcd.clear();
// Set valves as OUTPUT, sensors as INPUTS
pinMode(fillvalve, OUTPUT);
pinMode(lowlevelsensor, INPUT);
pinMode(filledlevelsensor, INPUT);
digitalWrite(fillvalve, LOW); //Initialize valve closed
}
void loop() {
lcd.setCursor(3,0);
lcd.print("Company Name Here");
//code for opening valve
if(filling == 0){
digitalWrite(fillvalve, HIGH);
lcd.setCursor(3,2);
lcd.print("System Filling");
}
//code for closing valve
if(full & filling == 1) {
digitalWrite(fillvalve, LOW);
lcd.setCursor(3,2);
lcd.print("System Full");
}
{
Serial.print(digitalRead(fillvalve));
delay(1000);
}
}
This is a good project to simulate in the Wokwi simulation:
Can you click on the code with the right mouse button and select "Format Document" ?
Then make the source code look better. Put every indent, every space, every comma at the right place.
Make more use of the Serial Monitor. You could show the three inputs on the Serial Monitor, so you can see something changing if a input changes.
My irrigation water storage is similar to your, except I use two float switches. The water comes in at the top of the tank, so there is a lot of turbulence/waves, and spray. That makes the filled switch bounce up and down. turning on and off a few times. I had to ignore the full switch after it first indicated full, until the empty switch was triggered. You may need to look into the similar problem on your system.
I just hijacked the input variables. The wiring of the switches made it harder to hijack the actual inputs. Close enough.
I added a millis() mechanism so the loop runs every 100 ms. Thus fully unblocked, there is almost an entire Mega worth of resources left unused and available for further tricks and hacks. Or real functionality.
I added a LED because the one on the relay didn't make itself apparent to me in time. I see it now.
The slide fader is easier to manipulate for demonstration purposes, and will not allow contradictory sensor input (too full and empty), but those conditions might be tested for as a diagnostic in the real code.
The only thing I don't like is the LCD report of "System Full" when the valve is closed and the level is below full, presumably on the way to needing to be filled.
With a little help from friends who don't want to be named.
It is not always possible to do this kind of thing. Starting with a non-blocking sketch is very useful, and the more it is IPO orientated, the better. Here there was no problem as at least the I part, two digitalRead()s was obvious and appeared but once, at the top of the loop() function.
Next: add a neopixel tank level strip and let it fill and empty as the valve is open and closed, hands off operation! Don't hold your breath, but I have that in a bag somewhere around here...
So the issue is that the relay is not turning on and off as I believe it should. ill double check hard wire connections. But when both sensors are on the relay should cut off and when the lowelevel sensor goes low the relay should turn on, correct?
it does not actually use the true state of lowlevelsensor when turning off as this has long since turned off as we are filling, it using the remembered state, if it's filling and it's full, then yes relay turns off..
when it turns relay off, it also toggles filling, then when it needFilling and it's not already filling, turns on relay, toggles filling..
So, I verified that I am seeing the correct output to my relay based on the signal from my two inputs. What is throwing me off is that I didn't see the led for my relay turning on or off (may be a power issue) and that my display only shows System Filling it doesn't change state once both sensors are made.
Also, I would like to thank all those who took the time to help me with this process as I hope that I may be able to help others going forward.
// Define pins for Valves
const int fillvalve = 22;
// Define pins for sensors
const int lowlevelsensor = 50;
const int filledlevelsensor = 52;
const int overfilledlevelsensor = 48;
int filling = 0; //varialbe to store read value
int full = 0; //variable to store read value
void setup() {
filling = digitalRead(lowlevelsensor);
full = digitalRead(filledlevelsensor);
Serial.begin(9600);
// Set valves as OUTPUT, sensors as INPUTS
pinMode(fillvalve, OUTPUT);
pinMode(lowlevelsensor, INPUT);
pinMode(filledlevelsensor, INPUT);
pinMode(overfilledlevelsensor, INPUT);
digitalWrite(fillvalve, LOW); //Initialize valve closed
}
void loop() {
bool needfilling = digitalRead(lowlevelsensor);
full = digitalRead(filledlevelsensor);
//code for opening valve
if(filling == 0 && needfilling == 0){
filling = 1;
digitalWrite(fillvalve, HIGH);
}
//code for closing valve
if(full == 1 && filling == 1) {
filling = 0;
digitalWrite(fillvalve, LOW);
}
{
Serial.print(digitalRead(fillvalve));
delay(1000);
}
}
So I have dumb down the code no longer using display, but what I see now is my relay is not stable when all three sensors are showing 0 the relay should be on and once the level of liquid reaches the filledlevelsensor the relay should cut off until the lowlevelsensor goes low again.