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 most likely no translated for the post!





LED Matrix: Create ...
 
Share:
Notifications
Clear all

LED Matrix: Create the American Flag in a LED matrix  

Page 1 / 2
  RSS

Hans
 Hans
(@hans)
Prominent Member Admin
Joined: 7 years ago
Posts: 829
January 19, 2017 3:37 AM  

This topic was started based on a request by 'Cam' who was looking for example code to create an American flag in a LED matrix, using WS2812 LED strips and an Arduino. See this comment under the Arduino WS2812 article.


ReplyQuote
Hans
 Hans
(@hans)
Prominent Member Admin
Joined: 7 years ago
Posts: 829
January 19, 2017 4:17 AM  

OK, so for this topic I assume that Arduino, LED strip etc work.
Make sure you've gone that route before starting to read these posts (to get started: Arduino and WS2812 and LED Strip Effects).

The LED Strip Effects article is a good one to take as a base, so the user can pick either FastLED of NeoPixel ...

To make a flag with a matrix, I'll assume that we use 3 colors: Red, White and Blue. 
Now I'll make it so that other flags hopefully can be made this way as well so be aware that there are more efficient ways of doing this, depending on the flag of your choice.

First we will define the colors, in this case 3 colors: red, white and blue (see more about arrays for Arduino).

byte Color1[3] = { 0xFF, 0x00, 0x00 }; // Define color 1 as red (RGB)
byte Color2[3] = { 0xFF, 0xFF, 0xFF }; // Define color 2 as white (RGB)
byte Color3[3] = { 0x00, 0x00, 0xFF }; // Define color 3 as blue (RGB)  

Next we create a matrix for the flag, where we use "1" for Color1, "2" for Color2, and "3" for Color3.
Our matrix will be 30 columns (X) and 8 rows (Y) - but feel free to modify this later on ...
I know this looks confusing since I'm swapping X and Y in the array, it does however help when you're defining the array:

byte FlagColors[8][30] = { { 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                           { 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                           { 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 
                           { 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                           { 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                           { 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                           { 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                           { 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; 

Now here I run into problem number 1 ... 

First of all, to be able to display the American flag, I'd need at least 13 rows (red/white bars), and you've chose to use only 8.
Next problem is displaying the stars.  If we want to depict every star, we'd need 9 rows for that.

Combining the two would at least require 15 rows ... the first 9 would be needed for a crude way of displaying the stars, but when we look at the American flag, you'll see that there are 6 more rows needed to complete the horizontal red/white stripes.

So my first question are:

If we stick to 30 columns and 8 rows, how would you fill in the matrix (see above)?
Or would you prefer to start using more rows?


ReplyQuote
maxxgold
(@maxxgold)
New Member
Joined: 3 years ago
Posts: 1
January 19, 2017 6:35 AM  

First of all let me say thank you very much, your help is greatly appreciated!

I'm not concerned with the proportions of the matrix that I have created. I just separated the WS2812 strip at the solder joints to make things easier. Once I get a good understanding of how to do this on my own I plan to order some more LEDS and create a enclosure for a more proportionate flag. So right now the 30x x 8y matrix will work for me. 

I completely understand that you have assigned the colors to the numbers 1,2, and 3 to use in the matrix you have created, and I understand the matrix. I have taken calculus so I understand the math, I have just never used it for programming.

I'm going to look at the link you posted for arrays, and I'm going to start in on your course!


ReplyQuote
cam
 cam
(@cam)
Active Member
Joined: 3 years ago
Posts: 7
January 19, 2017 7:08 AM  

Hey,

It definitely wasn't me  I'm not american and I'm not a patriot (as a clue to where I'm from... )


ReplyQuote
Hans
 Hans
(@hans)
Prominent Member Admin
Joined: 7 years ago
Posts: 829
January 20, 2017 7:31 AM  

@maxxgold; OK, let me know where I can help - I'd be happy to! 

@cam; Yeah I'm not American either, but ... I live there at the moment though 


ReplyQuote



philxu
(@philxu)
New Member
Joined: 5 years ago
Posts: 1
March 16, 2017 9:25 PM  

it seems the ws2813 is the same protocol as ws2812b,

though it is dual signal,

so could the ws2813 led use the same code ?


ReplyQuote
lferrer
(@lferrer)
Active Member
Joined: 2 years ago
Posts: 7
June 30, 2018 9:07 PM  

Hello !

Thanks for this wonderful topic,

I know this is super old but do you guys finalized this ? can you post the complete sketch (code)?

My plan is to make this in a wood panel 30 inches by 25 inches, but this is where it gets tricky.

I want to fade in the USA Flag, then fade out and Fade In my Puerto Rico flag then fade out and loop it.

Im new to playing with leds, but i knows the basics of programming so it wont be that hard.

I just want to gift this to my mom and dad <3

thanks in advance !!


ReplyQuote
Hans
 Hans
(@hans)
Prominent Member Admin
Joined: 7 years ago
Posts: 829
July 2, 2018 11:12 PM  

Hi lferrer,

That sounds like a very cool plan! I have no idea if maxxgold and cam ever finished the project.

It should not be too hard though, but the challenge will be the stars in the blue section of the US flag.

I'd define 2 arrays, one for each flag.
Start with flag 1 (US) and display it.
Next step is to slowly fade the colors to black (LED off).
Once the fade is done, show the second flag.
Reapply the fade.
Go to the beginning again (show US flag).

I'm no where near my gear so I cannot test anything, but following the scheme of predefining 3 colors (or more), this should not be too complicated.
When using FastLED, you could try the setBrightness() function which will save a lot of work.

So:

Display Flag 1
for (int i=255; i>=0; i--) { FastLED.setBrightness(i); }
Display Flag 2
for (int i=255; i>=0; i--) { FastLED.setBrightness(i); }

Hope this helps you get started. Obviously I'm curious to see the result 


ReplyQuote
lferrer
(@lferrer)
Active Member
Joined: 2 years ago
Posts: 7
July 17, 2018 1:09 AM  

Alright,

Thanks for the reply.

Just got the parts today from china lol.

I will be testing this soon.


ReplyQuote
lferrer
(@lferrer)
Active Member
Joined: 2 years ago
Posts: 7
August 11, 2018 4:15 AM  

Hello,

Finally im trying to do this on a scale prototype before doing it bigger scale.
If using one of the flag it works perfect and i like it this way but i dont know how to add the two flags to show up when one flag is done lol. I am pretty sure i will find an example of this but before i am going to post here and see if i get the answer here.
The sketch has the two flags code but one is "//"
The other thing is that i am using for now the "#define Red 0xFF0000" etc etc until i get more use to arrays and matrixes.
Other thing is that my led strips is join in serpentine way, thats why you see blue at the beginning then blue at the end etc.
I will post the sketch and a video to see how i can get help.
#include <avr/pgmspace.h>  // Needed to store stuff in Flash using PROGMEM
#include "FastLED.h" // Fastled library to control the LEDs
// How many leds are connected?
#define NUM_LEDS 195
// Define the Data Pin
#define DATA_PIN 6 // Connected to the data pin of the first LED strip
// Define the array of leds
CRGB leds[NUM_LEDS];
// Define Colors
#define Red 0xFF0000
#define White 0xFFFFFF
#define Blue 0x0000FF
#define Green 0x00FF00
#define Black 0x000000
#define Yellow 0xFFFF00

//uint32_t flagcolors[13][15] = { 
// { White, Blue, White, Blue, White, Blue, White, Red, Red, Red, Red, Red, Red, Red, Red, }, // row 1
// { White, White, White, White, White, White, White, White, Blue, White, Blue, White, Blue, White, Blue, }, // row 2
// { White, Blue, White, Blue, White, Blue, White, Red, Red, Red, Red, Red, Red, Red, Red, }, // row 3
// { White, White, White, White, White, White, White, White, Blue, White, Blue, White, Blue, White, Blue, }, // row 4
// { White, Blue, White, Blue, White, Blue, White, Red, Red, Red, Red, Red, Red, Red, Red, }, // row 5
// { White, White, White, White, White, White, White, White, Blue, White, Blue, White, Blue, White, Blue, }, // row 6
// { Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, }, // row 7
// { White, White, White, White, White, White, White, White, White, White, White, White, White, White, White, }, // row 8
// { Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, }, // row 9
// { White, White, White, White, White, White, White, White, White, White, White, White, White, White, White, }, // row 10
// { Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, }, // row 11
// { White, White, White, White, White, White, White, White, White, White, White, White, White, White, White, }, // row 12
// { Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, } // row 13
    
//};  
uint32_t flagcolors[13][15] = {
  { Blue, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, },
  { Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Blue, Blue, }, // row 1
  { Blue, Blue, Blue, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, }, // row 1a
  { White, White, White, White, White, White, White, White, White, White, White, Blue, Blue, Blue, Blue, }, // row 2
  { Blue, Blue, Blue, Blue, Blue, White, White, White, White, White, White, White, White, White, White, }, // row 2a
  { Red, Red, Red, Red, Red, Red, Red, Red, Red, Blue, Blue, Blue, White, Blue, Blue, }, // row 3
  { Blue, White, White, White, Blue, Blue, Blue, Red, Red, Red, Red, Red, Red, Red, Red, }, // row 
  { Red, Red, Red, Red, Red, Red, Red, Red, Red, Blue, Blue, Blue, White, Blue, Blue, }, // row 3a
  { Blue, Blue, Blue, Blue, Blue, White, White, White, White, White, White, White, White, White, White, }, // row 4
  { White, White, White, White, White, White, White, White, White, White, White, Blue, Blue, Blue, Blue, }, // row 4a
  { Blue, Blue, Blue, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, }, // row 5
  { Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Blue, Blue, }, // row 5a
  { Blue, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, Red, }
};
void setup() { 
FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds, NUM_LEDS); // Init of the Fastled library
FastLED.setBrightness(15);
}
void loop() { 

 // Light Up
  for(int row = 0; row < 13; row++) {
    for(int col = 0; col < 15; col++) {
      leds[ (row*15)+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 OFF
  for(int i = NUM_LEDS-1 ; i >= 0; i-- ) {
    leds = 0; 
    FastLED.show();
    delay(1);    
  }  
}

ReplyQuote



lferrer
(@lferrer)
Active Member
Joined: 2 years ago
Posts: 7
August 11, 2018 4:22 AM  

I dont know wheres my video ...

Here the link


ReplyQuote
lferrer
(@lferrer)
Active Member
Joined: 2 years ago
Posts: 7
August 22, 2018 3:56 AM  

Hans,

Im still struggling with this lol


ReplyQuote
Hans
 Hans
(@hans)
Prominent Member Admin
Joined: 7 years ago
Posts: 829
August 22, 2018 9:48 PM  

The video looks cool, although I'd go a little faster 

What are you struggling with? How can I help?


ReplyQuote
lferrer
(@lferrer)
Active Member
Joined: 2 years ago
Posts: 7
August 23, 2018 12:01 AM  

Hello!

well im struggling with how to code or tell the sketch to join or jump from one flag to the other one when is done.

So far i only managed to see one flag at a time doing the comment/uncomment thing.

So how do i tell arduino that first do PR flag then do USA flag?

In the future i will add scrolling text between flags but thats for another class lol.


ReplyQuote
lferrer
(@lferrer)
Active Member
Joined: 2 years ago
Posts: 7
August 23, 2018 2:13 PM  

And in the future something more difficult like adding a push button and that way when is pressed i can choose what flag will show? is that even possible with the Arduino ? 

Thanks Hans

ReplyQuote



Page 1 / 2
Share: