Hello,
I am a starting user with Arduino. I am working on a LED project en hopefully someone can help me ?
I've got a standard Philips RGB ledstrip 12V and a HC-SR04 ultrasonic sensor. I want to control the brightness of the ledstrip with the distance. When the sensor is close to the wall, there must be no light. If i move the sensor further and further from the wall, the light should be stronger.
My question is, can someone help me with the code ? and if someone has a simple circuit for this project ? I would be very grateful.
Thank you
So what have you tried?
First write the code and test it so that you can control the brightness of the LED strip. You will need a FET to switch the strip and you put a PWM signal on the gate. By changing the PWM duty cycle with analogWrite that will change the brightness. You are aiming to use a final value between 0 and 255.
Then wire up your ultrasonic sensor and get it to print out the reading it gives you.
Finally use the output number from the sensor to generate a number to send to the analogWrite function. To make these numbers align up use the map function, look up how that works.
This is how my code and electrical circuit looks like. But the sensor is not dimming/fading the ledstrip. Do you or someone know what the problem is ? Hopefully someone could help me with this.
#include <NewPing.h>
#define TRIGGER_PIN 13 // Arduino pin verbonden met trigger pin op de ultrasonic sensor.
#define ECHO_PIN 12 // Arduino pin verbonden met echo pin op de ultrasonic sensor.
#define MAX_DISTANCE 250 // Maximale afstand die ik wil meten (in centimeters).
#define REDPIN 5 // Rood van de rgb strip
#define GREENPIN 6 // Groen van de rgb strip
#define BLUEPIN 3 // Blauw van de rgb strip
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
long cm;
long old_cm = cm;
long z;
long old_b;
long new_b;
void setup() {
Serial.begin(9600); // Open serial monitor op 9600 baud voor resultaat.
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
delay(200); // wacht 75ms tussen metingen (ong. 15 pings/sec).
unsigned int uS = sonar.ping(); // Stuur meting, ontvangst meting in microseconde(uS).
Serial.print("afstand: ");
Serial.print(sonar.convert_cm(uS)); // Zet meettijd om in afstand en print resultaat (0 = maximale afstands bereik, geen meet resultaat)
// Serial.println("cm");
//Serial.print(new_b);
//Serial.println(": helderheid");
//Serial.print(cm);
//Serial.println(": cm");
if (cm < 16) {
cm = 0;
Serial.println(" 0");
} else
if (cm < 20) {
Serial.println(" 1");
cm = 20;
} else
if (cm < 30) {
Serial.println(" 2");
cm = 30;
} else
if (cm < 40) {
Serial.println(" 3");
cm = 40;
} else
if (cm < 50) {
Serial.println(" 4");
cm = 50;
} else
if (cm < 60) {
Serial.println(" 5");
cm = 60;
} else
if (cm < 70) {
Serial.println(" 6");
cm = 70;
} else
if (cm < 80) {
Serial.println(" 7");
cm = 80;
} else
if (cm < 90) {
Serial.println(" 8");
cm = 90;
} else
if (cm < 100) {
Serial.println(" 9");
cm = 100;
} else
if (cm < 110) {
Serial.println(" 10");
cm = 110;
} else
if (cm < 120) {
Serial.println(" 11");
cm = 120;
} else
if (cm < 130) {
Serial.println(" 12");
cm = 130;
} else
if (cm < 140) {
Serial.println(" 13");
cm = 140;
} else
if (cm < 150) {
Serial.println(" 14");
cm = 150;
} else
if (cm < 160) {
Serial.println(" 15");
cm = 160;
} else
if (cm < 170) {
Serial.println(" 16");
cm = 170;
} else
if (cm < 180) {
Serial.println(" 17");
cm = 180;
} else
if (cm < 190) {
Serial.println(" 18");
cm = 190;
} else
{ Serial.println(" 19");
cm = 200;
}
new_b = map(cm, 20, 200, 255, 0);
old_b = map(old_cm, 20, 200, 255, 0);
z = sonar.convert_cm(uS)*2;
Serial.print(z);
if (old_b <= new_b){
for (long z = old_b; z < new_b; z++) {
analogWrite(REDPIN, z);
analogWrite(BLUEPIN, 255);
analogWrite(GREENPIN, 255);
delay(2);
}
}
else {
for (long z = old_b; z > new_b; z--) {
analogWrite(REDPIN, z);
analogWrite(BLUEPIN, z);
analogWrite(GREENPIN, z);
delay(2);
}
}
old_cm = cm;
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
In this code:
Afstand = Distance
Helderheid = Brightness
1 Like
Please edit that post and put the code in code tags.
Where is the circuit?
What results do you get when you run that code?
This is the circuit !
When i use this circuit with the code above, the led does not fade but give light in one single color.
The color of the led is not important but the controlling of the brightness.
The word fade is a bit vague. You are only allowing 2mS per step so do you think you will see that?
Why not just output the result of the map function to the one LED colour you are intersted in? Use print statements to see the values you are inputting into the analogue write are the ones you think you are using.
Do you know that the variable z that is used inside the for loop is a totally diffrent variable than the one you use outside it?
How does that loop respond to the distance getting larger as well as getting smaller. Hint - it dosn't.
I really don't get it ! now i didn't use any z-value. My sensor gives the distance but no pulse. it gives a brightness value of 288 repeatedly ???

Why have you commented out all of that function?
to try something differant, because i don't get it!!I don't know what i have to change or add to the code ?
Well if you want help you are going to have to post your code using code tags so that other people can try your code and see what is going on. Especially as you don't seem willing / able to cooperate with the suggestions made so far -:
Grumpy_Mike:
The word fade is a bit vague. You are only allowing 2mS per step so do you think you will see that?
Why not just output the result of the map function to the one LED colour you are interested in? Use print statements to see the values you are inputting into the analogue write are the ones you think you are using.
#include <NewPing.h>
#define TRIGGER_PIN 13 // Arduino pin verbonden met trigger pin op de ultrasonic sensor.
#define ECHO_PIN 12 // Arduino pin verbonden met echo pin op de ultrasonic sensor.
#define MAX_DISTANCE 250 // Maximale afstand die ik wil meten (in centimeters).
#define REDPIN 5 // Rood van de rgb strip
#define GREENPIN 6 // Groen van de rgb strip
#define BLUEPIN 3 // Blauw van de rgb strip
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
long cm;
long old_cm = cm;
long z;
long old_b;
long new_b;
void setup() {
Serial.begin(9600); // Open serial monitor op 9600 baud voor resultaat.
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
delay(200); // wacht 75ms tussen metingen (ong. 15 pings/sec).
unsigned int uS = sonar.ping(); // Stuur meting, ontvangst meting in microseconde(uS).
Serial.print("afstand: ");
Serial.print(sonar.convert_cm(uS)); // Zet meettijd om in afstand en print resultaat (0 = maximale afstands bereik, geen meet resultaat)
// Serial.println("cm");
//Serial.print(new_b);
//Serial.println(": helderheid");
//Serial.print(cm);
//Serial.println(": cm");
if (cm < 16) {
cm = 0;
Serial.println(" 0");
} else
if (cm < 20) {
Serial.println(" 1");
cm = 20;
} else
if (cm < 30) {
Serial.println(" 2");
cm = 30;
} else
if (cm < 40) {
Serial.println(" 3");
cm = 40;
} else
if (cm < 50) {
Serial.println(" 4");
cm = 50;
} else
if (cm < 60) {
Serial.println(" 5");
cm = 60;
} else
if (cm < 70) {
Serial.println(" 6");
cm = 70;
} else
if (cm < 80) {
Serial.println(" 7");
cm = 80;
} else
if (cm < 90) {
Serial.println(" 8");
cm = 90;
} else
if (cm < 100) {
Serial.println(" 9");
cm = 100;
} else
if (cm < 110) {
Serial.println(" 10");
cm = 110;
} else
if (cm < 120) {
Serial.println(" 11");
cm = 120;
} else
if (cm < 130) {
Serial.println(" 12");
cm = 130;
} else
if (cm < 140) {
Serial.println(" 13");
cm = 140;
} else
if (cm < 150) {
Serial.println(" 14");
cm = 150;
} else
if (cm < 160) {
Serial.println(" 15");
cm = 160;
} else
if (cm < 170) {
Serial.println(" 16");
cm = 170;
} else
if (cm < 180) {
Serial.println(" 17");
cm = 180;
} else
if (cm < 190) {
Serial.println(" 18");
cm = 190;
} else
{ Serial.println(" 19");
cm = 200;
}
new_b = map(cm, 20, 200, 255, 0);
old_b = map(old_cm, 20, 200, 255, 0);
z = sonar.convert_cm(uS)*2;
Serial.print(z);
if (old_b <= new_b){
for (long z = old_b; z < new_b; z++) {
analogWrite(REDPIN, z);
analogWrite(BLUEPIN, 255);
analogWrite(GREENPIN, 255);
delay(2);
}
}
else {
for (long z = old_b; z > new_b; z--) {
analogWrite(REDPIN, z);
analogWrite(BLUEPIN, z);
analogWrite(GREENPIN, z);
delay(2);
}
}
old_cm = cm;
}
Two codes both different!
No sign of doing what I asked!
No hint of what it does.
From this end it looks like you are yanking my chain.
However, you never set the value of the variable cm so I am surprised you say that this works.