Hello I need some help working through some logic here.

(friends 4life) #1

Hello I need some help working through some logic here. This is mainly to do with arrays (I think!). The gist of it is that I want to test this out on my short strip with 23 leds (ws2811), but I may want to use a longer strip using this code in the future. Basically I wanna just be able to swap numbers in array when I need to use this code again.

I want even number leds in array to play RGB (204, 12, 156) and odd ones to play RGB (110, 231, 58). Further more I want the loop to run 3 times and I want the leds to fade out.

Now I’m aware that you can declare variables in array like this:

int exampleArray[3];

exampleArray[0]= 10;
exampleArray[1]= 12;
exampleArray[2]= 21;

However as you know this becomes problematic if your array is larger (can you imagine having to do this 200 times?)

Because I want to call up even and odd number leds, instead of doing all that declaration I thought maybe I could use if ((leds % 2)== 0) for the even number and else for the odd?

I have not tried to compile this yet as I’m 100% sure there is something wrong with my logic.

Here is my code thus far:

/*this program will play even numbers of leds in an array as
(204, 12, 156) and odd numbers as (110, 231, 58). Loop will repeat 3 times then go to a rainbow */

#include <FastLED.h>

#define PIN 4
#define NUM_LEDS 23 //my strip has 23 leds total
#define LED_TYPE WS2811
int fadeAmount = 8;
int brightness = 0;

CRGB leds[NUM_LEDS]; //this is the array of leds

void setup() {
delay(1000); //1000 millisecond delay


void loop() {

for (int i; i<3; i++) //loop runs 3 times
if ((leds % 2)== 0)
leds[i].setRGB (204, 12, 156); //even numbers play this color
} else{
leds.setRGB (110, 231, 58); //odd numbers play this color

if(brightness == 0 || brightness == 255)
fadeAmount = -fadeAmount ;
//fill rainbow loop
for (int i=0; i<4;i++){//loop runs 4 times
fill_rainbow (leds, NUM_LEDS, 0, 12);
delay (40);

(Marc Miller) #2

You must call FastLED.show() any time you make a change to the pixels and want to actually SEE that change on the strip. If you set the even and odd pixels but don’t call show() before running fill_rainbow you won’t ever see that change.

If you did:

fill_solid( leds, NUM_LEDS, CRGB::Red );
fill_solid( leds, NUM_LEDS, CRGB::Green );
fill_solid( leds, NUM_LEDS, CRGB::Blue );

You would only see blue since the change to red and then to green are never displayed using show().

It doesn’t look like your variable brightness is being updated anywhere. ???

Using (i % 2)== 0 works fine to check for odd vs even. Two other ways are:

if ( mod8(i,2) == 0) {
//is even number
} else {
//is odd number

if ((i & 0x01) == 0) {
// is even number
} else {
//is odd number

When sharing your program please use http://gist.github.com so it will display properly on all devices and line numbers can be referenced for discussion.