Smooth increase/deacrese fading with ultrasonic sensor

Hi everyone,

I start in arduino and I currently have a little problem and i come to ask some help :

This assembly consists of an arduino, an ultrasonic sensor and a led.

when I approach the sensor, I would like the latter gradually illuminates the led, but when I leave the sensor field rather than suddenly turn off the leds, it begins a fade out.

To that I would like to add, when entering the sensor field, a fade recovery by rising from the value where it is based on the distance detected in the field of the sensor.

Actually this is my code :

#define trigPin 7
#define echoPin 6
#define pinLed 9


void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(pinLed, OUTPUT);
 
}

void loop() {
  long duration, distance, distance2, fadeValue;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(1);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(2);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

 if (distance <= 50) 
  {
  distance2 = 50 - distance; 

  fadeValue = map(distance2, 0, 50, 0, 254);

    analogWrite(pinLed, fadeValue);  // Writes the fadeValue to pin 9 
   } 
   else
   {
    for (int fadeValueout = fadeValue; fadeValueout >= 0; fadeValueout -= 5) {
    analogWrite(pinLed, fadeValueout);
    delay(100);
  }

   }  
  delay(100);
}

currently the detector works, and lights the LED depending on the distance (but led is to quickly at her max brightness). But on the other hand, my fadeout code does not work (led just turn off), and I do not have much idea about how to code the fade in recovery on an existing value of the fade out.

Can you help me ?

Consider setting your project up similar to this tutorial.

Thx for this tutorial, but i'm lost with it...

Info : i use a HC-SR04 sensor.

Claymenia:
Thx for this tutorial, but i'm lost with it...

Info : i use a HC-SR04 sensor.

I have modified this source to output similar to the tutorial.

/**
 * HC-SR04 Demo
 * Demonstration of the HC-SR04 Ultrasonic Sensor
 * Date: August 3, 2016
 * 
 * Description:
 *  Connect the ultrasonic sensor to the Arduino as per the
 *  hardware connections below. Run the sketch and open a serial
 *  monitor. The distance read from the sensor will be displayed
 *  in centimeters and inches.
 * 
 * Hardware Connections:
 *  Arduino | HC-SR04 
 *  -------------------
 *    5V    |   VCC     
 *    7     |   Trig     
 *    8     |   Echo     
 *    GND   |   GND
 *  
 * License:
 *  Public Domain
 *  
 * added LED output
 * 
 * Hardware Connections:
 *  Arduino | For LED 
 *  -------------------
 *    9     |   220 ohm resistor     
 *          |   The long, positive leg (the anode) of the LED should be connected to the output from the resistor
 *    GND   |   The shorter, negative leg (the cathode) connected to ground.
 */

// Pins
const int TRIG_PIN = 7;
const int ECHO_PIN = 8;
const int analogOutPin = 9;// added LED

// Anything over 400 cm (23200 us pulse) is "out of range"
const unsigned int MAX_DIST = 23200;

void setup() {

  // The Trigger pin will tell the sensor to range find
  pinMode(TRIG_PIN, OUTPUT);
  digitalWrite(TRIG_PIN, LOW);

  // We'll use the serial monitor to view the sensor output
  Serial.begin(9600);
}

void loop() {

  unsigned long t1;
  unsigned long t2;
  unsigned long pulse_width;
  float cm;
  float inches;

  // Hold the trigger pin high for at least 10 us
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Wait for pulse on echo pin
  while ( digitalRead(ECHO_PIN) == 0 );

  // Measure how long the echo pin was held high (pulse width)
  // Note: the micros() counter will overflow after ~70 min
  t1 = micros();
  while ( digitalRead(ECHO_PIN) == 1);
  t2 = micros();
  pulse_width = t2 - t1;

  // Calculate distance in centimeters and inches. The constants
  // are found in the datasheet, and calculated from the assumed speed 
  //of sound in air at sea level (~340 m/s).
  cm = pulse_width / 58.0;
  inches = pulse_width / 148.0;

  // Print out results
  if ( pulse_width > MAX_DIST ) {
    Serial.println("Out of range");
    analogWrite(analogOutPin, 0);// turn off LED
  } else {
    unsigned int inverseSensorValue = MAX_DIST - pulse_width;// invert because 0 = full bright and 23200 = off
    Serial.print(cm);
    Serial.print(" cm \t");
    Serial.print(inches);
    Serial.println(" in");
    outputValue = map(inverseSensorValue, 0, MAX_DIST, 0, 255);// from led tutorial
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);set brightness
  }
  
  // Wait at least 60ms before next measurement
  delay(60);
}

I'm so sorry i'm in travel, i test this soon and come back :slight_smile: thx !