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:
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
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!
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.
Hey guys. I keep getting the following error message when attempting this with a practice code
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:
#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);
}
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.
if (righttrigPindistance < 4) { // This is where the LED On/Off happens
You have not defined a variable named "righttrigPindistance"
jremington:
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.
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!