# How to program "If" comments to discern between two different sensors

**URL:** <https://forum.arduino.cc/t/how-to-program-if-comments-to-discern-between-two-different-sensors/355122>\
**Category:** Programming\
**Created:** [December 28, 2015, 8:08pm UTC](https://forum.arduino.cc/t/how-to-program-if-comments-to-discern-between-two-different-sensors/355122 "2015-12-28T20:08:51Z")\
**Posts on this page:** 10\
**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:** [December 28, 2015, 8:08pm UTC](https://forum.arduino.cc/t/how-to-program-if-comments-to-discern-between-two-different-sensors/355122/1 "2015-12-28T20:08:51Z")

</div>

Hello everyone!

I'm finishing up some of my code this week and was hoping to ask a question. I have two separate Ultrasonic sensors hooked up to my arduino and am wondering how to separate each one in the "if" comments, i.e.: Right from Left and which device they turn on when something is in range. So far I have this: But I need to differentiate the first sensor form the second one. Any advice is appreciated.

digitalWrite(  
   
if (distance \>= maximumRange || distance \<= minimumRange){  
/\* Send a negative number to computer and Turn LED ON  
to indicate "out of range" _/_  
 _Serial.println("-1");  
 digitalWrite(leftledmosfet, HIGH);  
 digitalWrite(  
 }  
 else {  
 /_ Send the distance to the computer using Serial protocol, and  
turn LED OFF to indicate successful reading. \*/  
Serial.println(distance);  
digitalWrite(leftledmosfet, LOW);  
}

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [December 28, 2015, 8:24pm UTC](https://forum.arduino.cc/t/how-to-program-if-comments-to-discern-between-two-different-sensors/355122/2 "2015-12-28T20:24:34Z")

</div>

Welcome to the Forum. Please use code tags. To learn how, read the two posts  
[  
How to use this forum - please read.](http://forum.arduino.cc/index.php?topic=149014.0) and  
[Read this before posting a programming question ...](https://forum.arduino.cc/index.php?topic=97455.0)

The code tags make the code look

```cpp
like this

```

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.

Many questions can be answered by simply reading the documentation which is provided with the IDE, available under the help tab, or online [here](https://www.arduino.cc/en/Reference/HomePage#).

If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "\</\>" icon at the upper left hand corner. Click on the "Save" button.

There are many other things that programmers do to make their code understandable. Please do them, as a courtesy to the members who volunteer their time to help you here. One is to use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

---

<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:** [December 28, 2015, 9:20pm UTC](https://forum.arduino.cc/t/how-to-program-if-comments-to-discern-between-two-different-sensors/355122/3 "2015-12-28T21:20:04Z")

</div>

> Ian\_McElrath:  
> Hello everyone!
> 
> I'm finishing up some of my code this week and was hoping to ask a question. I have two separate Ultrasonic sensors hooked up to my arduino and am wondering how to separate each one in the "if" comments, i.e.: Right from Left and which device they turn on when something is in range. So far I have this: But I need to differentiate the first sensor form the second one. Any advice is appreciated.
> 
> digitalWrite(  
>    
> if (distance \>= maximumRange || distance \<= minimumRange){  
> /\* Send a negative number to computer and Turn LED ON  
> to indicate "out of range" _/_  
> _Serial.println("-1");  
> digitalWrite(leftledmosfet, HIGH);  
> digitalWrite(  
> }  
> else {  
> /_ Send the distance to the computer using Serial protocol, and  
> turn LED OFF to indicate successful reading. \*/  
> Serial.println(distance);  
> digitalWrite(leftledmosfet, LOW);  
> }

In pseudo code AKA verbal description of code

distance of Left sensor = read sensor pin  
distance of Right sensor = read sensor pin

assign variables - assuming analog values

int LeftSensor, RightSensor;

read sensors example

LeftSensor = analogRead( left sensor pin );  
RightSensor = analogRead(right sensor pin);

analyze example

if( analogRead( left sensor pin ) \< some value OR analogRead(right sensor pin) \> some value)  
{  
process code...

}

Essentially each sensor has its own assigned variable which can be independently analyzed.  
When more sensors are required using an array of variables can be advantageous, but the code than becomes more complex.

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [December 29, 2015, 9:54am UTC](https://forum.arduino.cc/t/how-to-program-if-comments-to-discern-between-two-different-sensors/355122/4 "2015-12-29T09:54:06Z")

</div>

You need something like @Vaclav has suggested (and he is here long enough to know to use code tags).

Something more like this

```cpp
int LeftSensorValue, RightSensorValue;

void loop() {
  LeftSensorValue = analogRead( left sensor pin );
  RightSensorValue = analogRead(right sensor pin);

  //  Note that the following line does not read the sensors
  if( LeftSensorValue < xxx OR RightSensorValue > yyy)
  {
      // do something

    }
}

```

By saving the values of the sensors you can use the same values in several different tests if necessary.

...R

---

<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:** [December 30, 2015, 4:55am UTC](https://forum.arduino.cc/t/how-to-program-if-comments-to-discern-between-two-different-sensors/355122/5 "2015-12-30T04:55:07Z")

</div>

Thank you everyone! I'm still a little bit confused though. Sorry, it takes me a little while to understand.

So I named my sensors:

L = leftultrasensortrigPin & left ultrasensorechoPin  
R = vice versa

Below are some incomplete portions of how my code is, but with some questions added.

```cpp
#leftultrasensortrigPin 6 // Trigger Pin
#leftultrasensorechoPin 7 // Echo Pin
#rightultrasensortrigPin 12 // Trigger Pin
#rightultrasensorechoPin 13 // Echo Pin

[question]The max range is universal for both sensors, but is this okay?[question]

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
pinMode(leftultransensortrigPin, OUTPUT);
pinMode(leftultrasensorechoPin, INPUT);
pinMode(rightultrasensortrigPin, OUTPUT); 
pinMode(rightultrasensorechoPin, INPUT);
}

void loop() {
  LeftSensorValue = analogRead( left sensor pin );  [question]What would I type for "LeftSensorValue" and "analogRead"[question]
  RightSensorValue = analogRead(right sensor pin);

  //  Note that the following line does not read the sensors
  if( LeftSensorValue < xxx OR RightSensorValue > yyy)
  {
      // do something

    }

```

---

<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:** [December 30, 2015, 9:02am UTC](https://forum.arduino.cc/t/how-to-program-if-comments-to-discern-between-two-different-sensors/355122/6 "2015-12-30T09:02:47Z")

</div>

```cpp
#leftultrasensortrigPin 6 // Trigger Pin
#leftultrasensorechoPin 7 // Echo Pin
#rightultrasensortrigPin 12 // Trigger Pin
#rightultrasensorechoPin 13 // Echo Pin

```

You are a bit mixed up here  
Either

```cpp
#define leftultrasensortrigPin 6 // Trigger Pin
etc

```

or

```cpp
const byte leftultrasensortrigPin = 6 // Trigger Pin
etc

```

Then you can use the defined names or variables in place of actual pun numbers in the program to make it more readable.

---

<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:** [December 31, 2015, 6:50am UTC](https://forum.arduino.cc/t/how-to-program-if-comments-to-discern-between-two-different-sensors/355122/7 "2015-12-31T06:50:38Z")

</div>

> UKHeliBob:
> 
> ```arduino
> #leftultrasensortrigPin 6 // Trigger Pin
> 
> ```

#leftultrasensorechoPin 7 // Echo Pin  
#rightultrasensortrigPin 12 // Trigger Pin  
#rightultrasensorechoPin 13 // Echo Pin

> ```arduino
> 
> You are a bit mixed up here
> Either
> 
> ```
> 
> #define leftultrasensortrigPin 6 // Trigger Pin  
> etc
> 
> ```arduino
> 
> or
> 
> ```
> 
> const byte leftultrasensortrigPin = 6 // Trigger Pin  
> etc
> 
> ```arduino
> 
> Then you can use the defined names or variables in place of actual pun numbers in the program to make it more readable.
> 
> ```

Thanks Bob! I forgot to put "#define" when I typed that up. Now for the void loop area, how can I list the pin names? 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:** [December 31, 2015, 7:22am UTC](https://forum.arduino.cc/t/how-to-program-if-comments-to-discern-between-two-different-sensors/355122/8 "2015-12-31T07:22:39Z")

</div>

> how can I list the pin names?

Sorry, but I don't understand the question. Can you please explain what you want to do in more detail.

---

<div class="post-metadata">

**Author:** ![PaulMurrayCbr](https://avatars.discourse-cdn.com/v4/letter/p/74df32/32.png) [@PaulMurrayCbr](https://forum.arduino.cc/u/PaulMurrayCbr)\
**Post date:** [December 31, 2015, 12:28pm UTC](https://forum.arduino.cc/t/how-to-program-if-comments-to-discern-between-two-different-sensors/355122/9 "2015-12-31T12:28:48Z")

</div>

What's complicated about this?

Using a const int or a define is no different to using a number.

If I go

```cpp
void loop() {
  int x = analogRead(0);
  if(x > 200) {
    do_something()
  }
}

```

It's no different to going

```cpp
const int THESPECIALVALUE = 200;

void loop() {
  int x = analogRead(0);
  if(x > THESPECIALVALUE) {
    do_something()
  }
}

```

The name of the variable - THESPECIALVALUE in this case - can be any combination of digits, letters, and underscores provided that it does not start with a digit and you don't use a name that's already used by something else. Anything you like.

There are conventions - variables should be camel case and starting with a lowercase letter - but they are not requirements.

So you do this

```cpp
void loop()
  int LeftSensorValue = analogRead(leftultrasensorechoPin); 
  int RightSensorValue = analogRead(rightultrasensorechoPin);

}

```

Great - you have read both sensors. You have the values in a pair of two variables. What were you hoping to do with these values, again?

Also, see this: [The XY problem](http://xyproblem.info/).

Because your problem statement doesn't make a lot of sense: "I have two separate Ultrasonic sensors hooked up to my arduino and am wondering how to separate each one in the "if" comments,".

Rather than talking about "separating" them (what? you want to separate sensors? Move them further apart! What? In code? put some blank lines in there!) in the "if" comments (What? You have an if? but it's a comment? It's _in_ a comment? There's a comment in the "if"?), tell us what your sketch is supposed to do.

---

<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:38pm UTC](https://forum.arduino.cc/t/how-to-program-if-comments-to-discern-between-two-different-sensors/355122/10 "2021-05-05T21:38:51Z")

</div>

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


