kartonman

randomly selected leds

Jan 8th, 2025
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. // arduino colored lightning with randomly selected leds to light
  2.  
  3. #include <FastLED.h>
  4.  
  5. #define NUM_LEDS 62 // Replace with the actual number of LEDs on your strip
  6. #define AUDIO_SENSOR 0
  7. #define LED_STRIP_PIN 6 // Change to the pin you've connected the data line of your LED strip
  8.  
  9. CRGB leds[NUM_LEDS];
  10.  
  11. void setup() {
  12. analogReference(DEFAULT); // Use the default analog reference
  13. FastLED.addLeds<WS2812B, LED_STRIP_PIN, GRB>(leds, NUM_LEDS);
  14. Serial.begin(9600);
  15. }
  16.  
  17. void loop() {
  18. int sensorValue = analogRead(AUDIO_SENSOR);
  19. int threshold = 450;
  20.  
  21. if (sensorValue > threshold) {
  22. int numLedsToLight = map(sensorValue, threshold, 1023, 0, NUM_LEDS);
  23. numLedsToLight = constrain(numLedsToLight, 0, NUM_LEDS);
  24.  
  25. // Set all LEDs to off initially
  26. fill_solid(leds, NUM_LEDS, CRGB::Black);
  27.  
  28. // Randomly select LEDs to light up
  29. for (int i = 0; i < numLedsToLight; i++) {
  30. int randomIndex = random(NUM_LEDS);
  31. leds[randomIndex] = blend(CRGB(0, 0, 128), CRGB::White, map(i, 0, numLedsToLight - 1, 0, 255));
  32. }
  33.  
  34. FastLED.show();
  35. delay(random(100, 300)); // Random delay between flashes
  36.  
  37. // Turn off all LEDs
  38. fill_solid(leds, NUM_LEDS, CRGB::Black);
  39. FastLED.show();
  40. }
  41.  
  42. delay(20); // Add a small delay to avoid continuous reading
  43. }
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment