Thanks .
This works too. Now that I'm looking at things differently.
Last input of pattern. Blacked LED 1 and 8.
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
HAHA I just twigged this is what you told me in the first place!!!!!!!!!!!
Thanks .
This works too. Now that I'm looking at things differently.
Last input of pattern. Blacked LED 1 and 8.
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
I regret posting that code it was too complex. Did Paul Mcwhorter explain nested loops?
Here is another version of the code to run a light along the leds using three colors in succession. Instead of repeating the for loop three times each time using a different color you could use a nested loop and a list of colors.
To understand what is going on you need to visualize how the variables are changing.
There are two variables used, ledNumber to select a led number and colorChoice to select the color for that led.
I have inserted print statements to show how the variables change in value each time the statement
leds[ledNumber] = colorList[colorChoice]; is executed.
Just choose Tools, Serial Monitor in menu bar of the Arduino IDE to watch the values change.
#include <Adafruit_NeoPixel.h> #include <FastLED.h> #define LED_PIN 4 #define NUM_LEDS 10 CRGB leds[NUM_LEDS]; CRGB colorList[4] = { {CRGB(0,0,0)}, // NO COLOR {CRGB(255,0,0)}, // RED {CRGB(0,255,0)}, // GREEN {CRGB(0,0,255)}, // BLUE }; void setup() { Serial.begin(9600); FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); FastLED.setBrightness(100); // turn off all leds for (int ledNumber = 0; ledNumber < NUM_LEDS; ledNumber++) { leds[ledNumber] = CRGB(0,0,0); } FastLED.show(); // copy leds[] values to actual leds } int ledNumber; int colorChoice; void loop() { Serial.println(); Serial.println("=====================" Serial.println("Start main loop again"); Serial.println("====================="); Serial.println(); Serial.println("START OUTER LOOP cycles colorChoice values"); for (colorChoice = 1; colorChoice < 4; colorChoice++){ Serial.println(); Serial.println(" START INNER LOOP cycles ledNumber values"); for (ledNumber = 0; ledNumber < NUM_LEDS; ledNumber++) // for each led { Serial.print(" leds["); Serial.print(ledNumber); Serial.print("] = colorChoice["); Serial.print(colorChoice); Serial.println("]"); leds[ledNumber] = colorList[colorChoice]; FastLED.show(); delay(400); leds[ledNumber] = CRGB(0, 0, 0); //turn off led FastLED.show(); delay(400); } } }
Thanks Mate. I will have a play around with it. I will post a video of your modified code in action. I think it looks good and more importantly I understand why things are happening.
PS
I spotted your deliberate mistake in last code.
😉
Cheers,
Davy.
Check it out. Suggestions for improvements welcome obviously.
Cheers,
Davy
#include <FastLED.h> // Fastled library access required for some commands. #define LED_PIN 4 #define NUM_LEDS 10 #define NUM_COLOURS 6 #define NUM_REPEATS #define NUM_REPEATS patdog uint8_t hue = 0; // Define colours used //CRGB colourOn = CRGB(255, 0, 0), colourBack; //CRGB colourOff = CRGB(0, 0, 0); CRGB colourList[6] = {CRGB(0, 0, 0), CRGB(0, 0, 255), CRGB(255, 0, 0),}; CRGB colourListdog[6] = {CRGB(0, 0, 0), CRGB(255, 255, 0), CRGB(0, 255, 0),}; CRGB colourListcat[3] = {CRGB(0, 0, 0), CRGB(0, 255, 0), CRGB(0, 255, 0), }; CRGB leds[NUM_LEDS]; // Define times for colour on and colour off in millis int lengthOn = 10, lengthOff = 10; int patterns[18][10] = {{1, 0, 0, 0, 0, 0, 0, 0, 0, 2}, {0, 1, 0, 0, 0, 0, 0, 0, 2, 0}, {0, 0, 1, 0, 0, 0, 0, 2, 0, 0}, {0, 0, 0, 1, 0, 0, 2, 0, 0, 0}, {0, 0, 0, 0, 1, 2, 0, 0, 0, 0}, {0, 0, 0, 0, 2, 1, 0, 0, 0, 0}, {0, 0, 0, 2, 0, 0, 1, 0, 0, 0}, {0, 0, 2, 0, 0, 0, 0, 1, 0, 0}, {0, 2, 0, 0, 0, 0, 0, 0, 1, 0}, {2, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {0, 2, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 2, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 2, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 2, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 2, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 2, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 2, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; int patdog[18][10] = {{1, 0, 0, 0, 0, 0, 0, 0, 0, 2}, {0, 1, 0, 0, 0, 0, 0, 0, 2, 0}, {0, 0, 1, 0, 0, 0, 0, 2, 0, 0}, {0, 0, 0, 1, 0, 0, 2, 0, 0, 0}, {0, 0, 0, 0, 1, 2, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 2, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 2, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 2, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 2, 0}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 2}, {0, 1, 0, 0, 0, 0, 0, 0, 2, 0}, {0, 0, 1, 0, 0, 0, 0, 2, 0, 0}, {0, 0, 0, 1, 0, 0, 2, 0, 0, 0}, {0, 0, 0, 0, 1, 2, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 2, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 2, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 2, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, }; void setup() { Serial.begin(9600); // Setup FastLED object FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); FastLED.setBrightness(100); for (int i = 0; i < NUM_LEDS; ++i) { leds[i] = colourList[0]; } } void loop() { Serial.println(" " ); for (int j = 0; j < 18; j++) { Serial.println(j); // set color for every LED for (int i = 0; i < 10; i++) { leds[i] = colourList[ patterns[j][i] ]; } FastLED.show(); delay(150); } Serial.println(" " ); for (int j = 0; j < 18; j++) { Serial.println(j); // set color for every LED for (int i = 0; i < 10; i++) { leds[i] = colourListdog[ patterns[j][i] ]; } FastLED.show(); delay(175); } Serial.println(" " ); for (int j = 0; j < 18; j++) { Serial.println(j); // set color for every LED for (int i = 0; i < 10; i++) { leds[i] = colourList[ patterns[j][i] ]; } FastLED.show(); delay(175); } for (int j = 0; j < 4; j++) { Serial.println(j); leds[0] = CRGB(255, 0, 0); //Green LED leds[1] = CRGB(0, 255, 0); //Red LED FastLED.show(); delay(100); leds[0] = CRGB(0, 0, 0); //Green LED leds[1] = CRGB(0, 0, 0); //Red LED FastLED.show(); delay(100); } for (int j = 0; j < 4; j++) { Serial.println(j); leds[2] = CRGB(255, 0, 0); //Green LED leds[3] = CRGB(0, 255, 0); //Red LED FastLED.show(); delay(100); leds[2] = CRGB(0, 0, 0); //Green LED leds[3] = CRGB(0, 0, 0); //Red LED FastLED.show(); delay(100); } for (int j = 0; j < 4; j++) { Serial.println(j); leds[4] = CRGB(255, 0, 255); //Cyan LED leds[5] = CRGB(255, 255, 0); //Yellow LED FastLED.show(); delay(100); leds[4] = CRGB(0, 0, 0); // cyan leds[5] = CRGB(0, 0, 0); //yellowLED FastLED.show(); delay(100); } for (int j = 0; j < 4; j++) { Serial.println(j); leds[6] = CRGB(255, 0, 0); //Green LED leds[7] = CRGB(0, 255, 0); //Red LED FastLED.show(); delay(100); leds[6] = CRGB(0, 0, 0); //Green LED leds[7] = CRGB(0, 0, 0); //Red LED FastLED.show(); delay(100); } for (int j = 0; j < 9; j++) { Serial.println(j); leds[8] = CRGB(255, 0, 0); //Green LED leds[9] = CRGB(0, 255, 0); //Red LED FastLED.show(); delay(100); leds[8] = CRGB(0, 0, 0); //Green LED leds[9] = CRGB(0, 0, 0); //Red LED FastLED.show(); delay(100); } { for (int j = 0; j < 4; j++) { Serial.println(j); leds[8] = CRGB(0, 255, 0); //Green LED FastLED.show(); delay(300); leds[8] = CRGB(0, 0, 0); //Green LED FastLED.show(); delay(300); leds[9] = CRGB(0, 0, 255); //Red LED FastLED.show(); delay(300); leds[9] = CRGB(0, 0, 0); //Red LED FastLED.show(); delay(300); } for (int j = 0; j < 9; j++) { } } }
What is the purpose of the final empty for loop (for j=0 to 9) ?
I was kidnapped by mimes.
They did unspeakable things to me.
How are your leds wired up you don't seem to be using an rgb led strip?
You seem to be getting the idea with the for loops. The FastLED library has a lot more to offer and probably worth your while looking at some examples of its use online.
Rather than have one for loop action after another for loop action in the loop() you can move them out to functions with names reflecting their action.
With your last example you a mixing methods (look up table for values to use vs algorithm to generate those values). I would recommend that you learn to use algorithms. Lists and tables have a use but they aren't teaching you how to improve your programming.
For example if you want two different colours to pass each other you might think how to compute the second led position from the position of the led moving right.
ledNumber1: 0 1 2 3 4 5 6 7 8 9
ledNumber2: 9 8 7 6 5 4 3 2 1 0
Notice that ledNumber2 = 9 - ledNumber1
In this example the red moves left to right while the green moves right to left. This is repeated.
#include <Adafruit_NeoPixel.h> #include <FastLED.h> #define LED_PIN 4 #define NUM_LEDS 10 #define BLACK 0 #define RED 1 #define GREEN 2 #define BLUE 3 CRGB leds[NUM_LEDS]; CRGB colorList[4] = { {CRGB(0,0,0)}, // NO COLOR {CRGB(255,0,0)}, // RED {CRGB(0,255,0)}, // GREEN {CRGB(0,0,255)}, // BLUE }; void setup() { Serial.begin(9600); FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); FastLED.setBrightness(100); // turn off all leds for (int ledNumber = 0; ledNumber < NUM_LEDS; ledNumber++) { leds[ledNumber] = colorList[BLACK]; } FastLED.show(); // copy leds[] values to actual leds } int ledNumber2; // working variable void loop() { for (int ledNumber1 = 0; ledNumber1 < NUM_LEDS; ledNumber1++) // for each led { leds[ledNumber1] = colorList[RED]; ledNumber2 = 9 - ledNumber1; leds[ledNumber2] = colorList[GREEN]; FastLED.show(); delay(400); leds[ledNumber1] = colorList[BLACK]; //turn off led leds[ledNumber2] = colorList[BLACK]; FastLED.show(); delay(400); } }
What is the purpose of the final empty for loop (for j=0 to 9) ?
Slackness on my part. Would have had another loop there as I was experimenting and didn't remove complete loop instruction during edit.
😉
How are your leds wired up you don't seem to be using an rgb led strip?
Green is data IN from Arduino via 330 Ohm resistor, which is then piggy backed to following LED and so on. Final pin on final LED is left as is.
There is also a 100 uF capacitor across + and -.
Using individual addressable LEDs.
Cheers,
Davy.
Missed that one! I didn't know you could buy individual addressable rgb leds or that Bill had covered the topic. Making colour led displays just wasn't on my radar.
Hi Mate thanks for this code. A lot easier to work with colours. One thing I'd like to ask;
How would I add a function to this code to give the effect of the LEDs bouncing off each other without using attached pattern? I have tried a few things but can only get them to run one way IE from led 5-9 and then restarting from 5 again.
Cheers,
Davy.
int patterns[18][10] = {{1,0,0,0,0,0,0,0,0,2},
{0,1,0,0,0,0,0,0,2,0},
{0,0,1,0,0,0,0,2,0,0},
{0,0,0,1,0,0,2,0,0,0},
{0,0,0,0,1,2,0,0,0,0},
{0,0,0,0,1,2,0,0,0,0},
{0,0,0,1,0,0,2,0,0,0},
{0,0,1,0,0,0,0,2,0,0},
{0,1,0,0,0,0,0,0,2,0},
{1,0,0,0,0,0,0,0,0,2},
{0,1,0,0,0,0,0,0,2,0},
{0,0,1,0,0,0,0,2,0,0},
{0,0,0,1,0,0,2,0,0,0},
{0,0,0,0,1,2,0,0,0,0},
{0,0,0,0,1,2,0,0,0,0},
{0,0,0,1,0,0,2,0,0,0},
{0,0,1,0,0,0,0,2,0,0},
{0,1,0,0,0,0,0,0,2,0}};
#include <Adafruit_NeoPixel.h> #include <FastLED.h> #define LED_PIN 4 #define NUM_LEDS 10 #define BLACK 0 #define RED 1 #define GREEN 2 #define BLUE 3 #define YELLOW 4 #define PURPLE 5 #define GRAY 6 CRGB leds[NUM_LEDS]; CRGB colorList[7] = { {CRGB(0,0,0)}, // NO COLOR {CRGB(255,0,0)}, // RED {CRGB(0,255,0)}, // GREEN {CRGB(0,0,255)}, // BLUE {CRGB(255,255,0)}, // YELLOW {CRGB(102,0,51)}, // PURPLE {CRGB(51,51,0)}, // GRAY }; void setup() { Serial.begin(9600); FastLED.addLeds<WS2812, LED_PIN, RGB>(leds, NUM_LEDS); FastLED.setBrightness(100); // turn off all leds for (int ledNumber = 0; ledNumber < NUM_LEDS; ledNumber++) { leds[ledNumber] = colorList[BLACK]; } FastLED.show(); // copy leds[] values to actual leds } int ledNumber2; // working variable void loop() { for (int ledNumber1 = 0; ledNumber1 < NUM_LEDS; ledNumber1++) // for each led { leds[ledNumber1] = colorList[BLUE]; ledNumber2 = 9 - ledNumber1; leds[ledNumber2] = colorList[BLUE]; FastLED.show(); delay(400); leds[ledNumber1] = colorList[BLACK]; //turn off led leds[ledNumber2] = colorList[BLACK]; FastLED.show(); delay(400); } for (int ledNumber1 = 0; ledNumber1 < NUM_LEDS; ledNumber1++) // for each led { leds[ledNumber1] = colorList[RED]; ledNumber2 = 9 - ledNumber1; leds[ledNumber2] = colorList[YELLOW]; FastLED.show(); delay(400); leds[ledNumber1] = colorList[BLACK]; //turn off led leds[ledNumber2] = colorList[BLACK]; FastLED.show(); delay(400); } for (int ledNumber1 = 0; ledNumber1 < NUM_LEDS; ledNumber1++) // for each led { leds[ledNumber1] = colorList[PURPLE]; ledNumber2 = 9 - ledNumber1; leds[ledNumber2] = colorList[GRAY]; FastLED.show(); delay(400); leds[ledNumber1] = colorList[BLACK]; //turn off led leds[ledNumber2] = colorList[BLACK]; FastLED.show(); delay(400); } }
Mate, further to my previous post....
With your last example you a mixing methods (look up table for values to use vs algorithm to generate those values). I would recommend that you learn to use algorithms. Lists and tables have a use but they aren't teaching you how to improve your programming.
For example if you want two different colours to pass each other you might think how to compute the second led position from the position of the led moving right.
ledNumber1: 0 1 2 3 4 5 6 7 8 9
ledNumber2: 9 8 7 6 5 4 3 2 1 0Notice that ledNumber2 = 9 - ledNumber1
In this example the red moves left to right while the green moves right to left. This is repeated.
So the bouncing effect IE when one colour starts at 0 to 4 and second colour starts from 9 to 5 would look like this?
ledNumber1: 0 1 2 3 4 4 3 2 1 0
ledNumber2: 9 8 7 6 5 5 6 7 8 9
If so, how is this applied?
Thanks,
Davy.
So the bouncing effect IE when one colour starts at 0 to 4 and second colour starts from 9 to 5 would look like this?
ledNumber1: 0 1 2 3 4 4 3 2 1 0
ledNumber2: 9 8 7 6 5 5 6 7 8 9
No. The colours just move all the way through each other. One left to right and the other right to left.
How would I add a function to this code to give the effect of the LEDs bouncing off each other without using attached pattern?
The fun part of programming is working this out yourself 🙂 That means learning more about programming. For example making use of the conditional statement if (this is true) do X else do Y
The method I used below, to give the bounce effect, uses variables, color1 and color2 to hold the colour numbers and swap their values half way through the for loop. I added a swapInt function to perform the swap task.
I have also added a loopCounter variable to count how many times the main loop() is executed. This is used to swap the colours. The pair of colour variables are changed three times after which the loopCounter is reset to repeat the colour pairs again.
I can't run the program so I just hope it behaves as I think it will.
#include <Adafruit_NeoPixel.h> #include <FastLED.h> #define LED_PIN 4 #define NUM_LEDS 10 #define BLACK 0 #define RED 1 #define GREEN 2 #define BLUE 3 #define YELLOW 4 #define PURPLE 5 #define GRAY 6 CRGB leds[NUM_LEDS]; CRGB colorList[7] = { {CRGB(0,0,0)}, // NO COLOR {CRGB(255,0,0)}, // RED {CRGB(0,255,0)}, // GREEN {CRGB(0,0,255)}, // BLUE {CRGB(255,255,0)}, // YELLOW {CRGB(102,0,51)}, // PURPLE {CRGB(51,51,0)}, // GRAY }; // == this is a function to swap values between two integer variables ===== void swapInt(int &a, int &b) { int c = a; a = b; b = c; } void setup() { Serial.begin(9600); FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); FastLED.setBrightness(100); // turn off all leds for (int ledNumber = 0; ledNumber < NUM_LEDS; ledNumber++) { leds[ledNumber] = colorList[BLACK]; } FastLED.show(); // copy leds[] values to actual leds } int ledNumber2; // working variable int color1, color2; // stores two colors int loopCounter = 0; // counts times loop() is repeated void loop() { // decide what pair of colors to use for this loop() count if (loopCounter == 0){ color1 = RED; color2 = BLUE; } if (loopCounter == 1){ color1 = GREEN; color2 = YELLOW; } if (loopCounter == 2){ color1 = PURPLE; color2 = GRAY; } // update loopCounter loopCounter = loopCounter + 1; if (loopCounter == 3){ loopCounter = 0; // reset counter } for (int ledNumber1 = 0; ledNumber1 < NUM_LEDS; ledNumber1++) // for each led { // swap colors half way to give bounce off each other effect if (ledNumber1 == 5){ swapInt(color1,color2); } leds[ledNumber1] = colorList[color1]; ledNumber2 = 9 - ledNumber1; leds[ledNumber2] = colorList[color2]; FastLED.show(); delay(400); leds[ledNumber1] = colorList[BLACK]; //turn off led leds[ledNumber2] = colorList[BLACK]; FastLED.show(); delay(400); } }