Hi,
I have a project that uses an Ultrasonic sensor and some Neopixel LED strips.
When something comes within the proximity of the Ultrasonic sensor it triggers an LED animation in the Neopixel strips. This is an animation I got from the FastLED Example folder.
What I want to happen is that if there is nothing in the Ultrasonic sensor's proximity that the LED animation completely stops, as it takes about a minute from start to finish.
I have an if statement, where if something is within the sensor's proximity the animation is triggered. But there is an "else" part where if there is nothing in the proximity it should go to black.
This method is working for other LED animations but not this one. I am posting the code below. If anyone has any ideas for how to remedy this please let me know.
// need to fix the if/else statement
// to turn off
// all LEDs when noting is sensed
#include "FastLED.h"
#define NUM_LEDS 660
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define DATA_PIN 6
//#define CLK_PIN 4
#define VOLTS 12
#define MAX_MA 500
// constants won't change
const int TRIG_PIN = 7; // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN = 8; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int LED_PIN = 6; // Arduino pin connected to LED's pin
const int DISTANCE_THRESHOLD = 100; // centimeters
// variables will change:
float duration_us, distance_cm;
CRGBArray<NUM_LEDS> leds;
CRGBPalette16 gCurrentPalette;
CRGBPalette16 gTargetPalette;
void setup() {
Serial.begin (9600); // initialize serial port
Serial.println("resetting");
pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode
pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
delay( 3000 ); //safety startup delay
FastLED.setMaxPowerInVoltsAndMilliamps( VOLTS, MAX_MA);
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(255);
// .setCorrection(TypicalLEDStrip);
}
void fadeall() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].nscale8(1000);
}
}
void loop()
{
// generate 10-microsecond pulse to TRIG pin
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// measure duration of pulse from ECHO pin
duration_us = pulseIn(ECHO_PIN, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us;
if (distance_cm < DISTANCE_THRESHOLD)
{
cylon();
}
else
{
// leds[0] = CRGB::Black;
// black();
// CRGB::Black;
fill_solid ( leds, 660, CRGB::Black);
FastLED.show();
delay(500);
}
}
void black()
{ for (int i = (NUM_LEDS) - 1; i >= 0; i++) {
// Set the i'th led to red
leds[i] = CRGB::Black;
// Show the leds
FastLED.show();
delay(500);
}
}
void cylon()
{
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);
}
}