HC-SR04 RGB LED Controller with Sensor

Hello everyone, nice to meet you.
I am a student
I need to understand why this code doesn't work.
I want to control the LED light with the HC-SR04 sensor.
Could I talk to someone about this problem please?

//libreria ultrasonic
#include <Ultrasonic.h>
Ultrasonic ultrasonic(4,2);//declarar trigger y echo
//Sensor HC-SR04
int trigger = 4;
int echo = 2;

int distancia;
long tiempo;




//Led RGB
int blue=9;
int green=10;
int red=11;





void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
//Led
pinMode(red,OUTPUT);
pinMode(blue,OUTPUT);
pinMode(green,OUTPUT);

//Sensor HC-SR04
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  distancia = ultrasonic.read();
  Serial.print("Distancia en cm:");
  Serial.println(distancia);
  delay(1000);


digitalWrite(trigger,LOW);
delay(1000);
digitalWrite(trigger, HIGH);
delay(200);
digitalWrite(trigger,LOW);



//definir dos variable para capturar tiempo y distancia


if (distancia < 15){
  analogWrite(blue, 0);
  analogWrite(red, 125);
  delay(2000);
  analogWrite(red, 0);
  analogWrite(green, 0);
  
}

if (distancia > 15 ){
  analogWrite(blue, 0);
  analogWrite(red, 0);
  delay(1000);
  analogWrite(green, 100);
  delay(1000);
  
}

if (distancia > 40){
  analogWrite(blue, 0);
  analogWrite(red, 0);
  analogWrite(green, 200);
  delay(1000);
  
}

switch (distancia){
  case 0:
  analogWrite(blue, 0);
  analogWrite(red, 125);
  delay(2000);
  analogWrite(red, 0);
  analogWrite(green, 0);
  break;

  case 1:
  analogWrite(blue, 0);
  analogWrite(red, 0);
  delay(1000);
  analogWrite(green, 100);
  delay(1000);
  break;

  case 2:
  analogWrite(blue, 0);
  analogWrite(red, 0);
  analogWrite(green, 200);
  delay(1000);
  break;



}



}

You started a topic in the Uncategorised category of the forum when its description explicitly tells you not to

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

So, what it does or does not to be "not working".
What you get on serial monitor?
Also what board you have and how is your wiring?

1 Like

Hello, I have checked the ports and connections and they are correct.
I have changed my code
But I cannot get the condition for the green LED to turn on
Thank you for your information

//HC - SR04 con librerĂ­a NrePing
#include <NewPing.h>
NewPing newPing(4,2,60);
//Sensor HC-SR04
int trigger = 4;
int echo = 2;

int maxdistanciasensor= 60;
int distancia;
long tiempo;

//Led RGB
int blue=9;
int green=10;
int red=11;

void setup() {
  Serial.begin(9600);
  //Sensor HC-SR04
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);

//Led
pinMode(red,OUTPUT);
pinMode(blue,OUTPUT);
pinMode(green,OUTPUT);
  


}

void loop() {
  delay(50);
  distancia = newPing.ping_cm();
  Serial.print("Distancia en cm:");
  Serial.println(distancia);
  delay(1000);

 



  if (distancia < 15 ){
    analogWrite(red, 125);
  delay(2000);
  analogWrite(green, 0);
  delay(0);
  analogWrite(blue, 0);
  delay(0);
  }

  else if (distancia > 15 ){
    analogWrite(red, 0);
  delay(0);
  analogWrite(green, 0);
  delay(0);
  analogWrite(blue, 120);
  delay(2000);
  }

  else if (distancia > 40 ) {
  analogWrite(red, 0);
  delay(0);
  analogWrite(green, 180);
  delay(2000);
  analogWrite(blue, 0);
  delay(0);
  }

  

  


}

There are a lot of unecessary delays in your code and the conditions need to be adjusted. Try the version below:

//HC - SR04 con librerĂ­a NrePing
#include <NewPing.h>
NewPing newPing(4,2,60);
//Sensor HC-SR04
int trigger = 4;
int echo = 2;

int maxdistanciasensor= 60;
int distancia;
long tiempo;

//Led RGB
int blue=9;
int green=10;
int red=11;

void setup() {
  Serial.begin(9600);
  //Sensor HC-SR04
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);

//Led
pinMode(red,OUTPUT);
pinMode(blue,OUTPUT);
pinMode(green,OUTPUT);
}

void loop() {
  delay(50);
  distancia = newPing.ping_cm();
  Serial.print("Distancia en cm:");
  Serial.println(distancia);

  if (distancia <= 15 ){
  analogWrite(red, 125);
  analogWrite(green, 0);
  analogWrite(blue, 0);
  }

  else if (distancia > 15 && distancia <=40){
  analogWrite(red, 0);
  analogWrite(green, 0);
  analogWrite(blue, 120);
  }

  else if (distancia > 40 ) {
  analogWrite(red, 0);
  analogWrite(green, 180);
  analogWrite(blue, 0);
  }
}

PS1: as you're configuring the maximun distance in 60cm, everything thereafter will be considered "zero" and arise a red light.

PS2: you don't need this block:

int trigger = 4;
int echo = 2;
int maxdistanciasensor= 60;

if you´re calling this line before it:

1 Like

40 is greater than 15, so when the HC-SR04 return is "41" you will have GREEN or BLUE (because the compiler might put green after blue). Change...

  else if (distancia > 15) {

to

  else if (distancia >= 15 && distancia <= 40) {
1 Like

Hello Brazilino,
Your opinion has helped me understand the problem a little more, I hope you have also been able to learn something about it.
thank you so much, :hugs:

Now I understand it better.
Thank you very much, nice to meet you.

Very good that your project is working now. : )

When you begin a topic, describing how the sketch works (or does not work) is very important in solving the problem. "doesn't work" must be more descriptive.

1 Like

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