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 - Strobe 6 ...
 
Share:
Notifications
Clear all

[Solved] Arduino - Strobe 6 LED strip segments in different colors.

11 Posts
2 Users
0 Reactions
2,746 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

This is a continuation of a question in the comment section of this post.

Original comment by Dan:

I am trying to figure out how to program segments of a strip to strobe in different colors and different strobes. The closest example i can think of is police lights. I have a strip of 144 pixels that work great with your code but I would like to start with 6 segments. I believe i would have to add and int for segment 1, segment 2 etc. Just at a loss how to do that using your examples.

Eventually i would like to add other effects and rotate through them with a push button.


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

 jFirst of all, I'd like to reference the "Arduino LED strip Effects" article, for a simple framework to work with - it just makes it easier to work more generic. I can imagine there are better ways to make a generic "framework", but this works well for now.

I understand the idea to be this:

144 LEDs, divided in 6 segments of 24 LEDs each. Each segment has it's own color, and all segments should strobe.
At a later time, a button should be added to do a different effect. I'll focus on the strobing effect of 6 segments for now.

The strobe effect in the "Arduino LED strip Effects" looks like this:

void loop() { 
  Strobe(0xff, 0xff, 0xff, 10, 50, 1000);
}
void Strobe(byte red, byte green, byte blue, int StrobeCount, int FlashDelay, int EndPause){
  for(int j = 0; j < StrobeCount; j++) {
    setAll(red,green,blue);
    showStrip();
    delay(FlashDelay);
    setAll(0,0,0);
    showStrip();
    delay(FlashDelay);
  }
 
 delay(EndPause);
}

Since we assume 6 fixed colors and a continuous strobe effect, some of the strobe() parameters can be dropped, like color and strobe count. The pause at the end of a number of flashes is probably not useful here either.

To keep it a continues "loop", you could consider moving the strobe function code into the loop(), but at a later time you'd want other functions as well, so it might be a better idea to keep the strobe function separate.

void loop() { 
  Strobe(50);
}
void Strobe(int FlashDelay){
  for(int j = 0; j < 5; j++) { // count 0 - 5 (6 segments)
    for(int i = 0; i < 23; i++) { // set the 6 segments
      switch(j) {
        case 0: setPixel( (j*24)+i, 0xff, 0x00, 0x00 ); // 1st segment, RED
        case 1: setPixel( (j*24)+i, 0x00, 0xff, 0x00 ); // 2nd segment, GREEN
        case 2: setPixel( (j*24)+i, 0x00, 0x00, 0xff ); // 3rd segment, BLUE
        case 3: setPixel( (j*24)+i, 0xff, 0xff, 0x00 ); // 4th segment, YELLOW
        case 4: setPixel( (j*24)+i, 0xff, 0x00, 0xff ); // 5th segment, PINK
        case 5: setPixel( (j*24)+i, 0x00, 0xff, 0xff ); // 6th segment, CYAN
      }
    }
  showStrip(); // show colors
  delay(FlashDelay); // wait a little
  setAll(0,0,0); // set all to black (OFF)
  showStrip();
  delay(FlashDelay);
  }
}

I have not tested this code, but I suspect it should do the trick.


   
ReplyQuote
 dan
(@dan)
Active Member
Joined: 9 years ago
Posts: 5
 

i appreciate the code but its not working as expected. I am going to see if i can figure it out myself for a bit, its certainly a step in the right direction though. 


   
ReplyQuote
 dan
(@dan)
Active Member
Joined: 9 years ago
Posts: 5
 

 I tried (sigh) But there are 2 things odd and 1 thing i neglected to mention and i"m not sure how to fix any of them

1. There are only 5 segments lit. The last 24 pixels do not light up. The math looks right. 0-5 segments for a total of 6 segments with 24 pixels each

2. All of the segments use the case 5 color (cyan) After changing all the colors back and forth, i changed case 5 to red and all the segments changed to that color.

3. I actually did want to be able to tweak the strobes on each segment individually, with all the strobe functionality you have on the original strobe example.

I'm sure you are busy with your regular life but any help would be greatly appreciated. 

  


   
ReplyQuote
 dan
(@dan)
Active Member
Joined: 9 years ago
Posts: 5
 

Looking through your forum it appears you may have already solved my problem in another post. Will give that a try.


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

It's a woopsie in my code ... typo in the for loop:

for(int j = 0; j < 5; j++) { // count 0 - 5 (6 segments)

should be:

for(int j = 0; j <= 5; j++) { // count 0 - 5 (6 segments)

Never write code before you had your coffee haha ....
j < 5 means count: 0, 1, 2, 3, 4
j <= 5: 0, 1, 2, 3, 4, 5


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

Strobing the individual segments is definitely possible, but it will take some more work and tinkering with the timing.

It becomes a little challenging when you'd like all segments to strobe, independently of each other.
Arduino doesn't know real multitasking or task switching, so we'd need to figure out something smart so it looks like the segments strobe individually.


   
ReplyQuote
 dan
(@dan)
Active Member
Joined: 9 years ago
Posts: 5
 

That certainly fixed the last segment

Thanks!

Do you know why i can only get the last segment color to show on all segments? I've tried changing each individual segment to a different color but only the last one seems to have any effect.


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

Another blunder on my end ... I forgot to add breaks after each case statement.

void loop() { 
  Strobe(50);
}
void Strobe(int FlashDelay){
  for(int j = 0; j < 5; j++) { // count 0 - 5 (6 segments)
    for(int i = 0; i < 23; i++) { // set the 6 segments
      switch(j) {
        case 0: setPixel( (j*24)+i, 0xff, 0x00, 0x00 ); break; // 1st segment, RED
        case 1: setPixel( (j*24)+i, 0x00, 0xff, 0x00 ); break; // 2nd segment, GREEN
        case 2: setPixel( (j*24)+i, 0x00, 0x00, 0xff ); break; // 3rd segment, BLUE
        case 3: setPixel( (j*24)+i, 0xff, 0xff, 0x00 ); break; // 4th segment, YELLOW
        case 4: setPixel( (j*24)+i, 0xff, 0x00, 0xff ); break;// 5th segment, PINK
        case 5: setPixel( (j*24)+i, 0x00, 0xff, 0xff ); break; // 6th segment, CYAN
      }
    }
    showStrip(); // show colors
    delay(FlashDelay); // wait a little
    setAll(0,0,0); // set all to black (OFF)
    showStrip();
    delay(FlashDelay);
  }
}

My apologies ... kind-a what happens when you write code without coffee and without the ability to test ... 


   
ReplyQuote
 dan
(@dan)
Active Member
Joined: 9 years ago
Posts: 5
 

It's been awhile, as you know life certainly gets in the way. 

But I'm back and have with your help, and lots of reading, have learned a little more.

Which just adds more questions that I just can't seem to find simple answers to.

I am trying to add your intStrobecount to the code you made above, but it does not seem to work.

Actually i have gotten the effect i want. I made this code 

#include "FastLED.h"
// How many leds in your strip?
#define NUM_LEDS 144
// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 3
#define CLOCK_PIN 13
CRGBArray<NUM_LEDS> leds;
// Define the array of leds
CRGBSet partA(leds(0,47)); // Define custom pixel range with a name.
CRGBSet partB(leds(48,95)); // Define custom pixel range with a name.
CRGBSet partC(leds(96,144)); // Define custom pixel range with a name.


void setup() { 
      
     FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
     
}
void loop() { 
  // Turn the LED on, then pause
 
//=============================================PartA======================================================
partA = CRGB(0,0,0);
 FastLED.show();
  delay(50);
  partA = CRGB(255,0,0);
 FastLED.show();
  delay(50);
partA = CRGB(0,0,0);
 FastLED.show();
  delay(50);
  partA = CRGB(255,0,0);
 FastLED.show();
  delay(50);
  partA = CRGB(0,0,0);
 FastLED.show();
  delay(50);
//=====================================PartB============================================
partB = CRGB(0,0,0);
 FastLED.show();
  delay(50);
  partB = CRGB(0,255,0);
 FastLED.show();
  delay(50);
  partB = CRGB(0,0,0);
 FastLED.show();
  delay(50);
   partB = CRGB(0,255,0);
 FastLED.show();
  delay(50);
  partB = CRGB(0,0,0);
 FastLED.show();
  delay(50);
//===================================PartC==============================================
partC = CRGB(0,0,0);
 FastLED.show();
  delay(50);
  partC = CRGB(255,255,0);
 FastLED.show();
  delay(50);
  partC = CRGB(0,0,0);
 FastLED.show();
  delay(50);
   partC = CRGB(255,255,0);
 FastLED.show();
  delay(50);
  partC = CRGB(0,0,0);
 FastLED.show();
  delay(50);
}

But i am trying to understand your style of programming, I've tried to add the StrobeCount myself,

I'm not getting errors but it's not working, I think i'm missing something fundamental

void loop() { 
  Strobe(150, 2);
}
void Strobe(int FlashDelay, int StrobeCount){ 
  for(int k = 0; k < StrobeCount; k++) // trying to get 2 strobes from each segment
  for(int j = 0; j < 3; j++) { // count 0 - 1 (3 segments)
    for(int i = 0; i < 47; i++) { // set the 3 segments
      switch(j) {
        case 0: setPixel( (j*48)+i, 0xff, 0x00, 0x00 ); break; // 1st segment, RED
        case 1: setPixel( (j*48)+i, 0x00, 0xff, 0x00 ); break; // 2nd segment, GREEN
        case 2: setPixel( (j*48)+i, 0xff, 0xff, 0x00 ); break; // 2nd segment, YELLOW
      
      }
    }
    showStrip(); // show colors
    delay(FlashDelay); // wait a little
    setAll(0,0,0); // set all to black (OFF)
    showStrip();
    delay(FlashDelay);
  }
}

Do you know of a book that focuses of what I'm trying to learn? Or is this just common programming knowledge? 


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

Hi Dan,

sorry for the late reply.

It looks like you forgot a opening and closing accolade ({ and }) for the "for(int k=" loop - I've marked it in the code below:

void loop() { 
  Strobe(150, 2);
}
void Strobe(int FlashDelay, int StrobeCount) { 
  for(int k = 0; k < StrobeCount; k++) // trying to get 2 strobes from each segment  <-- missing a "{"
    for(int j = 0; j < 3; j++) { // count 0 - 1 (3 segments)
      for(int i = 0; i < 47; i++) { // set the 3 segments
        switch(j) {
          case 0: setPixel( (j*48)+i, 0xff, 0x00, 0x00 ); break; // 1st segment, RED
          case 1: setPixel( (j*48)+i, 0x00, 0xff, 0x00 ); break; // 2nd segment, GREEN
          case 2: setPixel( (j*48)+i, 0xff, 0xff, 0x00 ); break; // 2nd segment, YELLOW
        }
      }
    showStrip(); // show colors
    delay(FlashDelay); // wait a little
    setAll(0,0,0); // set all to black (OFF)
    showStrip();
    delay(FlashDelay);
  }
  } // <-- missing a close accolade
}

   
ReplyQuote
Share: