How to create a rainbow wave on LED Strip using Arduino? - arduino

I want to create some effects for my led strip with my arduino nano as the controller.
So far I managed to get the basics done (same static color for each led, color fade with each leds simultaneous).
I got a rainbow effect working, but its basically only a cycle through the color spectrum for all leds at the same time.
What I want is a rainbow wave, where the colors are moving in one direction and fading into/chasing each other.

I assume you want something like this:
I am using the FastLED library for this, but I think you can change the code a bit to make it work with different LED libraries.
#include <FastLED.h>
#define NUM_LEDS 60 /* The amount of pixels/leds you have */
#define DATA_PIN 7 /* The pin your data line is connected to */
#define LED_TYPE WS2812B /* I assume you have WS2812B leds, if not just change it to whatever you have */
#define BRIGHTNESS 255 /* Control the brightness of your leds */
#define SATURATION 255 /* Control the saturation of your leds */
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<LED_TYPE, DATA_PIN>(leds, NUM_LEDS);
}
void loop() {
for (int j = 0; j < 255; j++) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(i - (j * 2), SATURATION, BRIGHTNESS); /* The higher the value 4 the less fade there is and vice versa */
}
FastLED.show();
delay(25); /* Change this to your hearts desire, the lower the value the faster your colors move (and vice versa) */
}
}

have you tried fill_rainbow from FastLED library? Not tested but should work.
#include "FastLED.h"
#define LED_DT 1
#define COLOR_ORDER GRB
#define LED_TYPE WS2812
#define NUM_LEDS 15
uint8_t max_bright = 255;
struct CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(115200);
LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(max_bright);
} // setup()
void loop () {
uint8_t thisSpeed = 10;
uint8_t deltaHue= 10;
uint8_t thisHue = beat8(thisSpeed,255);
fill_rainbow(leds, NUM_LEDS, thisHue, deltaHue);
FastLED.show();
}

Same assumption, different approach: you want something like this (love this gif):
It is working with the latest FastLED library 'onboard' features more even timing and smooth found here:
#include <FastLED.h>
#define NUM_LEDS 18
#define LED_PIN 2
CRGB leds[NUM_LEDS];
//has to be uint8_t so it starts at 0 after it reached 256
uint8_t hue = 0;
void setup() {
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(50);
}
void loop() {
for (int i = 0; i < NUM_LEDS; ++i) {
leds[i] = CHSV(hue + (i * 10), 255, 255);
}
//You can change the pattern speed here
EVERY_N_MILLISECONDS(15){
hue++;
}
FastLED.show();
}
Have fun with it :)

Related

Arduino Control addressable led WS2812B in chunk of 10

Hi I want to light up the led strip in chunk of 10 leds, ie LED 0-9 light up with brightness 0-60, and then LED 10-19 brightness 0-60 and so on. Whenever I tried to light up the next 10 it's also change the previous LEDs. TIA for any help!
#include <FastLED.h>
#define LED_PIN 7
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}
void loop() {
for (int i = 0; i <= 9; i++) {
leds[i] = CRGB ( 0, 0, 255);
for (int brightness = 0; brightness <=65; brightness++) {
FastLED.setBrightness(brightness); }
FastLED.show();
delay(40);
}
for (int i = 10; i <= 19; i++) {
leds[i] = CRGB ( 0, 0, 255);
for (int brightness = 0; brightness <=65; brightness++) {
FastLED.setBrightness(brightness); }
FastLED.show();
delay(40);
}
}

Arduino class to control a RGB led

I'm a new arduino user, so I'm learning now...
I want make a new class to control my RGB led...
I already use this led with a method on my code... But I want to generalize the code to put more led without ctrl+c ctrl+v my code.
I create the class:
/*
STATUSLED CLASS DEFINITION
*/
class StatusLED {
int pinVermelho;
int pinVerde;
int pinAzul;
public:
StatusLED(int pinRed, int pinGreen, int pinBlue) {
this->pinVermelho = pinRed;
this->pinVerde = pinGreen;
this->pinAzul = pinBlue;
}
void RGB(int red, int green, int blue) {
Serial.print("StatusLED.RGB(");
Serial.print(red);
Serial.print(", ");
Serial.print(green);
Serial.print(", ");
Serial.print(blue);
Serial.println(");");
digitalWrite(pinVermelho, 255 - red);
digitalWrite(pinVerde, 255 - green);
digitalWrite(pinAzul, 255 - blue);
}
};
I initialize that with the pins of my RBG led:
#define pinoAzul 9
#define pinoVerde 10
#define pinoVermelho 11
StatusLED led(pinoVermelho, pinoVerde, pinoAzul);
And to test it I use:
void setup() {
pinMode(pinoAzul, OUTPUT);
digitalWrite(pinoAzul, LOW);
pinMode(pinoVerde, OUTPUT);
digitalWrite(pinoVerde, LOW);
pinMode(pinoVermelho, OUTPUT);
digitalWrite(pinoVermelho, LOW);
}
void loop() {
led.RGB(255, 0, 0);
delay(1000);
led.RGB(0, 255, 0);
delay(1000);
led.RGB(0, 0, 255);
delay(1000);
}
The method RGB is called, but my led don't turn on.
If i move the RGB method to outside of my class, this works fine.
Can someone please tell my what I'm doing worng?
You are attempting to control an RGB LED using PWM (Pulse Width Modulation). To set the pulse on/off ratio on a specific PWM pin, you should use analogWrite(PWM_out_pin, PWM_out_level);
Your code is incorrectly using digitalWrite which doesn't affect the PWM wave form. It also only takes a HIGH or LOW value parameter besides pin number.
I moved the configuration of pins as OUTPUT to constructor of my class like that:
StatusLED::StatusLED(int pinRed, int pinGreen, int pinBlue) {
this->pinRed = pinRed;
pinMode(pinRed, OUTPUT);
this->pinGreen = pinGreen;
pinMode(pinGreen, OUTPUT);
this->pinBlue = pinBlue;
pinMode(pinBlue, OUTPUT);
RGB(0, 0, 0);
}
This solve the problem.
I also change the method RGB to use analogWrite as you tell and now the fade is working well too.
void StatusLED::RGB(int redValue, int greenValue, int blueValue) {
if (redValue > 255) {
redValue = 255;
}
if (greenValue > 255) {
greenValue = 255;
}
if (blueValue > 255) {
blueValue = 255;
}
if (redValue < 0) {
redValue = 0;
}
if (greenValue < 0) {
greenValue = 0;
}
if (blueValue < 0) {
blueValue = 0;
}
analogWrite(pinRed, 255 - redValue);
analogWrite(pinGreen, 255 - greenValue);
analogWrite(pinBlue, 255 - blueValue);
}
Thanks everyone!

FastLed library use the CRGB as attribute

I'm working on a program for an addressable LED strip. It is working and at this point I'm trying to make my code better. I have 3 LED strips and I made a function which all three has to do. In the function I want to specify which one needs to be updated so I used attributes, but this doesn't seem to work. I can't find this on the FastLed documentation.
//Number of leds powered
int led_state_1 = 0;
int led_state_2 = 0;
int led_state_3 = 0;
// This is an array of leds. One item for each led in your strip.
CRGB leds1[NUM_LEDS];
CRGB leds2[NUM_LEDS];
CRGB leds3[NUM_LEDS];
void CheckAndUpdateLed(CRGB LedArray, int led_state){
resetLedStrip(LedArray);
for(int whiteLed = 0; whiteLed < led_state; whiteLed = whiteLed + 1) {
// Turn our current led on to white, then show the leds
LedArray[whiteLed] = CRGB::White;
// Show the leds (only one of which is set to white, from above)
FastLED.show();
}
}
When I change LedArray to leds1 it is working. I'm calling the function as CheckAndUpdateLed(leds1, led_state_1);
I think my question was a bit unclear sorry for that. I came up with another way of doing this. Instead of 1 led strip I check them all in the same function.
#define NUM_STRIPS 3
#define NUM_LEDS_PER_STRIP 15
CRGB leds[NUM_STRIPS][NUM_LEDS_PER_STRIP];
int led_states[] = {0, 0, 0};
void CheckAndUpdateLeds(){
//resets the leds to black
resetLedStrips();
// This outer loop will go over each LED strip, one at a time
for(int x = 0; x < NUM_STRIPS; x++) {
// This inner loop will go over each led in the current strip, one at a time till the amount of light is the same as in the led_state
for(int led = 0; led < led_states[x]; led = led + 1) {
// Turn our current led on to white, then show the leds
leds[x][led] = CRGB::White;
FastLED.show();
}
}
}
void resetLedStrips(){
for(int x = 0; x < NUM_STRIPS; x++) {
for(int led = 0; led < NUM_LEDS_PER_STRIP; led = led + 1) {
leds[x][led] = CRGB::Black;
}
}
}

Multiple If statements for Arduino

I'm new to this site and also the wonderful world of Arduino, I have been playing around with a Leonardo board and some Neopixel LEDS ( WS2812B ). I'm currently trying to set predefined colors on the LEDs with a single Pot, but also have an interrupt for a PIR sensor. I'm using the Fastled library since its great but at this point I seem to be stuck on the interrupt part. Any guidance will be appreciated !
#include "FastLED.h"
#define NUM_LEDS 3
#define DATA_PIN 6
#define PIR_PIN 2
int PIR_PIN = 0
// These constants won't change:
const int analogPin = A0; // pin that the sensor is attached to
CRGB leds[NUM_LEDS];
void setup() {
// initialize the LED pin as an output:
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
// initialize serial communications:
pinMode(PIR_PIN, INPUT);
attachInterrupt(0, PIR_PIN, CHANGE);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
if (analogPin < 1000)
{
leds[0] = CRGB::Red;
FastLED.show();// do Thing A
}
else if (analogPin < 500)
{
leds[0] = CRGB::Orange;
FastLED.show();// do Thing B
}
else if (analogPin < 250)
{
leds[0] = CRGB::Green;
FastLED.show();//do fuckity
}
else
{
leds[0] = CRGB::Purple;
FastLED.show();// do Thing C
}
}
void PIN_PIR () {
buttonState = digitalRead(PIR_PIN);
digitalWrite(DATA_PIN, buttonState);
}

Arduino code to make an led strip of 3 blink

I have an arduino board with an led strip with 3 led plugged into pin 1. a picture of the wiring is below. this is the code I used to attempt to lightup the led, with no luck:
#include <Adafruit_NeoPixel.h>
#define PIN 1
Adafruit_NeoPixel strip = Adafruit_NeoPixel(3, PIN, NEO_KHZ800);
void setup() {
strip.begin();
strip.show();//Initialize all pixels to 'off'
strip.setbrightness(50);
forcint i=0; i<3;i++)
strip.setPixelColor(0,255,255,255);
strip.show();
}
void loop() {
for (int i=255;i>=0;i--){
lightColor(i,0,0);
delay(10);
i=i+2
}
}
any help will be aprecited, this is my first time using arduino
#include <Adafruit_NeoPixel.h>
#define PIN 13
Adafruit_NeoPixel strip = Adafruit_NeoPixel(3, PIN, NEO_KHZ800);
void setup() {
strip.begin();
strip.show();//Initialize all pixels to 'off'
strip.setbrightness(50);
strip.setPixelColor(0,255,255,255);
strip.show();
}
void loop() {
for (int i=255;i>=0;i--){
lightColor(i,0,0);
delay(10);
}
delay(500);
}
With this code, LED will decrease RED channel until 0, wait 0'5 seconds and repeat.
I change your setup function by deleting a malformed for loop. And in loop() I added a delay and removed i=i+2 bacause I didn't understand its function. And finally, you must use another pin because PIN 1 is for serial use.

Resources