A small part of the project I'm working on cycles through colors and animations from the fastled library. I have it all working now, but can't figure out one part. Basically I want to be able to turn certain leds certain colors all at the same time i.e. leds 1,2,3,56 red and 99,100 blue. I can't find an example on how to do so. I'm using the leds = CRGB(0,0,0); line and it works, but I can't figure out how to add multiple numbers in the variable that it reads (i). I have tried :
for (i=0;i<5;cycle++;){
leds[cycle] = CRGB(100,0,0);
fastled.show;
}
This works and displays color in the desired pixels, but has a noticeable refresh between cycles. The leds stay on, but when it first starts you can see each led turn on in order. Is there a way to essentially access every value from an array at eh same time or a different function that would allow to put in every number i want to light up and assign it to a variable that I use to replace [i[? I'm basically trying to avoid a noticable update and having 50 lines of pixel positions to light up one layout.
I can provide more of my code if needed. Everything else is working properly, but its 300 lines so I figured it'd be easier to just explain the issue.
Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup (the italics in your post above for example), leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.
Try calling FastLED.show(); after you have set all the LEDs instead of in each iteration of the for loop.
Thanks for the info on code tags I will do that I there is additional code I need to post. I tried to use show after the cycle and it actually resets my Arduino. It's an Uno and the code is only using 31% memory, so I'm not sure why.
Post the code that resets the Arduino.
I totally just figured out why there was a delay. I've been slowly, but surely cleaning up my code and making all of my settings into voids that I can call as colors, and had a straggler delay left in my loop. That solved that issue, but I'm curious as to what you think about the reset so I'll post it anyway. It isn't all of the time that it reset, so I'm concerned about voltage drop, but i've tested for voltage drop and my leds are powered externally not via output pins. The part I was talking about starts on line 126.
#include "FastLED.h"
#define NUM_LEDS 100
#define DATA_PIN 8
CRGB leds[NUM_LEDS];
int Bvalue = 40;
int Rvalue = 140;
int Left[]= {0,1,2,3,4,5,16,17,18,19,20,26,27,28,29,30,36,37,38,39,40,46,47,48,49,50,56,57,58,59,60,66,67,68,69,70,76,77,78,79,80,86,87,88,89,90,96,97,98,99,100};
int cycle = 0;
/////////////// Switch 1 (eyes / vents / mouth cycle)
int S1 = 1;
int S1B = 0;
int S1Val = 0;
///////////////
/////////////// Switch 2 (battery)
int S2 = 1;
int S2B = 0;
int S2Val = 0;
///////////////
/////////////// Switch 3 (el wire)
int S3 = 1;
int S3B = 0;
int S3Val = 0;
///////////////
/////////////// Switch 4 (fan)
int S4 = 1;
int S4B = 0;
int S4Val = 0;
///////////////
int VAL=0;
void setup() {
pinMode(13, INPUT);
pinMode(6, OUTPUT);
pinMode(8, OUTPUT);
FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
delay(200);
START();
Serial.begin(9600);
}
void loop() {
SWITCH1();
///////////////////////// BASIC COLORS (ON SWITCH 1)
if (S1Val == 100) {
OFF();
}
if (S1Val == 100) {
RED();
}
if (S1Val == 2) {
ORANGE();
}
if (S1Val == 3) {
YELLOW();
}
if (S1Val == 4) {
GREEN();
}
if (S1Val == 5) {
BLUE();
}
if (S1Val == 6) {
PURPLE();
}
if (S1Val == 7) {
WHITE();
}
////////////////////////// OTHER COLORS
if (S1Val == 8) {
FLASH();
}
if (S1Val == 9) {
STROBE();
}
if (S1Val == 10) {
SHIMMER();
}
if (S1Val == 11) {
SHIMMERbp();
}
if (S1Val == 10) {
leds[0] = CRGB(20,40,90);
FastLED.show();
}
if (S1Val == 0) {
for(cycle = 0; cycle < 9; cycle++){
leds[Left[cycle]] = CRGB(0, 100, 0);
}
FastLED.show();
delay(1000);
for(cycle = 0; cycle < 9; cycle++){
leds[Left[cycle]] = CRGB(100, 0, 0);
}
FastLED.show();
delay(1000);
}
//////////////////////////
SWITCH1RESET();
Serial.println(S1Val);
}
//////////////////////////////// END OF LOOP
void START()
{ analogWrite(3, 2);
analogWrite(5, 90);
analogWrite(6, 20);
fill_solid(leds, NUM_LEDS, CRGB(0, 100, 0));
FastLED.show();
delay(200);
analogWrite(3, 0);
analogWrite(5, 0);
analogWrite(6, 0);
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0));
FastLED.show();
delay(200);
analogWrite(3, 2);
analogWrite(5, 90);
analogWrite(6, 20);
fill_solid(leds, NUM_LEDS, CRGB(0, 100, 0));
FastLED.show();
delay(200);
analogWrite(3, 0);
analogWrite(5, 0);
analogWrite(6, 0);
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0));
FastLED.show();
}
void SWITCH1RESET()
{ if (S1Val == 12) {
S1Val = 0;
}
}
void SWITCH1()
{ S1 = digitalRead(13);
if (S1 == S1B) {
S1Val++;
S1B = 2;
}
if (S1 == 1) {
S1B = 0;
}
}
///////////COLOR FUNCTIONS////////////////
void OFF()
{ fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0));
FastLED.show();
analogWrite(3, 0);
analogWrite(5, 0);
analogWrite(6, 0);
}
void RED()
{ fill_solid(leds, NUM_LEDS, CRGB(100, 0, 5));
FastLED.show();
analogWrite(6, 150);
}
void ORANGE()
{ fill_solid(leds, NUM_LEDS, CRGB(75, 40, 0));
FastLED.show();
analogWrite(3, 0);
analogWrite(5, 15);
analogWrite(6, 150);
}
void YELLOW()
{ fill_solid(leds, NUM_LEDS, CRGB(60, 70, 0));
FastLED.show();
analogWrite(3, 0);
analogWrite(5, 40);
analogWrite(6, 100);
}
void GREEN()
{ fill_solid(leds, NUM_LEDS, CRGB(0, 120, 20));
FastLED.show();
analogWrite(3, 2);
analogWrite(5, 90);
analogWrite(6, 20);
}
void BLUE()
{ fill_solid(leds, NUM_LEDS, CRGB(10, 1, 100));
FastLED.show();
analogWrite(3, 60);
analogWrite(6, 5);
}
void PURPLE()
{ fill_solid(leds, NUM_LEDS, CRGB(30, 0, 90));
FastLED.show();
analogWrite(3, 200);
analogWrite(6, 120);
}
void WHITE()
{ fill_solid(leds, NUM_LEDS, CRGB(30, 40, 40));
FastLED.show();
analogWrite(3, 75);
analogWrite(5, 110);
analogWrite(6, 100);
}
void FLASH() {
EVERY_N_MILLISECONDS( 200 ) {
fill_solid(leds, NUM_LEDS, CRGB(30, 40, 40));
FastLED.show();
analogWrite(3, 75);
analogWrite(5, 110);
analogWrite(6, 100);
}
EVERY_N_MILLISECONDS( 400 ) {
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0));
FastLED.show();
analogWrite(3, 0);
analogWrite(5, 0);
analogWrite(6, 0);
}
}
void STROBE() {
EVERY_N_MILLISECONDS( 200 ) {
fill_solid(leds, NUM_LEDS, CRGB(60, 70, 70));
FastLED.show();
analogWrite(3, 75);
analogWrite(5, 110);
analogWrite(6, 100);
}
EVERY_N_MILLISECONDS( 50 ) {
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0));
FastLED.show();
analogWrite(3, 0);
analogWrite(5, 0);
analogWrite(6, 0);
}
}
void SHIMMER() {
analogWrite(3, 75);
analogWrite(5, 110);
analogWrite(6, 100);
delay(50);
leds[random(1, 100)] = CRGB(30, 40, 40);
FastLED.show();
delay(50);
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0));
}
void SHIMMERbp() {
analogWrite(3, 40);
analogWrite(5, 40);
analogWrite(6, 150);
delay(50);
leds[random(1,100)] = CRGB(Rvalue, 20, Bvalue);
FastLED.show();
leds[random(1,100)] = CRGB(Rvalue, 20, Bvalue);
FastLED.show();
delay(50);
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0));
Bvalue = Bvalue+1;
Rvalue = Rvalue-1;
if (Bvalue == 140) {
Rvalue = 140;
Bvalue = 40;
}
}
I also plan on changing all of my delays to the every milliseconds function in fastled. My code has to check button position constantly so I realize delays are sloppy, but once I finish all of the animations I plan to include those will be replaced.