controlling Addressable leds with HC-SR04

Hi I am trying to make this colorWipe

#include "FastLED.h"
#define NUM_LEDS 6
CRGB leds[NUM_LEDS];
#define PIN 6

void setup()
{
FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
}

// *** REPLACE FROM HERE ***
void loop() {
colorWipe(0x00,0xff,0x00, 50);
colorWipe(0x00,0x00,0x00, 50);
}

void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
for(uint16_t i=0; i<NUM_LEDS; i++) {
setPixel(i, red, green, blue);
showStrip();
delay(SpeedDelay);
}
// *** REPLACE TO HERE ***

void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}

void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}

go faster the closer i am to the Hc-SR04 I have the colorWipe sketch working and I have this sketch for the HC-SR04

// Turning NeoPixels on and off using a HC-SRO4 Ping Sensor

/*
This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.
The Arduino then takes this information and illuminates a strip of
NeoPixel's based on the distance of the object from the sensor.
This code was developed partially from Ping))) code found in the public domain
written by David A. Mellis, and adapted to the HC-SRO4 by Tautvidas Sipavicius,
while other portions were written by Charles Gantt and Curtis Gauger from
http://www.themakersworkbench.com.
*/

//Tell the Arduino IDE to include the FastLED library
#include <FastLED.h>

//Setup the variables for the HC-SR04
const int trigPin = 8;
const int echoPin = 7;

//Setup the variables for the NeoPixel Strip
#define NUM_LEDS 7 // How many leds in your strip?
#define DATA_PIN 6 // What pin is the NeoPixel's data line connected to?
CRGB leds[NUM_LEDS]; // Define the array of leds

void setup() {
// initialize serial communication:
Serial.begin(9600);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

if (cm <= 40) {fill_solid( &(leds[0]), NUM_LEDS /number of leds/, CRGB::Green); //{whitestrobe(30);
FastLED.show();

}
else if (cm >= 41) {fill_solid( &(leds[0]), NUM_LEDS /number of leds/, CRGB::Black);
FastLED.show();
}

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

how can I combine the tow?

void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  for(uint16_t i=0; i<NUM_LEDS; i++) {
      setPixel(i, red, green, blue); 
      showStrip();
      delay(SpeedDelay); 
  }

The speed of the colour wipe depends on the value of SpeedDelay passed to the function. You could make that value dependant on the distance returned by the sensor. However, as written the speed will only change once the colour wipe is finished. Is that acceptable ?

thanks for the fast reply yes that should work I think sorry for the noob question but how would i make the speedDelay dependent on distance?

What range of distances do you want to react to ?

From 300cm getting faster as I get closer

From 300cm to 20cm getting faster as I get closer

OK. The simple but clumsy way to do this is to use discrete speed bands. For instance if you wanted 3 bands then you could do something like

if (distance < 100)
{
  SpeedDelay = 50;
}
else
if (distance < 200)
{
  SpeedDelay = 100; 
}
else
if (distance < 300)
{
  SpeedDelay = 200; 
}

Adjust the values and number of bands to suit your requirements

A smarter way would be to use the map() function to derive a speed delay proportional to the distance. Something like

SpeedDelay = map(distance, 0, 300, 100, 300);  //input 0 to 100, output 100 to 300

thanks I now have this // Turning NeoPixels on and off using a HC-SRO4 Ping Sensor

/*
This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.
The Arduino then takes this information and illuminates a strip of
NeoPixel's based on the distance of the object from the sensor.
This code was developed partially from Ping))) code found in the public domain
written by David A. Mellis, and adapted to the HC-SRO4 by Tautvidas Sipavicius,
while other portions were written by Charles Gantt and Curtis Gauger from
http://www.themakersworkbench.com.
*/

//Tell the Arduino IDE to include the FastLED library
#include <FastLED.h>

//Setup the variables for the HC-SR04
const int trigPin = 8;
const int echoPin = 7;

//Setup the variables for the NeoPixel Strip
#define NUM_LEDS 7 // How many leds in your strip?
#define PIN 6 // What pin is the NeoPixel's data line connected to?
CRGB leds[NUM_LEDS]; // Define the array of leds

void setup() {
// initialize serial communication:
Serial.begin(9600);
FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

colorWipe(0x00,0xff,0x00, 50);
colorWipe(0x00,0x00,0x00, 50);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

void colorWipe(byte red, byte green, byte blue, int SpeedDelay=map(distance, 0, 300, 100, 300); }
for(uint16_t i=0; i<NUM_LEDS; i++) {
setPixel(i, red, green, blue);
showStrip();
);
}

}
0

void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}

void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}

but am getting colorWipe was not declared in this scope

Check the colorWipe() function definition

Here is the start of it

void colorWipe(byte red, byte green, byte blue, int SpeedDelay = map(distance, 0, 300, 100, 300);
}
for (uint16_t i = 0; i < NUM_LEDS; i++)
{

Does it look like the start of any other function definition ?

yes colorWipe(0x00,0xff,0x00, 50);
colorWipe(0x00,0x00,0x00, 50);

do I not need that any more? if I remove it I get distance was not declared in this scope

sorry for the noob questions I know I am probable missing something really simple

I have moved void colorWipe(byte red, byte green, byte blue, int SpeedDelay = map(distance, 0, 300, 100, 300); {
for (uint16_t i = 0; i < NUM_LEDS; i++)
setPixel(i, red, green, blue);
showStrip();
delay(SpeedDelay);
}
}

above

colorWipe(0x00, 0xff, 0x00, 300);
colorWipe(0x00, 0x00, 0x00, 300);

and now get distance was not declared in this scope

Try this.

Leave the colorWipe() function as it is, ie

void colorWipe(byte red, byte green, byte blue, int SpeedDelay)
{
  for (uint16_t i = 0; i < NUM_LEDS; i++)
  {
    setPixel(i, red, green, blue);
    showStrip();
    delay(SpeedDelay);
  }
}

Work out the required delay like this, substituting your own variable name for distance

int mappedDelay = map(distance, 0, 300, 100, 300);  //input 0 to 100, output 100 to 300

Then call the colorWipe() function like this

colorWipe(0x00,0xff,0x00, mappedDelay);

I can't try it because I don't have the same hardware as you

I frankencoded an arduino sketch to display distance data on LCD and play different sounds based on a distance scanner (happens to be a TFMini) but similar circumstances...may be of interest to you....

build details here:

code details here:
https://forums.adafruit.com/viewtopic.php?f=25&t=142445&p=710141#p710141

Thank you very much for all your help I now have a working sketch

// defines pins numbers
#include <FastLED.h>

#define LED_PIN 6
#define NUM_LEDS 6
#define BRIGHTNESS 64
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100

const int trigPin = 8;
const int echoPin = 7;
// defines variables
long duration;
int distance;

void setup() {
delay( 3000 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

int mappedDelay = map(distance, 0, 300, 100, 300); //input 0 to 100, output 100 to 300

colorWipe(0x00,0xff,0x00, mappedDelay);
colorWipe(0x00,0x00,0x00, mappedDelay);

}

void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
for(uint16_t i=0; i<NUM_LEDS; i++) {
setPixel(i, red, green, blue);
showStrip();
delay(SpeedDelay);
}
}

// ---> here we define the effect function <---
// *** REPLACE TO HERE ***

void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}

void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}

Thank you very much for all your help I now have a working sketch

That's good news, but you have not learned to Auto Format the code in the IDE and use code tags like this when posting here to make it easier to read and copy to an editor

// defines pins numbers
#include <FastLED.h>

#define LED_PIN     6
#define NUM_LEDS    6
#define BRIGHTNESS  64
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100

const int trigPin = 8;
const int echoPin = 7;
// defines variables
long duration;
int distance;

void setup()
{
  delay( 3000 ); // power-up safety delay
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(  BRIGHTNESS );
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(115200); // Starts the serial communication
}

void loop()
{
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);
  int mappedDelay = map(distance, 0, 300, 100, 300);  //input 0 to 100, output 100 to 300
  colorWipe(0x00, 0xff, 0x00, mappedDelay);
  colorWipe(0x00, 0x00, 0x00, mappedDelay);
}

void colorWipe(byte red, byte green, byte blue, int SpeedDelay)
{
  for (uint16_t i = 0; i < NUM_LEDS; i++)
  {
    setPixel(i, red, green, blue);
    showStrip();
    delay(SpeedDelay);
  }
}

// ---> here we define the effect function <---
// *** REPLACE TO HERE ***

void showStrip()
{
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  FastLED.show();
#endif
}

void setPixel(int Pixel, byte red, byte green, byte blue)
{
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  leds[Pixel].r = red;
  leds[Pixel].g = green;
  leds[Pixel].b = blue;
#endif
}

void setAll(byte red, byte green, byte blue)
{
  for (int i = 0; i < NUM_LEDS; i++ )
  {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

Thanks