Coding the "IF/Else" function w/ HC-SR04 and Arduino UNO

Hello all in the arduinosphere,
I am a super noob and have taken on a project beyond my newly acquired knowledge base. Im am attempting to make an automated liquid fill control regulator. I dont know what t call it but when the liquid gets so low the arduino needs to send a high pulse to the relay which opens the valve. I have all the hardwaretaken care of. I just cant get the coding down. I can get the sensor to display distance on the serial monnitor but how do code, if the output = 12" open the valve, when it reads 4" close the valve? The goal is to automatically fill the resevoir when it gets low. Can anyone help me please to figure out how to code this. Im using an arduino uno and an HC-SR04 sensor. This is what I have so far:

int trigPin = 11;
int echoPin = 12;
long duration, inches;
int selenoidPin = 10;
const int threshold = 6;

void setup() {
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(selenoidPin, OUTPUT);

//Serial Port begin
Serial.begin (9600);

}

void loop() {
int digitalValue = digitalRead(echoPin);
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

if (digitalValue > threshold) {
digitalWrite(selenoidPin, HIGH);

} else {
digitalWrite(selenoidPin, LOW);
}
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

inches = (duration/2) / 74;

Serial.print(inches);
Serial.print("in, ");
Serial.println(digitalValue);
delay(1);
}
Please tell me why it will read the distance but not engage the selenoid by sending a HIGH to the pin?
Thank you

FirstAttempt.ino (860 Bytes)

(Deleted my suggestion, sorry if you already read it, but I realised it was wrong :frowning: )

This probably makes more sense compared to what I had earlier. Possible that the distances will never exactly equal 12 or 4, so:

if (distance > 11.5)
{
   digitalWrite(relayPin, HIGH); //or low, whichever opens the valve
}

if (distance < 4.5)
{
   digitalWrite(relayPin, LOW); //or high, whichever closes the valve
}

Thank you Im going to try that right now Ill let you know if it worked.

This is how I implemented your suggestion :

int echoPin = 12;
long duration, inches;
int selenoidPin = 10;
const int threshold = 6;

void setup() {
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(selenoidPin, OUTPUT);

//Serial Port begin
Serial.begin (9600);

}

void loop() {
int digitalValue = digitalRead(echoPin);
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

if (threshold > 11.5)
{
digitalWrite(selenoidPin, HIGH);
}

if (threshold < 4.5)
{
digitalWrite(selenoidPin, LOW);
}
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

inches = (duration/2) / 74;

Serial.print(inches);
Serial.print("in, ");
Serial.println(digitalValue);
delay(1);
}
But unfortunatly it still doesnt work. Im using an led to represent the selenoid and i will include a picture of the hardware to ensure it is setup right. Also my dad says I need to inform you that I am 12 years old. Not sure why that matters but i just do what im told. also pic no good, too big to upload.

![](https://lh3.googleusercontent.com/9hDC0YVFx2NFNgs9ljtX-
YP71GIrTJIPJh4BvYwu2_auoIxIZTW0IVZFk4ZIjZ2w9o9Y_y8M4XniiwsK7NhJm11ZmnbkqnNjabgktyGykcUky3xz-v2fLgfIjpXw3crebBrDYdgiv2dpivbJZY8nuW7vnTDeabZTNjllnu3vwle3Qx1Sc4Fs2WL4cUgZizAkSspWiOt5LFW4rPLnJgOBZmokrWHu12-Fskt2VjuKeEtrBFdsODmsBo4c9YCEKYONL0SyOJsKDRlcb1Vh20j4cS3bqI6NFnmnGAMuXsJDZaLmZsm9Bih7tqynUfjsB0-6JrntckRUEiCyNm_08iNyKWsWhBGfzAOWw-qOsx31LRehZlUSWqbzdIitTbfK54d5Q7n6HTAV7osLVrR_ZqHAR3380Ch72C6OIG15RMxOZ8UPsmYoYEY7qEvD621BrYlt5GSy0QBXrvkYQWGKv0B7CQ-tREOm7iXnFeWGDzyoEy6k0djrDKqhf1AkPESSVmKbSoHI6jsEArtTuc0fPRtH-764kbLS87RGZrnyo4U5tS86N7JFewB2gs_dBSwJ1fpgLn7OkJBuJLK1ELoDIwwC8ADH8r-i6BY47pAXU5GaUMWmdr5bYRzo=w1662-h935-no)

so ill just add a link, heck yeah I love technology!!

In loop(), you never update the value of "threshold." You defined it as a constant, 6, so it will forever be 6, and so threshold < 4.5 will always be false, and threshold > 11.5 will always be false.

I think you meant to compare "inches" to the two thresholds, 4.5 and 11.5.

Also, suggest you use the NewPing library Arduino Playground - NewPing Library and its ping_median() method, to easily eliminate the occasional erroneous measurement.

PS: "inches" is not a great variable name. "measuredDistance" would be more descriptive.

Compare inches ( the one which you have calculated after pulse, to a threshold)

if (inches > threshold) {
digitalWrite(selenoidPin, HIGH);

} else {
digitalWrite(selenoidPin, LOW);
}

vishnu_v:
if (inches > threshold) {
digitalWrite(selenoidPin, HIGH);
} else {
digitalWrite(selenoidPin, LOW);
}

The above does not accomplish the OP's goal:

TrstNRsk:
if the output = 12" open the valve, when it reads 4" close the valve? The goal is to automatically fill the resevoir when it gets low.

@manor_royal's approach does. The OP just implemented it incorrectly, as explained in post #4.