SR04 to control LED Strips

I am trying to control an LED strip using an HC-SR04 proximity sensor. I am looking to have the distance that the proximity sensor reads control the effects of the LED strip. Aka, I want the distance of my hand from the proximity sensor to control parameters (brightness, fade, LED library, etc) of the LED Strips.

Is there anyone who has done something similar to this and could assist me in the coding of this project?

First thing to do is break this into individual steps/behaviors...

So start off with getting the prox sensor working... and outputting specific 'values' to the serial monitor.

I would image after that.. the rest is fairly easy?

Just a bunch of IF (maybe switch/case) statements that check the returned value against a threshold you declare as a new 'animation/pattern' effect on the strip of choice.

What type of LED strip are you talking about here? Like an individually addressable Neopixel/Dotstar led strip?

A single color led strip?

An RGB non-addressable led strip?

How many more permutations of this question can we expect?
(I deleted the duplicates)

I have a SR04 sensor and WS2813 LED strip. I have used each of these products individually with success.

I have the serial monitor running and I can observe the readings from my proximity sensor. I have a solid understanding of the code up until the point where I am using the distance readings as "cues/indicators" for the IF statements.

The part that I am stuck on is how I can use the distance (<=1, <=10 && > 20, etc) to generate change in the LED strip (whether it be fade, change of direction, a code for a new pattern, etc).

Starting at an easy parameter like brightness might be smart. Then I can expand from there.

Do I use analogWrite or digitalWrite (i think digital?) for the LED strip?

Right now, I have the LED strip connected to the digital i/o 13 (both trigger and echo as one pin).

This is the code that I have thus far. I am generating a reading in my serial monitor from the proximity sensor, but the LED strip is not responding to any readings > 40..

#include <FastLED.h>
#include <NewPing.h>

#define DATA_PIN 7
#define CLOCK_PIN 8
#define NUM_LEDS 151
#define PING_PIN  12  // Arduino pin tied to both trigger and echo pins on the ultrasonic sensor.
#define MAX_DISTANCE 120 // Maximum distance we want to ping for (in centimeters). Maximum sensor dista
NewPing sonar(PING_PIN, PING_PIN, MAX_DISTANCE); 

// Define the array of leds
CRGB leds[NUM_LEDS];


void setup() { 
  Serial.begin(9600);
  Serial.println("resetting");
  LEDS.addLeds<WS2813,PING_PIN,RGB>(leds,NUM_LEDS);
  LEDS.setBrightness(84);
}

void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } } 


void loop() {
  LEDS.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
  LEDS.setBrightness(84);
  float distance = digitalRead(PING_PIN);
  delay(30);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("distance: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
  delay(500);

 if(distance > 40) {
  static uint8_t hue = 0;
  Serial.print("x");
  // First slide the led in one direction
  for(int i = 0; i < NUM_LEDS; i++) 
  {
    // Set the i'th led to red 
    leds[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show(); 
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again
    delay(10);
  }
  Serial.print("x");

  // Now go in the other direction.  
  for(int i = (NUM_LEDS)-1; i >= 0; i--) 
  {
    // Set the i'th led to red 
    leds[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again
    delay(10);
  }
 }

}

Moderator editor: the usual. {Sigh}

Couple things:

  • You mention this: "WS2813 LED strip" but your code references "WS2812"
  • You state this: " I have used each of these products individually with success."... but then go on to ask: "Do I use analogWrite or digitalWrite (i think digital?) for the LED strip?" (especially if you have already had success in using this led strip? use what you did previously that was successful........no?)

I'm not sure what the difference is between the: WS2813 vs WS2812's?

Maybe check out the Adafruit tutorials on the Neopixel stuff/led strips...

They walk you through how to do non-blocking code that cycles through different animations/patterns with a momentary button press..

Seems tailor made for you..except you swap out the checking of the switch...with your sensor.