if statement for ultrasonic sensor

Hello, I created a project for an a funny alarm system. The Arduino will be placed at the end of a hallway before my bedroom door. By using an ultrasonic sensor to determine when an individual is walking down the hall, the system will be activated and close the door to my room. It will be closed using a Pneumatic Cylinder to gently push the bedroom door closed to keep the unwanted individuals out.

my problem is getting a working If statement to tell the ultrasonic sensor to active the pump when someone is within range.

My current code can be seen below.

Thanks to anyone who can help!

// Below is for clinder

#define pump 34 // pump

#define push 35 // push solenoid

#define pull 36 // pull solenoid


#define trigPin 4

#define echoPin 5

#define button 3

long duration;

int distance;

void setup() {



pinMode(pump, OUTPUT); //relay for pump

pinMode(push, OUTPUT); //relay push solenoid

pinMode(pull, OUTPUT); //relay pull solenoid



pinMode(trigPin, OUTPUT); //settings

pinMode(echoPin, INPUT); //settings


void loop() {


//if (digitalRead(trigPin) == LOW){ // found out i need 2 "=" or else it wont work

//if (digitalRead(echoPin) == LOW)

// Clear the trigPin by setting it LOW:

digitalWrite(trigPin, LOW);


// digitalRead(button, LOW); Check if needs to moves eleswhere

digitalWrite(trigPin, HIGH);


digitalWrite(trigPin, LOW);

// was using this due to errors recived

//duration = pulseIn(echoPin, HIGH);

int time1 = pulseIn(echoPin, HIGH);

int distance = time1/ 58;


// piston in and out motion

if(cm > 150){

// for(int i = 0; ; i++)

else if (A == HIGH && B == HIGH && C == LOW){

digitalWrite(pump, HIGH);

digitalWrite(push, HIGH);

digitalWrite(pull, LOW);

delay(4000); // make button work for 4 seconds

digitalWrite(pump, HIGH);

digitalWrite(push, LOW);

digitalWrite(pull, HIGH);

delay(4000); // make button work for 4 seconds




else if (A == HIGH && B == HIGH && C == LOW){

What is A?
What is B?
What is C?

Does this code even compile?

int time1 = pulseIn(echoPin, HIGH); pulseIn returns unsigned long.

Please remember to use code tags when posting code.

int distance = time1/ 58;
// piston in and out motion
if(cm > 150){

You are saving the result of a calculation in ‘distance’ and looking for a value in ‘cm’. Those are two different variables.

I can’t even get it to compile.

the A, B, and C are for the solenoid valve to control when to push or pull air.
i didn't realize that I didn't paste them in the top of the code as:
int A = 0;
int B = 0;
int C = 0;

I had the code working when I had a push button connected to the pump. I removed all the button parts of the code as I want the code to work without it. the ultrasonic sensor was taking reading before an I could see the values on the monitor. Once I removed the button commands I can't figure out how to make the if statement work to where the ultrasonic sensor will activate the pump.

And what changes the values of A, B and C?

Did you forget to post that too?

There are too many "attempts" and "experiments" in that code, to troubleshoot it effectively. You need to pare it down to something you're almost sure of, such as one of the sketches that you say worked. The way it is, it's a bit of guesswork what you would keep and what you would throw away.

Also please heed the advice in reply #1, regarding putting your code in code tags to make it more readable.

thank you, i will be sure to use the code tags next time. Sorry it was my first question here.