In need of help combining two Ultrasonic Sensor codes together

Hi everyone!

I’m currently building a fountain that reacts to movement, to experiment with the ultrasonic sensor (HC-SR04) I tried a lot of different codes but every single one of them was giving me a constant output of 0. Then I stumbled upon this code and it read the distance correctly:

long reflectionDuration; // The time it takes to get the echo of the waves

long distanceInCm; // Calculated distance in centimeter

const int TRIGGER_PIN = 7; // The arduino pin connected to the trigger pin

const int ECHO_PIN = 6; // The arduino pin connected to the echo pin



void setup() {

pinMode(ECHO_PIN, INPUT); // We receive the results from the Echo, so it's input

pinMode(TRIGGER_PIN, OUTPUT); // We trigger the module to start sending waves, so it's output

Serial.begin(9600); // Initialize serial pin to fetch responses from Arduino Uno

}



void loop() {

digitalWrite(TRIGGER_PIN, HIGH); // Set the trigger pin to HIGH to start sending

delayMicroseconds(10); // Wait for 10 microseconds while sending is in progress

digitalWrite(TRIGGER_PIN, LOW); // Stop sending waves

// Duration from sending the signal until the sensor module receives the echo of the waves (in microseconds)

// check the pulseIn() function documentation for more info.

reflectionDuration = pulseIn(ECHO_PIN, HIGH);

// The time takes for the signal to goes and gets reflected should be divided by 2 to

// get the time it takes for the ultrasonic wave to go from the module to the reflection point

// then divide it by 29.1 to calculate the distance in centimeter

distanceInCm = (reflectionDuration/2) / 29.1;

Serial.println(distanceInCm);

delay(250);

}

Source

But now I need to combine this with my project, and I’m very new to this so I need your help. I did this lesson in Tinkercad and I want to use this code and setup to power my motors. I know how it will work with motors and transistors but i want to test it with simple LED’s first. Below is the image and code:

Arduino setup

int distanceThreshold = 0;



int cm = 0;



int inches = 0;



long readUltrasonicDistance(int triggerPin, int echoPin)

{

pinMode(triggerPin, OUTPUT); // Clear the trigger

digitalWrite(triggerPin, LOW);

delayMicroseconds(2);

// Sets the trigger pin to HIGH state for 10 microseconds

digitalWrite(triggerPin, HIGH);

delayMicroseconds(10);

digitalWrite(triggerPin, LOW);

pinMode(echoPin, INPUT);

// Reads the echo pin, and returns the sound wave travel time in microseconds

return pulseIn(echoPin, HIGH);

}



void setup()

{

Serial.begin(9600);



pinMode(2, OUTPUT);

pinMode(3, OUTPUT);

pinMode(4, OUTPUT);

}



void loop()

{

// set threshold distance to activate LEDs

distanceThreshold = 350;

// measure the ping time in cm

cm = 0.01723 * readUltrasonicDistance(7, 6);

// convert to inches by dividing by 2.54

inches = (cm / 2.54);

Serial.print(cm);

Serial.print("cm, ");

Serial.print(inches);

Serial.println("in");

if (cm > distanceThreshold) {

digitalWrite(2, LOW);

digitalWrite(3, LOW);

digitalWrite(4, LOW);

}

if (cm <= distanceThreshold && cm > distanceThreshold - 100) {

digitalWrite(2, HIGH);

digitalWrite(3, LOW);

digitalWrite(4, LOW);

}

if (cm <= distanceThreshold - 100 && cm > distanceThreshold - 250) {

digitalWrite(2, HIGH);

digitalWrite(3, HIGH);

digitalWrite(4, LOW);

}

if (cm <= distanceThreshold - 250 && cm > distanceThreshold - 350) {

digitalWrite(2, HIGH);

digitalWrite(3, HIGH);

digitalWrite(4, HIGH);

}

if (cm <= distanceThreshold - 350) {

digitalWrite(2, HIGH);

digitalWrite(3, HIGH);

digitalWrite(4, HIGH);

}

delay(100); // Wait for 100 millisecond(s)

}

It will turn on the LED’s on and off depending on the distance of the object to the ultrasonic distance sensor. But it still reads the value as 0cm and 0in, so how do I combine the two codes so that I get the right readout?

But it still reads the value as 0cm and 0in, so how do I combine the two codes so that I get the right readout?

I think the first thing you need to do is grab a ruler or a tape measure. Is it marked in inches or centimeters?

There is no reason for cm and inches to be global when you only use the variables in loop().

pinMode(triggerPin, OUTPUT); // Clear the trigger

That is NOT what that code does.

One code (that works) sets the mode of the trigger and echo pins once. One code (that doesn't work) sets the mode of the trigger and echo pins over and over.

I don't know about you, but I'd go with the approach that works.

One code (that works) sets the trigger pin HIGH once and LOW once. One code (that doesn't work) sets the trigger pin HIGH twice and LOW twice.

I don't know about you, but I'd go with the approach that works.

Code tags are a wonderful thing. Too bad you failed to use them.

Thank you for the quick response and damn I totally missed the code tags, I feel so stupid having missed it xD

I know what you mean and I have to go with what works, but the function of the lower code (reading out the distance and setting another pin (which will be an LED) to high is what I need, but my sensor is not working with that code, but it IS working with the top code. So I need to merge the first one into the second one so that the second one will work.

So I need to merge the first one into the second one so that the second one will work.

Are you waiting for permission?

I just don't know how i merge them together? Where should what parts of the code go?

I just don't know how i merge them together?

You don't need to merge anything. You have a function that works, in one code, and a function that doesn't work in another code. Delete the function that doesn't work and copy the one that does.

I've already pointed out the differences between the working and the non-working versions.