Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Arduino - LED stran...
 
Share:
Notifications
Clear all

[Solved] Arduino - LED strand with flag colors ...

17 Posts
2 Users
0 Reactions
4,277 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2864
Topic starter  

This is a continuation of the comments under the Arduino WS2812 LEDs article (comment) in which Chris and Hans discuss how to get started.

"Basically what I want to do, is assign individual colors to the 150 LEDs at will; e.g. all red, all blue, all green or mixtures thereof. I would also like to use the strip to create flag colours on the strip such as 3red, 3white, 3blue (Dutch flag) or 3 red, 3white, 3green, (Italian) and repeat this for all following LED’s. Not being familiar with programming I don’t know how simple/difficult this is, but I would be very grateful if someone could point me in the right direction."


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2864
Topic starter  

Question by Chris;

I would however still like to know how I can get the following effect: run from LED 1 to Led 150, pause when the whole string is lit for x minutes and repeat this routine. Where exactly should the modification be inserted (end (bottom) of the program. beginning etc.

Chris could you post the full working code here? 
This way I can figure where to make the modifications.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2864
Topic starter  

So if I understand you correctly: matching the desired color pattern, light LED1, LED2,... LED150. Wait a few seconds. Blank all LEDs. Rinse and repeat?


   
ReplyQuote
(@chris)
Active Member
Joined: 10 years ago
Posts: 7
 

Hi,

The code I am using at present is as below. (Please forgive me if this is not correctly formatted - I hope I am doing it right).  Basically this generates a series of strip flags which remain (static) ON..  What I would like to do is have each LED turn on in sequence (running) in this pattern.  When all LEDs are on, I would like the flags to display for "x" minutes, after which the whole sequence repeats itself.  I hope this all makes sense.  I am totally illiterate of the appropriate terminology/jargon

#include "FastLED.h"
// Number of RGB LEDs in the strand
#define NUM_LEDS 150 
// Define the array of leds
CRGB leds[NUM_LEDS];
// Arduino pin used for Data
#define PIN 6 
// Define a few colors
#define Red 0x00FF00
#define White 0xFFFFFF
#define Blue 0x0000FF
#define Green 0xFF0000
#define Black 0x000000
#define Yellow 0xFFFF00
#define Orange 0x5FF500
#define DimGray 0x050505
#define Lightblue 0xBB00AA
uint32_t flagcolors[5][30] = { 
    { Red, Red, Red, Red, White, White, White, White, Blue, Blue, 
       Blue, Blue, Black, Red, Red, Red, Red, White, White, White, 
       White, Green, Green, Green, Green, Black, Red, Red, Red, Red }, // row 1
    { Yellow, Yellow, Yellow, Yellow, DimGray, DimGray, DimGray, DimGray, Black, Yellow, 
       Yellow, Yellow, Yellow, Red, Red, Red, Red, Yellow, Yellow, Yellow, 
       Yellow, Black, Blue, Blue, Blue, Blue, White, White, White, White }, // row 2
    { Red, Red, Red, Red, Black, Lightblue, Lightblue, Lightblue, Lightblue, White, 
       White, White, White, Lightblue, Lightblue, Lightblue, Lightblue, Black, Red,Red, 
       Red, Red, White, White, White, White, Lightblue, Lightblue, Lightblue, Lightblue }, // row 3
    { Black, Red, Red, Red, Red, White, White, White, White, 
      Red, Red, Red, Red, Black, Lightblue, Lightblue, Lightblue, Lightblue, Yellow, 
      Yellow, Yellow, Yellow, Lightblue, Lightblue, Lightblue, Lightblue, Black, Red, Red }, // row 4
    { Red, Red, White, White, White, White, Blue, Blue, Blue, Blue, 
       Black, Orange, Orange, Orange, Orange, White, White, White, White, Green,
       Green, Green, Green, Red, White, Blue, Orange, Lightblue, Green, Yellow } // row 5
  };
void setup()
{
  FastLED.addLeds<NEOPIXEL, PIN, RGB>(leds, NUM_LEDS);  
  // For setting the flag one time
  for(int row = 0; row < 5; row++) {
    for(int col = 0; col < 30; col++) {
      leds[ (row*30)+col ] = flagcolors[row][col];
    }
  }  
  FastLED.show();
}
void loop() {
  // put your main code here, to run repeatedly:
}

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2864
Topic starter  

Hi Chris,

yep you did it right ...

As for the idea, yes explaining the idea can be challenging, for me as well ... 

As for you code - I'd change it to something like this (play with the delays - this is just the setup() and loop() part, the rest remains the same):  

void setup()
{
  FastLED.addLeds<NEOPIXEL, PIN, RGB>(leds, NUM_LEDS);  
}
void loop() // this keeps repeating: draw flag colors, wait, wipe strip to black,... {
  // For setting the flag one time
  for(int row = 0; row < 5; row++) {
    for(int col = 0; col < 30; col++) {
      leds[ (row*30)+col ] = flagcolors[row][col];
      delay(100);   // 0.1 second slow down FastLED.show(); // Light up one at a time
    }
  delay(5000);   // 5 seconds // All LEDs OF for(int i = NUM_LEDS-1 ; i >= 0; i-- ) {
    led = 0;
    FastLED.show();
    delay(1); // You can remove this (instant black, or increase the delay)
  }  
}

To be clear, of your original code everything before "void setup()" should remain the same.
As of "void setup()" should be replaced with this (untested) code.


   
ReplyQuote
(@chris)
Active Member
Joined: 10 years ago
Posts: 7
 

Thanks Hans,

It is quite late here in Brisbane now.  I'll have a go tomorrow.

Chris

ps Thank you so much for your patience and perserverance


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2864
Topic starter  

No problem and you're most welcome!

Let me know when you've tried it ... 


   
ReplyQuote
(@chris)
Active Member
Joined: 10 years ago
Posts: 7
 

Hi Hans,

I have had a chance to try the additional code, but it appears that there may be a minor error somewhere in the last entries.  I have tried a few things, but whatever I do, it only seems to make things worse !

The error code I get is:

Arduino: 1.6.3 (Windows 7), Board: "Arduino Duemilanove or Diecimila, ATmega328"

Flagsweep.ino: In function 'void loop()':

Flagsweep.ino:51:1: error: 'led' was not declared in this scope

Flagsweep.ino:55:1: error: expected '}' at end of input

Error compiling.

  This report would have more information with

  "Show verbose output during compilation"

  enabled in File > Preferences.

The complete sketch I used, now looks as follows:

#include "FastLED.h"
// Number of RGB LEDs in the strand
#define NUM_LEDS 150 
// Define the array of leds
CRGB leds[NUM_LEDS];
// Arduino pin used for Data
#define PIN 6 
// Define a few colors
#define Red 0x00FF00
#define White 0xFFFFFF
#define Blue 0x0000FF
#define Green 0xFF0000
#define Black 0x000000
#define Yellow 0xFFFF00
#define Orange 0x5FF500
#define DimGray 0x050505
#define Lightblue 0xBB00AA
uint32_t flagcolors[5][30] = { 
    { Red, Red, Red, Red, White, White, White, White, Blue, Blue, 
       Blue, Blue, Black, Red, Red, Red, Red, White, White, White, 
       White, Green, Green, Green, Green, Black, Red, Red, Red, Red }, // row 1
    { Yellow, Yellow, Yellow, Yellow, DimGray, DimGray, DimGray, DimGray, Black, Yellow, 
       Yellow, Yellow, Yellow, Red, Red, Red, Red, Yellow, Yellow, Yellow, 
       Yellow, Black, Blue, Blue, Blue, Blue, White, White, White, White }, // row 2
    { Red, Red, Red, Red, Black, Lightblue, Lightblue, Lightblue, Lightblue, White, 
       White, White, White, Lightblue, Lightblue, Lightblue, Lightblue, Black, Red,Red, 
       Red, Red, White, White, White, White, Lightblue, Lightblue, Lightblue, Lightblue }, // row 3
    { Black, Red, Red, Red, Red, White, White, White, White, 
      Red, Red, Red, Red, Black, Lightblue, Lightblue, Lightblue, Lightblue, Yellow, 
      Yellow, Yellow, Yellow, Lightblue, Lightblue, Lightblue, Lightblue, Black, Red, Red }, // row 4
    { Red, Red, White, White, White, White, Blue, Blue, Blue, Blue, 
       Black, Orange, Orange, Orange, Orange, White, White, White, White, Green,
       Green, Green, Green, Red, White, Blue, Orange, Lightblue, Green, Yellow } // row 5
  };
void setup()
{
  FastLED.addLeds<NEOPIXEL, PIN, RGB>(leds, NUM_LEDS);  
}
void loop() // this keeps repeating: draw flag colors, wait, wipe strip to black,...
{
// For setting the flag one time
for(int row = 0; row < 5; row++) {
for(int col = 0; col < 30; col++) {
leds[ (row*30)+col ] = flagcolors[row][col];
delay(100); // 0.1 second slow down
FastLED.show(); // Light up one at a time 
}
delay(5000); // 5 seconds
// All LEDs OF
for(int i = NUM_LEDS-1 ; i >= 0; i-- ) {
led = 0;
FastLED.show();
delay(1); // You can remove this (instant black, or increase the delay)
}  
}

Cheers,

Chris

 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2864
Topic starter  

Hi Chris,

woops ... typo on my end ... sorry. 

Correction for the "void loop()" part:

void loop()
{
  // For setting the flag one time
  for(int row = 0; row < 5; row++) {
    for(int col = 0; col < 30; col++) {
      leds[ (row*30)+col ] = flagcolors[row][col];
      delay(100); // 0.1 second slow down
      FastLED.show(); // Light up one at a time 
    }
  } // forgot an accolade  here
  delay(5000); // 5 seconds
  // All LEDs OF
  for(int i = NUM_LEDS-1 ; i >= 0; i-- ) {
    leds = 0; // forgot the "s" for ledS
    FastLED.show();
    delay(1); // You can remove this (instant black, or increase the delay)
  }  
}

A little explanation that might help in the future ...

Flagsweep.ino:51:1: error: ‘led’ was not declared in this scope

Each variable has to be declared before we can use it. In this case we defined the variable "leds" as "CRGB leds[NUM_LEDS];" way in the beginning, outside of the void loop() and void setup(), making it a "global" variable, known everywhere in the program/sketch. The variable "leds" is of the type "CRGB" a type only used by the FastLED library.

If a variable (for example: "int anumber;" which would define the variable "anumber" as an integer number) is defined inside for example void loop() or void setup(), then it's a so called "local" variable and only known in that function. "void setup()" and "void loop()" are both functions, returning nothing (indicated with the "void" before the name) and taking no parameters (indicated by the "()" after the name).

A variable can also be defined "on the fly" as we do in the for-loop: "for(int row=0..." where we define again a local variable called "row" of the type integer;

Obviously we did not define a variable called "led", so it fails. Stupid typo haha.

The error:

Flagsweep.ino:55:1: error: expected ‘}’ at end of input

Basically tells us that the compiler has been looking for a closing accolade (}) matching an opening accolade ({) until the end of the code and couldn't find it.

Error messages can be confusing at times (as you have seen before you tried this sketch), but sometimes they really help finding the problem. My most common errors are forgetting the ";" at the end of a line, typo in variable names, or forgetting an accolade somewhere. 

I hope the explanation is helpful ...


   
ReplyQuote
(@chris)
Active Member
Joined: 10 years ago
Posts: 7
 

Hello Hans,

NO it was not just helpful - it was brilliant and it works like a charm !!!  Although it is only 21:00 here, I do get a bit tired and the grey matter does not function that well.  Tomorrow morning I will study your reply in detail and do my best to digest it, because I have to know how and why certain actions are taken.

Thanks once again,

Cheers,

Chris


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2864
Topic starter  

Cool! 

Don't worry about how fast you reply ... take your time, I know there is a significant time difference .


   
ReplyQuote
(@chris)
Active Member
Joined: 10 years ago
Posts: 7
 

Hello Hans,

I have now had a chance to read your previous technical comments in detail.  I read them about three times and I think I have digested the intricacies of opening and closing accolades.  The spelling error in the LED(s) was of course obvious and I should have noticed that myself.  I have played with the timings and the set-up is working exactly as I wanted.  I also think that may be I could have defined whole flags, rather than just the colours that make them up, but there is no way I am going to mess around with the code, since it is working beautifully.  Don't fix it, if it ain't broke.

Thank you once again for your patience and advise - I think I am just a little more litterate now.

Take care and cheers,

Chris


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2864
Topic starter  

Hi Chris!

Awesome, glad to hear it's working. Feel free to post a small video (up to 4Mb) if you'd like (not required of course).

If you want to toy with the code; Copy the code to a new sketch or a separate text document so you'll have it nearby when things go south ... 


   
ReplyQuote
(@chris)
Active Member
Joined: 10 years ago
Posts: 7
 

Hello Hans,

The video has taken longer than I expected, because I had to add some LEDs to the string and the programme would not work.  I was determined to solve the problem myself, which I eventually did.  I was also hoping to make a video of the string installed on my deck, but I now realize that it will take a while..  Furthermore I am trying to reduce the size.  At the moment it is about 20Mb as an *avi file and I will have to try and find some way of reducing the resolution, to get it down to < 4Mb

Thank you once again for your invaluable assistance.

Cheers,

Chris


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2864
Topic starter  

Ah the good old huge video file issue. Yeah I'm familiar with that ... you could try using HandBrake, see if you can compress it more. Handbrake was originally thought for ripping DVD's, but when you select a video file as "input" (instead of a DVD), it will work just fine as well. It has some rudimentary features like cropping and trimming playback time. I have used it for some videos myself.

But,... don't worry, no hurry, and posting a video is not required, I just thought it would be cool to see it in action.
If you have dropbox, then you can share it there as well, and I can take a look and see how far I can get it compressed, once I'm done moving ... 


   
ReplyQuote
Page 1 / 2
Share: