Change input from pot to a ultrasonic sensor to control WS2812B

Hey all, I have working code to light up my WS2812B with a potentiometer - but can't find any good tutorials or examples to switch input from a potentiometer to an ultrasonic sensor.

trying to get it to light up more as object gets closer to the prox sensor

any help in the change is greatly appreciated. Thanks - Jane

#include "FastLED.h"




#define LED_TYPE WS2811
#define PIN_SLIDE_POT_A A0 // input pin of the slide pot
#define MAX_SLIDE_POT_ANALGOG_READ_VALUE 1020 // maximum voltage as analog-to-digital converted value, depends on the voltage level of the VCC pin. Examples: 5V = 1023; 3.3V ~700

#define NUM_LEDS 55 // add number of LEDs of your RGB LED strip
#define PIN_LED 3 // digital output PIN that is connected to DIN of the RGB LED strip
#define LED_COLOR CRGB::White // see https://github.com/FastLED/FastLED/wiki/Pixel-reference for a full list, e.g. CRGB::AliceBlue, CRGB::Amethyst, CRGB::AntiqueWhite...

CRGB grb_led[NUM_LEDS]; // color array of the LED RGB strip
#define COLOR_ORDER GRB

const int buzzer=11;

void setup() {
  Serial.begin(9600);
  
  pinMode(PIN_SLIDE_POT_A, INPUT);
  FastLED.addLeds<WS2812B, PIN_LED>(grb_led, NUM_LEDS);  

FastLED.addLeds<WS2812B, PIN_LED, GRB>(grb_led, NUM_LEDS);
//FastLED.addLeds<WS2812B, PIN_LED, GRB>(leds, NUM_LEDS);  // GRB ordering is typical

  FastLED.setBrightness(15);
  Serial.println("Setup done.");

 pinMode(buzzer, OUTPUT);

}

void loop() {
  // 1) Analog value of slide pot is read
  int value_slide_pot_a = analogRead(PIN_SLIDE_POT_A);
  Serial.print("Slide Pot value: ");
  Serial.println(value_slide_pot_a);

  // 2) Analog value is mapped from slide pot range (analog input value) to led range (number of LEDs)
  int num_leds_switchedon = map(value_slide_pot_a, 0, MAX_SLIDE_POT_ANALGOG_READ_VALUE, 0, NUM_LEDS);  


  // 3) Light up the LEDs
  // Only LEDs are switched on which correspond to the area left of the slide knob
  for (int i = 0; i < num_leds_switchedon; ++i) {
    grb_led[i] = LED_COLOR;
  }  
  // LEDs are switched off which correspond to the area right of the slide knob
  for (int i = num_leds_switchedon; i < NUM_LEDS; ++i) {
    grb_led[i] = CRGB::Black;
  }


for (int i = 0; i < num_leds_switchedon; ++i) { if (value_slide_pot_a > 785) {
grb_led[i] = CRGB::DarkRed;


FastLED.setBrightness(45);
 
 

 tone(buzzer, 110);
 //digitalWrite(buzzer, HIGH); 
} else if (value_slide_pot_a > 450) {
grb_led[i] = CRGB::DarkGreen;
FastLED.setBrightness(15);
noTone(buzzer);
//digitalWrite(buzzer, LOW); 
} else if (value_slide_pot_a > 120) {
grb_led[i] = CRGB::DarkGreen;
} else {
grb_led[i] = CRGB::DarkGray;
}
}



  FastLED.show();

  
}
















Where are you stuck ?

Read the distance and use map() to convert it to the number of LEDs to be lit in the same way as you map the pot value to the number of LEDs

You may need to use constrain() to limit the upper value of the distance before using map()

I'm new to this, I'm stuck after defining the pins, I don't know how to map them
#define trigPin 11
#define echoPin 12

You do not map() the pin numbers, rather you map() the distance measured by the ultrasonic sensor

Have you got a simple sketch that measures the distance and prints it to the Serial monitor ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.