Water level indicator ultrasonic sensor

#include <HCSR04.h>
const int RGBPin = 2;
int triggerPin = 7;
int echoPin = 8;
int led_g = 12;
int led_b = 11;
int led_y = 10;
int led_r = 9;
int distance ;
int long duration ;

void setup() {
pinMode (triggerPin, OUTPUT) ;
pinMode (echoPin, INPUT) ;
pinMode (led_g, OUTPUT) ;
pinMode (led_b, OUTPUT) ;
pinMode (led_y, OUTPUT) ;
pinMode (led_r, OUTPUT) ;
pinMode (RGBPin, OUTPUT) ;
}

void loop() {
Serial.begin(9600);
digitalWrite (RGBPin, HIGH);
digitalWrite (triggerPin, HIGH);
delayMicroseconds(10) ;
digitalWrite (triggerPin, LOW);
delayMicroseconds(2) ;
digitalWrite (echoPin, HIGH);
duration = pulseIn(echoPin, HIGH) ;
distance = duration*0.0340/2 ;

digitalWrite (led_g, HIGH);
digitalWrite (led_b, HIGH);
digitalWrite (led_y, HIGH);
digitalWrite (led_r, HIGH);

if (distance > 20 ) {

}

digitalWrite (led_g, LOW);
digitalWrite (led_b, HIGH);
digitalWrite (led_y, HIGH);
digitalWrite (led_r, HIGH);

if (distance > 15 ) ;
(distance < 20 ) ;{

}
digitalWrite (led_g, LOW);
digitalWrite (led_b, LOW);
digitalWrite (led_y, HIGH);
digitalWrite (led_r, HIGH);

if (distance > 10 ) ;
(distance < 15 ) ;{

}
digitalWrite (led_g, LOW);
digitalWrite (led_b, LOW);
digitalWrite (led_y, LOW);
digitalWrite (led_r, HIGH);
if (distance > 5 ) ;
(distance < 10 ) ;{

}

digitalWrite (led_g, LOW);
digitalWrite (led_b, LOW);
digitalWrite (led_b, LOW);
digitalWrite (led_r, LOW);
if (distance < 5 ) ;{

}
}

Please edit your opening post, select all code and click the <CODE/> button; next save your post. This will apply code tags to the code which makes it easier to read and easier to copy and the forum software will display it correctly.


Is there any problem or are you just sharing?

what should the code do? what does it do?

Led are not lighting

Led are not lighting with ultrasonic sensor

Ok, got it, but please do what we already asked you, edit your first post to enclose the sketch inside "code" tags (select the lines and click on the <CODE/> button).

it's not matter of aesthetics but it lets you (and us!) better read your code.
And if you have/post a correctly indented the code (use Ctrl-T command from the IDE to do it for you!), you probabily be able to see what's wrong with it (I already found it, but I think it's better for you to find the problem yourself and learn something more about C programming...) :wink:

Do it now, please. Thanks.

Just as an example, this code is completely wrong:

  if (distance > 20 ) {

  }
  if (distance > 15 ) ;
  (distance < 20 ) ;{

  }

Do what I asked you, then we'll get right on the C programming basics.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.