Programmable LEDs: Difference between revisions

From IxD Studio
Jump to navigation Jump to search
Created page with "= LED strip = * RS product: https://se.rs-online.com/web/p/led-ljuslister/1807500?gb=s ** Datasheet: https://docs.rs-online.com/c4c2/0900766b816d3d12.pdf == Dependencies == * Install the library FastLED: https://github.com/FastLED/FastLED ** Documentation: https://github.com/FastLED/FastLED/wiki/Overview ** Basic usage: https://github.com/FastLED/FastLED/wiki/Basic-usage == Examples online == * https://learn.adafruit.com/adafruit-neopixel-uberguide/the-magic-of-neop..."
 
No edit summary
Line 26: Line 26:


=== Source code ===
=== Source code ===
Important functions:
* '''random()'''
* '''randomSeed()'''
* '''addLeds()'''
* '''setBrightness()'''
The script depends on the number of LEDs (see variable '''NUM_LEDS''').
<pre>
<pre>
// include library
// include library

Revision as of 11:29, 4 March 2026

LED strip

Dependencies

Examples online

How to use

Install the FastLED library in the Arduino IDE.

How to connect the LED strip

Make sure the arrows are pointing to the right direction. The beginning of an arrow indicates the connection point to the Arduino board. The tip of the arrow show how the current flows in the strip.

Source code

Important functions:

  • random()
  • randomSeed()
  • addLeds()
  • setBrightness()

The script depends on the number of LEDs (see variable NUM_LEDS).


// include library
#include<FastLED.h>

//define number of LED and pin
#define NUM_LEDS 4
#define DATA_PIN 3

// create the ld object array
CRGB leds[NUM_LEDS];

// define 3 byte for the random color
byte  r, g, b;
float brightness;

void setup() {
  Serial.begin(9600);
  // init the LED object
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  // set random seed
  randomSeed(analogRead(0));
}

void loop() {
  // loop over the NUM_LEDS
  for (int cur = 0; cur < NUM_LEDS; cur++) {
    brightness = 50.0 / pow(2, cur);
    FastLED.setBrightness(brightness); // range: 0-255
    r = random(150, 200);
    g = random(100, 150);
    b = random(0, 50);
    //set the value to the led AND turn on
    leds[cur] = CRGB(r, g, b); FastLED.show();
    FastLED.delay(200);
    // turn off previous
    leds[cur] = CRGB::Black; FastLED.show();
    Serial.println(cur, DEC);
  }
}