What is the proper if statement to make the servo goes 180 and activates the red LED when the distance is less than 50 and goes zero and activates th

And activates the green LED?


My project is meant to be a water level sensor:

My code:

// C++ code




const int trigPin = 13;
const int echoPin = 12;
const int ServoPin = 4;
const int green = 3;
const int red = 4;
int distance;

// **************************************//
void setup()
{
 
 Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  srv.attach(4);
 
}

void loop() {
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(3);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(9);
  digitalWrite(trigPin, LOW);
  uint32_t distance = (pulseIn(echoPin, HIGH) * 17) / 50;
  Serial.print("distance: ");
  Serial.print(distance);
  Serial.println(" cm");
 
  if (distance <= 50); {
  digitalWrite(red, HIGH);
  srv.write(180);
} else {
digitalWrite(red, LOW);
  srv.write(0);
}
  if (distance >= 50); {
digitalWrite(green, HIGH);
    else {
  digitalWrite (green, LOW);
delay(3000);
}

My Arduino Uno:

Looks like there is some code missing.

1 Like

Oops

And again

The pin should already be an output, and it should already be LOW

1 Like

The editor says:

" In function 'void loop()':
53:3: error: 'else' without a previous 'if'
59:5: error: 'else' without a previous 'if'
62:5: error: expected '}' at end of input
62:5: error: expected '}' at end of input"

However I don't quite catch this.

An if with a semicolon at the end, as I highlighted, is your problem

// C++ code
const byte trigPin = 13;
const byte echoPin = 12;
const byte ServoPin = 7;
const byte green = 3;
const byte red = 4;

// **************************************//
void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  srv.attach(ServoPin);
}

void loop() {
  delayMicroseconds(3);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(9);
  digitalWrite(trigPin, LOW);
  uint32_t distance = (pulseIn(echoPin, HIGH) * 17) / 50;
  Serial.print("distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  if (distance < 50) {
    digitalWrite(red, HIGH);
    digitalWrite (green, LOW);
    srv.write(180);
  } else {
    digitalWrite(red, LOW);
    digitalWrite(green, HIGH);
    srv.write(0);
  }
  delay(3000);
}

Oops

not my oops

It's still an oops.
Like the fact that the trigger pin should already be an OUTPUT, and LOW

repaired. should i distance formula correcting?

Temperature correction?
Probably not worth it.

actually should be
uint32_t distance = (pulseIn(echoPin, HIGH) * 17) / 1000;

I changed connections in the Arduino and didn't proofread my code before posting it here :frowning:

Could you please explain the trigger part as if you were talking to an 8-year-old girl? I don't need answers, just guidance to find them myself.

if (distance < 50) {
  digitalWrite(red, HIGH);
  srv.write(180);
} else {
digitalWrite(red, LOW);
  srv.write(0);
}
  if (distance > 50) {
digitalWrite(green, HIGH);
  }else {
  digitalWrite (green, LOW);
delay(3000); 
  }
}
    

A colleague of mine sent me his code and it seems that he has written a code to test the servo as if it was the water triggering it to open and close it . Is it what my code is lacking?

Actually, I was just referring to the fact that your code as posted doesn't compile.

1 Like

The way you have it, you write the trigger pin low, then high, then low again.
But this is in a repeated loop, so the last thing you wrote in the loop function will be the condition it will be in when the loop function repeats.
So, the initial write to low (and associated delay) are pointless, provided you ensure the pin is low when setup exits

1 Like

If we re-arrange your above codes, we get the following: (Note that if() is not a function; so, it should not have a terminating semicolon.)

if (distance <= 50)
; 
{
  digitalWrite(red, HIGH);
  srv.write(180);
} 
else 
{
  digitalWrite(red, LOW);
  srv.write(0);
}

There is something in the above codes that does not match with if-else statement. What is this?

2 Likes

Could you please tell me if the wiring that I made is correct?