Constrain() ultrasonic inputs to control LED

Hello everyone, I'm trying to use an ultrasonic sensor to control an LED using the constrain() and map() functions. But it's not working. Some help would be very appreciated. Thank you!

The goal is to limit the inputs between 2 and 22 using the constrain() function.

Code:

// Arduino pin numbers for the sensor output and input pins
const int pingTrig = A0;
int pingEcho = A1;

void setup() {
// initialize serial communication
Serial.begin(9600);
// initialize sensor pins
pinMode(pingTrig, OUTPUT); // change A0 pin mode to digital output
pinMode(pingEcho, INPUT); // change A1 pin mode to digital input
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The HC-SR04 is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse
digitalWrite(pingTrig, LOW);
delayMicroseconds( 5);
digitalWrite(pingTrig, HIGH);
delayMicroseconds(10);
digitalWrite(pingTrig, LOW);

pingEcho = constrain(pingEcho, 2, 22);
pingEcho = map(pingEcho, 2, 22, 0, 255);

// The Echo pin is used to read the signal from HC-SR04; a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(pingEcho, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to the Ping sensor datasheet, there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.

return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.

return microseconds / 29 / 2;
}

Serial output:
0 in, 0 cm
0 in, 0 cm
0 in, 0 cm
0 in, 0 cm
0 in, 0 cm
0 in, 0 cm

You have not yet received a value for pingEcho, why are you constraining it or mapping it.

Paul

because in the example code from the IDE I'm using they did not read the pingEcho values and they were displayed anyway. All I did to the code was adding these two lines to limit the inputs

But how can they limit the values at that point, you have not received it yet?
Paul

This is the original code without the added two lines and it works perfectly. The values are read and displayed in the serial monitor. I wanted to limit those values. I don't know how they read them without using digitalRead() or analogRead()

Code:
// Arduino pin numbers for the sensor output and input pins
const int pingTrig = A0;
int pingEcho = A1;

void setup() {
// initialize serial communication
Serial.begin(9600);
// initialize sensor pins
pinMode(pingTrig, OUTPUT); // change A0 pin mode to digital output
pinMode(pingEcho, INPUT); // change A1 pin mode to digital input
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The HC-SR04 is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse
digitalWrite(pingTrig, LOW);
delayMicroseconds( 5);
digitalWrite(pingTrig, HIGH);
delayMicroseconds(10);
digitalWrite(pingTrig, LOW);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to the Ping sensor datasheet, there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.

return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.

return microseconds / 29 / 2;
}

output:
5 in, 12 cm
1 in, 4 cm
3 in, 8 cm

output is based on distance to the sensor

Your constrain, etc is being applied always to the previous values.
Paul

That was what I tried but I keep on getting 0 readings.

pingEcho is an analog input pin.

Just a guess, this might be what you’re trying to do:

duration = constrain(duration, 2, 22);
duration = map(duration, 2, 22, 0, 255);

The two lines above must be after the call to pulseIn(), that’s what Paul is telling you.

Okay I’m gonna try that. Your help is much appreciated

Just tried it and it didn’t work. I get a constant output of:
1 in, 4cm
1 in, 4 cm
Even though I move the object’s distance from/to the sensor

@diefg, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project.

Please edit your posts that contain code, select all code and click the </> button to apply code tags. It makes it easier to read and copy and prevents the forum software from misinterpreting.

Your sketch is missing a critical line:
duration = pulseIn(pingEcho, HIGH, 30000);

Since you never measure the HIGH pulse duration on the 'pingEcho' pin, you have no way to calculate the distance.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.