Advertisement
Guest User

Untitled

a guest
Nov 19th, 2021
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. #define PIN_SLIDE_POT_A A0 // input pin of the slide pot
  4. #define MAX_SLIDE_POT_ANALGOG_READ_VALUE 700 // maximum voltage as analog-to-digital converted value, depends on the voltage level of the VCC pin. Examples: 5V = 1023; 3.3V ~700
  5.  
  6. #define NUM_LEDS 10 // add number of LEDs of your RGB LED strip
  7. #define PIN_LED 3 // digital output PIN that is connected to DIN of the RGB LED strip
  8. #define LED_COLOR CRGB::DarkOrchid // see https://github.com/FastLED/FastLED/wiki/Pixel-reference for a full list, e.g. CRGB::AliceBlue, CRGB::Amethyst, CRGB::AntiqueWhite...
  9.  
  10. CRGB rgb_led[NUM_LEDS]; // color array of the LED RGB strip
  11.  
  12. void setup() {
  13.   Serial.begin(9600);
  14.  
  15.   pinMode(PIN_SLIDE_POT_A, INPUT);
  16.   FastLED.addLeds<WS2812B, PIN_LED>(rgb_led, NUM_LEDS);  
  17.  
  18.   Serial.println("Setup done.");
  19. }
  20.  
  21. void loop() {
  22.   // 1) Analog value of slide pot is read
  23.   int value_slide_pot_a = analogRead(PIN_SLIDE_POT_A);
  24.   Serial.print("Slide Pot value: ");
  25.   Serial.println(value_slide_pot_a);
  26.  
  27.   // 2) Analog value is mapped from slide pot range (analog input value) to led range (number of LEDs)
  28.   int num_leds_switchedon = map(value_slide_pot_a, 0, MAX_SLIDE_POT_ANALGOG_READ_VALUE, 0, NUM_LEDS);  
  29.  
  30.  
  31.   // 3) Light up the LEDs
  32.   // Only LEDs are switched on which correspond to the area left of the slide knob
  33.   for (int i = 0; i < num_leds_switchedon; ++i) {
  34.     rgb_led[i] = LED_COLOR;
  35.   }  
  36.   // LEDs are switched off which correspond to the area right of the slide knob
  37.   for (int i = num_leds_switchedon; i < NUM_LEDS; ++i) {
  38.     rgb_led[i] = CRGB::Black;
  39.   }
  40.   FastLED.show();
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement