# Can I program sensors into "distance" IF comments?

**URL:** <https://forum.arduino.cc/t/can-i-program-sensors-into-distance-if-comments/386026>\
**Category:** Programming\
**Created:** [May 15, 2016, 10:08am UTC](https://forum.arduino.cc/t/can-i-program-sensors-into-distance-if-comments/386026 "2016-05-15T10:08:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_McElrath](https://avatars.discourse-cdn.com/v4/letter/i/b782af/32.png) [@Ian\_McElrath](https://forum.arduino.cc/u/Ian_McElrath)\
**Post date:** [May 15, 2016, 10:08am UTC](https://forum.arduino.cc/t/can-i-program-sensors-into-distance-if-comments/386026/1 "2016-05-15T10:08:25Z")

</div>

Aloha! I'm trying to figure out how to program my "IF" comments to coincide with distance on two of my ultrasonic sensors. I want them to turn on separate LED's individually according to what is in their path. For example:

Is it okay if I code:

```cpp
  if (leftSensorEchoPin distance < minRange){
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(leftLEDMosfet, LOW);

```

and

```cpp
 if (rightSensorEchoPindistance < minRange){
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(rightLEDMosfet, LOW);

```

Or, if incorrect, how would I do that for each individual sensor? Thank you!

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 15, 2016, 10:18am UTC](https://forum.arduino.cc/t/can-i-program-sensors-into-distance-if-comments/386026/2 "2016-05-15T10:18:58Z")

</div>

The code looks OK apart from the missing closing braces on the code blocks, but I assume that these are in your complete program. A small improvement would be to make the second if into an else/if unless it is possible that both sensors could return a distance less than minRange.

---

<div class="post-metadata">

**Author:** ![Ian\_McElrath](https://avatars.discourse-cdn.com/v4/letter/i/b782af/32.png) [@Ian\_McElrath](https://forum.arduino.cc/u/Ian_McElrath)\
**Post date:** [September 22, 2016, 6:27pm UTC](https://forum.arduino.cc/t/can-i-program-sensors-into-distance-if-comments/386026/3 "2016-09-22T18:27:05Z")

</div>

Hey guys. I keep getting the following error message when attempting this with a practice code

```cpp
sketch_sep21a:41: error: expected ')' before 'distance'
  if (righttrigPin distance < 4) {  // This is where the LED On/Off happens
                    ^
exit status 1
expected ')' before 'distance'

```

Here's my code:

```cpp
#define righttrigPin 9
#define rightechoPin 8
#define lefttrigPin 13
#define leftechoPin 12
#define led 11
#define led2 10
#define led3 7
#define led4 6

void setup() {
  Serial.begin (9600);
  pinMode(righttrigPin, OUTPUT);
  pinMode(rightechoPin, INPUT);
  pinMode(lefttrigPin, OUTPUT);
  pinMode(leftechoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(lefttrigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(lefttrigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(lefttrigPin, LOW);
  duration = pulseIn(leftechoPin, HIGH);
  distance = (duration/2) / 29.1;
  digitalWrite(righttrigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(righttrigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(righttrigPin, LOW);
  duration = pulseIn(rightechoPin, HIGH);
  distance = (duration/2) / 29.1;
  
  if (righttrigPindistance < 4) {  // This is where the LED On/Off happens
    digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  digitalWrite(led2,LOW);
  
}
  else {
    digitalWrite(led,LOW);
    digitalWrite(led2,HIGH);
    digitalWrite(led3, LOW);
    digitalWrite(led4, HIGH);
  }
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {  0
    ;Serial.print(distance); 
    Serial.println(" cm");
  }
  delay(500);
}

```

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [September 22, 2016, 7:05pm UTC](https://forum.arduino.cc/t/can-i-program-sensors-into-distance-if-comments/386026/4 "2016-09-22T19:05:58Z")

</div>

```cpp
  if (righttrigPin distance < 4) {  // This is where the LED On/Off happens

```

Not sure why this error message was emitted, but variable names cannot contain blanks.

```cpp
  if (righttrigPindistance < 4) {  // This is where the LED On/Off happens

```

You have not defined a variable named "righttrigPindistance"

---

<div class="post-metadata">

**Author:** ![Ian\_McElrath](https://avatars.discourse-cdn.com/v4/letter/i/b782af/32.png) [@Ian\_McElrath](https://forum.arduino.cc/u/Ian_McElrath)\
**Post date:** [September 22, 2016, 7:25pm UTC](https://forum.arduino.cc/t/can-i-program-sensors-into-distance-if-comments/386026/5 "2016-09-22T19:25:46Z")

</div>

> jremington:
> 
> ```arduino
>   if (righttrigPin distance < 4) {  // This is where the LED On/Off happens
> 
> ```
> 
> Not sure why this error message was emitted, but variable names cannot contain blanks.
> 
> ```arduino
>   if (righttrigPindistance < 4) {  // This is where the LED On/Off happens
> 
> ```
> 
> You have not defined a variable named "righttrigPindistance"

Ah, gotcha. So if I were wanting to define this variable, or multiple distance variables, where would I add them? Thanks!

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 22, 2016, 7:30pm UTC](https://forum.arduino.cc/t/can-i-program-sensors-into-distance-if-comments/386026/6 "2016-09-22T19:30:06Z")

</div>

> **[Using Variables in Sketches | Arduino Documentation](https://docs.arduino.cc/learn/programming/variables)**
>
> What are variables, and how can we use them in a sketch.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:53pm UTC](https://forum.arduino.cc/t/can-i-program-sensors-into-distance-if-comments/386026/7 "2021-05-05T21:53:52Z")

</div>

Closed on May 5, 2021, 9:53pm UTC


