Hello everyone, I'm working on an Arduino project that involves adjusting the brightness of an LED projector based on the distance from an ultrasonic sensor. Sometimes, the setup works, but at other times, it stops working, and I'm not sure why. I wanted to know if anyone has any ideas to resolve this issue.
I've attached a photo of the setup and the code below.
Thanks in advance.
#include <afstandssensor.h>
AfstandsSensor afstandssensor(15, 16);
//Library IRremote
//int nb=0;
//int nn=0;
int nb;
uint8_t sCommand =0x10;
uint8_t sRepeats = 0;
int tempo=200;
int dist;
int distMax=120;
int distRef=distMax;
int pas=distMax/10;
#include <Arduino.h>
#define DISABLE_CODE_FOR_RECEIVER // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
//#define SEND_PWM_BY_TIMER // Disable carrier PWM generation in software and use (restricted) hardware PWM.
//#define USE_NO_SEND_PWM // Use no carrier PWM, just simulate an active low receiver signal. Overrides SEND_PWM_BY_TIMER definition
#include "PinDefinitionsAndMore.h" // Define macros for input and output pin etc.
#include <IRremote.hpp>
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(14,OUTPUT);
pinMode(15,OUTPUT);
digitalWrite(14,HIGH);
digitalWrite(17,LOW);
Serial.begin(115200);
// Just to know which program is running on my Arduino
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
/*
* The IR library setup. That's all!
*/
// IrSender.begin(); // Start with IR_SEND_PIN as send pin and if NO_LED_FEEDBACK_CODE is NOT defined, enable feedback LED at default feedback LED pin
IrSender.begin(DISABLE_LED_FEEDBACK); // Start with IR_SEND_PIN as send pin and disable feedback LED at default feedback LED pin
sCommand =0x04;//descente 0x05 spot 0x00 ruban
for(int i=0;i<100;i++){ /* Boucle pour baisser l'intensité au minimum */
IrSender.sendNEC(0x00, sCommand, sRepeats); //0x00 spot 0xEF00 ruban
delay(200);
sRepeats++;
// clip repeats at 4
if (sRepeats > 4) {
sRepeats = 4;
}
}
// Serial.print(F("Send IR signals at pin "));
// Serial.println(IR_SEND_PIN);
}
/*
* Set up the data to be sent.
* For most protocols, the data is build up with a constant 8 (or 16 byte) address
* and a variable 8 bit command.
* There are exceptions like Sony and Denon, which have 5 bit address.
*/
//uint8_t sCommand = 0x34;
void loop() {
Serial.print(dist);
dist=afstandssensor.afstandCM(); /* Récupération de la distance de détection */
nb=(distRef-dist)/pas-1; /* calcul de l'écat entre la ref et la nouvelle détection */
if (nb<0) /* On s'éloigne */
{ sCommand =0x04;//descente 0x04 spot 0x01 ruban
IrSender.sendNEC(0x00, sCommand, sRepeats); /* On diminue la lumière d'un pas */
distRef=distRef+pas; /* Calcul du nouvel écart */
}
else if (nb>0) /* on s'approche */
{
sCommand =0x05;//montée 0x04 spot 0x01 ruban
IrSender.sendNEC(0x00, sCommand, sRepeats); /* On augmente la lumière d'un pas */
distRef=distRef-pas; /* Calcul du nouvel écart */
}
delay(tempo); /* pause avant nouvelle mesure */
}
I'm reading from my iPhone so might be wrong but your math with distRef seems fishy
why don't you just map the expected brightness between 0% and 100% based on the distance between 0 and distMax and use the current brightness to decide how many pulses you need to send?
Thank you for your reply, here is the modified code.
#include <afstandssensor.h>
AfstandsSensor afstandssensor(15, 16);
//Library IRremote
//int nb=0;
//int nn=0;
int nb;
uint8_t sCommand = 0x10;
uint8_t sRepeats = 0;
int tempo = 200;
int dist;
int distMax = 120;
int distRef = distMax;
int pas = distMax / 10;
#include <Arduino.h>
#define DISABLE_CODE_FOR_RECEIVER // Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
//#define SEND_PWM_BY_TIMER // Disable carrier PWM generation in software and use (restricted) hardware PWM.
//#define USE_NO_SEND_PWM // Use no carrier PWM, just simulate an active low receiver signal. Overrides SEND_PWM_BY_TIMER definition
#include "PinDefinitionsAndMore.h" // Define macros for input and output pin etc.
#include <IRremote.hpp>
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);
digitalWrite(14, HIGH);
digitalWrite(17, LOW);
Serial.begin(115200);
// Just to know which program is running on my Arduino
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
/*
* The IR library setup. That's all!
*/
// IrSender.begin(); // Start with IR_SEND_PIN as send pin and if NO_LED_FEEDBACK_CODE is NOT defined, enable feedback LED at default feedback LED pin
IrSender.begin(DISABLE_LED_FEEDBACK); // Start with IR_SEND_PIN as send pin and disable feedback LED at default feedback LED pin
sCommand = 0x04; //descente 0x05 spot 0x00 ruban
for (int i = 0; i < 100; i++) { /* Boucle pour baisser l'intensité au minimum */
IrSender.sendNEC(0x00, sCommand, sRepeats); //0x00 spot 0xEF00 ruban
delay(200);
sRepeats++;
// clip repeats at 4
if (sRepeats > 4) {
sRepeats = 4;
}
}
// Serial.print(F("Send IR signals at pin "));
// Serial.println(IR_SEND_PIN);
}
/*
* Set up the data to be sent.
* For most protocols, the data is build up with a constant 8 (or 16 byte) address
* and a variable 8 bit command.
* There are exceptions like Sony and Denon, which have 5 bit address.
*/
//uint8_t sCommand = 0x34;
void loop() {
Serial.print(dist);
dist = afstandssensor.afstandCM(); /* Récupération de la distance de détection */
nb = (distRef - dist) / pas - 1; /* calcul de l'écat entre la ref et la nouvelle détection */
if (nb < 0) /* On s'éloigne */
{
sCommand = 0x04; //descente 0x04 spot 0x01 ruban
IrSender.sendNEC(0x00, sCommand, sRepeats); /* On diminue la lumière d'un pas */
distRef = distRef + pas; /* Calcul du nouvel écart */
} else if (nb > 0) /* on s'approche */
{
sCommand = 0x05; //montée 0x04 spot 0x01 ruban
IrSender.sendNEC(0x00, sCommand, sRepeats); /* On augmente la lumière d'un pas */
distRef = distRef - pas; /* Calcul du nouvel écart */
}
delay(tempo); /* pause avant nouvelle mesure */
}
Normally when the program starts the light intensity of the projector decreases to the maximum of the light intensity and when we approach the sensor it is supposed to increase or decrease if we move away. The problem is that it doesn't work and then, without me touching anything, the program starts working.
Is the sensor working as you expect. This will help you narrow down where the problem area is. I highly recommend you get that part working first. The sensor is not a precision device and spurious noise can and will mess with it.
You need to note sound will bounce off most surfaces, any surface change/position will change the results, even a human body can cause problems as it will probably adsorbed some of the sound.
Yes, sorry if it's not very clear, I speak English quite poorly. In fact, when the program turns on the light intensity is at its lowest and it increases as you get closer, but if you move away from it it decreases. The problem is that by leaving the program, most often, the light goes down to its minimum but if I approach the sensor it does not increase. And other times it works without me knowing why. I tested with another sensor to see if the problem was there but the sensor works. There is also no overheating problem.
The sensor works because sometimes the program works and my hand in front can vary the light intensity. But I just have to reset the program and it no longer works. Do you think I should use another more precise sensor? It's true that the project is supposed to work with the movement of a human body.
no it's just an spoken expression - when you say to the maximum ("au max") we interpret from the context and don't read that literally as a mathematical maximum. we see it as "all the way"
"diminuer au max" ➜ "decrease all the way"
if you are picky of course you can make a joke about it. (I would)