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}