Need assistance with coding

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);
  }
}

Hi @southcentralla ,

Welcome to the forum..

Good job on the first post, although you don't mention what is wrong..

quick look, think the above should be in the loop..

good luck.. ~q

Should pad with spaces so it erases what is already there..

  lcd.print("System Full   ");

otherwise it says Fulling.. :slight_smile:

~q

need another bool to keep track of whether or not we are actually filling..
lowlelvelsesnor won't be low once we start filling..
maybe like this..

#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() {


  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() {

  bool needFilling = digitalRead(lowlevelsensor);
  full = digitalRead(filledlevelsensor);

  lcd.setCursor(3, 0);
  lcd.print("Company Name Here");

  //code for opening valve
  if (filling == 0 && needFilling == 0) {
    filling = 1;
    digitalWrite(fillvalve, HIGH);
    lcd.setCursor(3, 2);
    lcd.print("System Filling");

  }
  //code for closing valve
  if (full && filling) {
    filling = 0;
    digitalWrite(fillvalve, LOW);
    lcd.setCursor(3, 2);
    lcd.print("System Full   ");
  }

  {
    Serial.print(digitalRead(fillvalve));
    delay(1000);
  }
}

have fun.. ~q

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.

1 Like

yours looks better than mine.. :slight_smile:
Mega Pumps..

~q

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 could not resist adding a slide fader to @qubits-us simulation. See what I did there?

When the slide fader is enabled, the slide switches are overridden.

Try it here


Wokwi_badge Simulated Simulation


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...

a7

  • Probably because the sun is being obscured by clouds while you are lying on the sand.
    :sunglasses:
1 Like

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?

No and Yes..

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..

seems good in sim..

What are you seeing??

~q

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.

You have not posted code since the code that had many issues that have been found.

You have not said you've tried any of the three wokwi simulations that have been linked on this thread.

You have not posted the code you are looking at now, nor said you've tried to place that in the wokwi, where there would be no hardware issues.

Please post the code you are working with now. A sketch that compiles that we could for ourselves.
a7

Hi, @southcentralla
Welcome to the forum.

What model of Arduino are you using?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

I'm using the Arduino mega 2560.

I have tried two of the wokwi simulations, the one Koepel and alto777 posted. alto777 yours worked as I intend for the program to run.

The only simulator I used for arduino before this was tinker cad. wokwi is new to me so I'm learning how to navigate.

Once I get a chance I will re-post any changes I've made to code. Thank You.

// 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.

My physical set up

  • Signal wires should be kept away from power wires.
  • How are switches wired ?
  • We need to see a schematic of this project.

  • Start using defines so as to make your code readable.
if(filling == 0 && needfilling == 0){
  filling = 1;

1s and 0s are meaningless.

The reason for using 1s and 0s are because these are the inputs from the sensors. I will try and get a schematic up.