Controlling LED Fade Speed with HC SR 04

Dear Community,

i hope somebody can help me with this (as i didn’t found something on the forum that did): I’m fading an LED with the cosinus function. As input i have a HC SR 04 ultrasonic sensor. I want the LED to fade faster when the object is distant from the sensor and slower when the object is closer. So far i kind of managed but when the object gets closer to the sensor and the LED fades slower it also starts flickering. I figured out it must be because the constant input of the sensor doesn’t allow teh LED to complete one fade-cycle (“valueR” of red LED from 0 to 255 and back to 0). So i tried modifying the code so that the LED only takes a new Value from the ultrasonic sensor when the “valueR” is back to 0 and starts a new fade-cycle only using ONE value from the sensor and only using a new one when a new fade-cycle begins. But it still doesn’t work… Does anybody have a tip on how to use only one Value for one fade-cycle? I would be very happy about any help! :slight_smile:

Thank you in advance!

Here’s the code i’m using:

// http://playground.arduino.cc/Code/NewPing
#include <NewPing.h>
 
#define TRIG_PIN 5
#define ECHO_PIN 11
#define MAX_DIST 50

int RED = 3;
int valueR;

long time=0;
int periode = 2000;
 
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DIST); // NewPing initialisieren

int inputPin = A0;


 
void setup() {
 Serial.begin(9600);
 pinMode(RED, OUTPUT);
 }

 
void loop() {

  
 


  analogWrite(RED, valueR);
  valueR = 0;


 
 //delay(10);
 unsigned int distanceCM = sonar.ping_cm();
 //Serial.print(distanceCM);
 //Serial.println("cm");
 //sonar.ping_median(3);

 
 if (distanceCM <= 5) {
  distanceCM = distanceCM + MAX_DIST;
 }

 float VAR = map(distanceCM, 0, 50, 7500, 100);

 int VALUE;





if (valueR >= 250) {
  VALUE = VAR;
}

if (VALUE == 0) {
  VALUE = VAR;
}


 time = millis();
 valueR = 128+127*cos(2*PI/(VALUE)*time);

 analogWrite(RED, valueR);


}

Any reason you want to use a sin() for fading? Calculating the sin live on the Arduino is slow and a sin looks terrible to the eye...

And why do you do two analogWrite()'s?

  valueR = 0;
//[...]
if (valueR >= 250) {
  VALUE = VAR;
}

Do you ever expect that to be true?

And because you just jump around in that sin because you use a scaled version of millis(). For the fading part you might want to look at my library :slight_smile:

I use the sin because before i had the standard fading code from the arduino reference and the fade of the sin looked smoother to me.

The first digital write was to set valueR to 0 at the beginning of the void loop so that the if-condititon becomes true and a value from the ultrasonic sensor is used to calculate the sin.

Yes, i expect that to be true as i thought the valueR (= brightness of the red LED) would start from 0, increase until it reaches 255 and then decrease to 0 again. So that at the end of the fade, when the light is the brightest, a new value from the ultrasonic sensor is used to calculate the next fade 0.

Thank you! I will have a look at your library!

Sine should look even more terrible than linear. What you want is gamma corrected. :slight_smile:

Thank you for the tip with gamma correction, i’ll check it out!