LED strip ws2812b: Difference between revisions

From wikiluntti
 
(4 intermediate revisions by the same user not shown)
Line 41: Line 41:


     leds[i] = CRGB::Black;
     leds[i] = CRGB::Black;
    FastLED.show();
}
}
</syntaxhighlight>
== Exercises ==
# Make the leds go around the octagon
# Make it go to the other direction
# Make it go both directions, but not at the same time
# Turn three consecutive leds on, and go around the octagon
# Make one led going around, and then put another t follow the first one
# Make a clock
# Smooth transition on brightness
# Smooth transition on color (see fill_gradient_rgb, fill_rainbow)
# HSV colors
# Palettes
== Code: Two leds ==
Two leds chasing each other (same speed).
<syntaxhighlight lang="C">
#include <FastLED.h>
#define NUM_LEDS 38
#define LED_PIN 5
CRGB leds[NUM_LEDS];
void setup () {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(20);
  delay(2000);
}
void loop () {
  int k = 4;
  for (int i=0; i<NUM_LEDS; i++){
    leds[i] = CRGB::Green;
    FastLED.show();
    delay(100);
    int k = (i+10)%NUM_LEDS;
    leds[k] = CRGB::Green;
    FastLED.show();
    delay(100);
    leds[i] = CRGB::Black;
    leds[k] = CRGB::Black;
     FastLED.show();
     FastLED.show();
  }
  }
}
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 16:57, 18 November 2024

Introduction

300 LEDs, 5m,

Each part of the RGB ledd will use at maximum 20mA which gives us 3x20mA = 60mA per pixel. The USB plugged into a computer can provide 500mA of power, and Arduino can supply about 200 mA.

  • If powered by USB, there is a 500mA PTC fuse in line from 5V from the PC.
  • The 5V pin of the Arduino is not connected through the microcontroller. When you are powering your arduino from USB, the USB interface limits your total power consumption to 500 mA.

Take 8 LED in a row: max 8x60 mA = 480 mA.

https://arduino.stackexchange.com/questions/24545/what-happens-if-i-draw-too-much-current-from-atmega-io-pin/24546#24546

Programming

Simple code using FastLED package:

#include <FastLED.h>

#define NUM_LEDS 3
#define LED_PIN 5

CRGB leds[NUM_LEDS];

void setup () {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
  delay(2000);
}


void loop () {
  for (int i=0; i<NUM_LEDS; i++){
    leds[i] = CRGB::Red;
    FastLED.show();
    delay(500);
    leds[i] = CRGB::Green;
    FastLED.show();
    delay(500);

    leds[i] = CRGB::Black;
    FastLED.show();
 }
}

Exercises

  1. Make the leds go around the octagon
  2. Make it go to the other direction
  3. Make it go both directions, but not at the same time
  4. Turn three consecutive leds on, and go around the octagon
  5. Make one led going around, and then put another t follow the first one
  6. Make a clock
  7. Smooth transition on brightness
  8. Smooth transition on color (see fill_gradient_rgb, fill_rainbow)
  9. HSV colors
  10. Palettes

Code: Two leds

Two leds chasing each other (same speed).

#include <FastLED.h>

#define NUM_LEDS 38
#define LED_PIN 5

CRGB leds[NUM_LEDS];

void setup () {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(20);
  delay(2000);
}

void loop () {
  int k = 4;
  for (int i=0; i<NUM_LEDS; i++){
    leds[i] = CRGB::Green;
    FastLED.show();
    delay(100);

    int k = (i+10)%NUM_LEDS; 
    leds[k] = CRGB::Green;
    FastLED.show();
    delay(100);

    leds[i] = CRGB::Black;
    leds[k] = CRGB::Black;
    FastLED.show();
 }
}