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);
}
}
Related
I'm trying to make a program so when 2 switches are flipped they light up two 4 pin LEDs, but only when both switches are flipped. I'm trying to make a cool BattleBots arena and thought something like that would be cool to have as a starting sequence! If you spot any extra errors feel free to correct me.
int buttonPin = 13;
int buttonPin2 = 3;
int counter = 0;
int redpin = 11; //select the pin for the red LED
int bluepin =10; // select the pin for the blue LED
int greenpin =9;// select the pin for the green LED
int redpin2 = 7; //select the pin for the 2 red LED
int bluepin2 =6; // select the pin for the 2 blue LED
int greenpin2 =5;// select the pin for the 2 green LED
int pinButton = 13; //the pin where we connect the button
int pinButton2 = 3; //the pin where we connect the button
int val;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(redpin2, OUTPUT);
pinMode(bluepin2, OUTPUT);
pinMode(greenpin2, OUTPUT);
Serial.begin(9600);
}
void loop()
{ int buttonState;
buttonState = digitalRead(buttonPin);
int buttonState2;
buttonState2 = digitalRead(buttonPin2);
}
int main()
if (buttonState == LOW && buttonState2 == LOW);
analogWrite(11, 0);
analogWrite(10, 225);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 225);
analogWrite(5, 0);
delay(5);
}else{
analogWrite(11, 225);
analogWrite(10, 0);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 0);
analogWrite(5, 225);
delay(5);
}
Your code seem to be a cut and paste from several sources, here's what it should look like, I added notes in the code:
const int buttonPin = 13;
const int buttonPin2 = 3;
const int redpin = 11; //select the pin for the red LED
const int bluepin = 10; // select the pin for the blue LED
const int greenpin = 9; // select the pin for the green LED
const int redpin2 = 7; //select the pin for the 2 red LED
const int bluepin2 = 6; // select the pin for the 2 blue LED
const int greenpin2 = 5; // select the pin for the 2 green LED
/*
Unused variables removed:
int counter = 0;
int pinButton = 13;
int pinButton2 = 3;
int val;
*/
void setup() {
pinMode(buttonPin, INPUT);
pinMode(redpin, OUTPUT);
/*
Depending on your hardware you may want to pull-up the button pins:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
*/
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(redpin2, OUTPUT);
pinMode(bluepin2, OUTPUT);
pinMode(greenpin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
int buttonState;
buttonState = digitalRead(buttonPin);
int buttonState2;
buttonState2 = digitalRead(buttonPin2);
/*
main() function had several issues, it was a function returning an integer without
any return statement and it is never called by your code. Looks like it should be
part of loop() like so:
*/
if (buttonState == LOW && buttonState2 == LOW) {
analogWrite(11, 0);
analogWrite(10, 225);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 225);
analogWrite(5, 0);
delay(5);
} else {
analogWrite(11, 225);
analogWrite(10, 0);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 0);
analogWrite(5, 225);
delay(5);
}
}
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 :)
I have a strip of Neopixels that I'm using for underglow on my bed. Recently, I decided to add a sound sensor so that I can clap once to toggle the light animations I'm using either on or off. Now I'm just running into trouble getting my code to work. I can turn the lights on but I can't clap again to turn them off. What I suspect may be the problem is the fact that I only give the condition for the light animation to be turned off at the end of the while loop that I use to run the light animations, however, I have no idea what I can do to fix that. Can somebody help me on this?
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
int soundSensor = 2;
int LED = 13;
void setup()
{
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode (soundSensor, INPUT);
pinMode (LED, OUTPUT);
}
void loop()
{
int toggle = 0;
int statusSensor = digitalRead (soundSensor);
if (statusSensor == 1 && toggle == 0)
{
toggle= 1;
digitalWrite(LED, HIGH);
//statusSensor = 0;
statusSensor = digitalRead (soundSensor);
}
//NEOPIXEL CODE:
while (toggle == 1)
{
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(200);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
digitalWrite(LED,HIGH);
colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color(0, 255, 0), 50); // Green
colorWipe(strip.Color(255,255,255), 50); //White
colorWipe(strip.Color(255,255,255), 50);
colorWipe(strip.Color(0, 0, 255), 50); // Blue
rainbow(20);
rainbowCycle(20);
if(statusSensor == 1){
toggle = 0;
}
}
if (statusSensor == 1 && toggle == 1)
{
toggle = 0;
digitalWrite(LED, LOW);
}
}
//NEOPIXEL Functions:
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
When you set toggle to 0, you don't write any colors to the neo-pixel strip. So they will just be left in whatever state they were in. You can fix this by just setting them all to "black" (i.e. off) when you set the toggle to zero:
if(statusSensor == 1) {
toggle = 0;
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(strip.Color(0,0,0));
}
strip.show();
}
You don't need any delay here if you just want them to turn out straight away, but perhaps you could make them fade down?
I am trying to make a led controller for my room rgb led strip.
I am using arduino uno. I want two pushbuttons:
controls static colors of led (one color at a time.cycles color on pressing that same switch again)
rgb crossfade
Both programs work separately. Now I want these two programs in one sketch and toggled by two pushbuttons one for color cycle and one for crossfade
sketch works but only one pushbutton is working.
I have connected 2 10k ohm resistors with both buttons according to various guides and tutorials. I think the wiring is correct.
Here is the sketch:
const int buttonPin1 = 2; // button 1
const int buttonPin2 = 3; // button 2
const int redPin = 9; // red led pin
const int greenPin = 11; // green led pin
const int bluePin = 10; // blue led pin
int pushCounter = 0; // push counter for changing colors at each press
int buttonState = 0; // button state i.e. HIGH or LOW
int lastButtonState = 0;
void setup()
{
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
setColourRgb(0,0,0);
}
void setColor(int red, int green, int blue) // function for setting custom colors
{
//#ifdef COMMON_ANODE
//red = 255 - red;
//green = 255 - green;
//blue = 255 - blue;
//#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) { //function for setting crossfade colors
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void loop()
{
int buttonState1 = 0;
int buttonState2 = 0;
buttonState1 = digitalRead(buttonPin1); //reads button states
buttonState2 = digitalRead(buttonPin2);
if ((buttonState1 == HIGH) && (buttonState2 == LOW)) //compares button states of both switches to enable the correct program
{
buttonState = digitalRead(buttonPin1);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH) //increases pushcounter of button1 on every press
{
pushCounter++;
}
}
//delay(1);
lastButtonState = buttonState;
if (pushCounter == 1) //custom led colors
{ setColor(255, 0, 0);}; //red
if (pushCounter == 2)
{ setColor(0, 255, 0);}; //green
if (pushCounter == 3)
{ setColor(0, 0, 255);}; //blue
if (pushCounter == 4)
{ setColor(255,200,255);}; //white
if (pushCounter == 5)
{ setColor(255,255,0);}; //lime
if (pushCounter == 6)
{ setColor(0,255,255);}; //aqua
if (pushCounter == 7)
{ setColor(255,0,255);}; //violet
if (pushCounter == 8)
{ setColor(128,0,128);}; //dim_violet
if (pushCounter == 9)
{ pushCounter = 0;}
}
if ((buttonState1 == LOW) && (buttonState2 == HIGH)) //compares button states to load second program
{
unsigned int rgbColour[3];
// Start off with red.
rgbColour[0] = 255; //sets first color
rgbColour[1] = 0;
rgbColour[2] = 0;
// Choose the colours to increment and decrement.
for (int decColour = 0; decColour < 3; decColour += 1)
{
int incColour = decColour == 2 ? 0 : decColour + 1;
// cross-fade the two colours.
for(int i = 0; i < 255; i += 1)
{
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
delay(30);
}
}
};
}
Now if I reverse the if condition for second program... the second program runs but the first doesn't run. What is wrong here?
Since you are reading from two buttons you need to do some debouncing.
Something like you've done but a bit better :)
There is a library that does this for you and can be downloaded from Arduino web
Once downloaded Change the #include "WProgram.h" to #include "Arduino.h" in Debounce.cpp and you should be good to go.
below is the implementation of the lib with your code.
From what described the logic is right.
#include <Debounce.h>
#define buttonPin1 2 // button 1
#define buttonPin2 3 // button 2
#define redPin 9 // red led pin
#define greenPin 11 // green led pin
#define bluePin 10 // blue led pin
#define debounceTime 20 // debounce time
int pushCounter = 0; // push counter for changing colors at each press
int buttonState = 0; // button state i.e. HIGH or LOW
int lastButtonState = 0;
// Instantiate a Debounce object
Debounce button1 = Debounce(debounceTime, buttonPin1); //for button 1
Debounce button2 = Debounce(debounceTime, buttonPin2); //for button 2
void setup()
{
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
setColourRgb(0,0,0);
}
void loop()
{
//update debounce stat for both buttons
button1.update();
button2.update();
int button1State = button1.read(); //reads button states
int button2State = button2.read();
if ((button1State == 1) && (button2State == 0)) //compares button states of both switches to enable the correct program
{
//Since we are checking for the first button being HIGH or 1 we can increament the counter
pushCounter++;
/*
buttonState = button1State;
if (buttonState != lastButtonState)
{
if (buttonState == HIGH) //increases pushcounter of button1 on every press
{
pushCounter++;
lastButtonState = buttonState;
}
}
*/
//delay(1);
switch (pushCounter)
{
case 1:
setColor(255, 0, 0); //red
break;
case 2:
setColor(0, 255, 0); //green
break;
case 3:
setColor(0, 0, 255); //blue
break;
case 4:
setColor(255,200,255); //white
break;
case 5:
setColor(255,255,0); //lime
break;
case 6:
setColor(0,255,255); //aqua
break;
case 7:
setColor(255, 0, 0); //violet
break;
case 8:
setColor(128,0,128); //dim_violet
break;
case 9:
pushCounter = 0; // reset the counter
break;
}
}
else if ((button1State == 0) && (button2State == 1)) //compares button states to load second program
{
unsigned int rgbColour[3];
// Start off with red.
rgbColour[0] = 255; //sets first color
rgbColour[1] = 0;
rgbColour[2] = 0;
// Choose the colours to increment and decrement.
for (int decColour = 0; decColour < 3; decColour++)
{
int incColour = decColour == 2 ? 0 : decColour + 1;
// cross-fade the two colours.
for(int i = 0; i < 255; i += 1)
{
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
delay(30);
}
}
}
}
void setColor(int red, int green, int blue) // function for setting custom colors
{
//#ifdef COMMON_ANODE
//red = 255 - red;
//green = 255 - green;
//blue = 255 - blue;
//#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) { //function for setting crossfade colors
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
I am having some issues with my code. What I want it to do:
Fade in leds 0-12
Delay 0.5 sec
Fade in leds 13-26
Delay (x amount)
Fade out leds 0-12
Delay 0.5 sec ( same as above)
Fade out leds 13-26
What it does:
Fade in leds 0-12
Delay (x amount)
Fade out leds 0-12
Fade in leds 13-26
Delay (x amount)
-Fade out leds 13-26
Here is my code:
#include <Adafruit_NeoPixel.h>
#define SENSORPIN 4
#define LEDPIN 13
// variables will change:
int sensorState = 0, lastState=0; // variable for reading the pushbutton status
int PIN = 6;
int totalLEDs = 26;
int ledFadeTime = 10;
int lightuptime = 7000;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(totalLEDs, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH); // turn on the pullup
Serial.begin(9600);
}
void loop() {
// read the state of the sensor value:
sensorState = digitalRead(SENSORPIN);
// check if the sensor beam is broken
// if it is, the sensorState is LOW:
if (sensorState == LOW) {
rgbFadeInAndOut(255, 255, 255, ledFadeTime,0,13);
delay(50);
rgbFadeInAndOut(255, 255, 255, ledFadeTime,13,26);
}
else {
}
lastState = sensorState;
}
void rgbFadeInAndOut(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait, uint8_t ledStart, uint8_t ledEnd) {
for(uint8_t b = 0; b <255; b++) {
for(uint8_t i=ledStart; i < ledEnd; i++) {
strip.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
}
delay(25);
strip.show();
//delay(wait);
};
delay(lightuptime);
for(uint8_t b=255; b > 0; b--) {
for(uint8_t i = ledStart; i < ledEnd; i++) {
strip.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
if(b==1){
strip.setPixelColor(i, 0, 0, 0);
}
}
strip.show();
delay(wait);
};
};
What am I missing?
Your function rgbFadeInAndOut will fade in and then fade out the specified Leds before returning control to the calling function.
If you want your desired behaviour, just split the fade In and fade Out in two separate functions: rgbFadeIn(...) and rgbFadeOut(...)
then you do:
rgbFadeIn(0..12);
delay(500); // note that 0.5 seconds is 500ms not 50
rgbFadeIn(13..26);
delay(x amount)
rgbFadeOut(0..12);
delay(500);
rgbFadeOut(13..26);