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

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);
}

Welcome to the Forum. Please use code tags. To learn how, read the two posts

How to use this forum - please read.
and
Read this before posting a programming question ...

The code tags make the code look

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.

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.

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.

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

Something more like this

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

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.

#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

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

You are a bit mixed up here
Either

#define leftultrasensortrigPin 6 // Trigger Pin
etc

or

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.

UKHeliBob:

#leftultrasensortrigPin 6 // Trigger Pin

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



You are a bit mixed up here
Either


#define leftultrasensortrigPin 6 // Trigger Pin
etc



or


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.

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!

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.

What's complicated about this?

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

If I go

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

It's no different to going

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

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.

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.