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!





All In One code Two...
 
Share:
Notifications
Clear all

All In One code Two ws2812 strips  

  RSS

jerryc
(@jerryc)
New Member
Joined: 5 months ago
Posts: 3
June 25, 2020 9:47 AM  

Been a couple years now and the single led strip is still working perfectly.  Time to change up a few things. I would like to add a second led strip on a different data pin and have it work in sequence with the first. Could use a hint for a starting place on the code.


ReplyQuote
Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1373
June 26, 2020 4:36 AM  

Do you mean: both strips should do the exact same thing?
In that case you can connect the Din pin of both strips to the same pin. No code changes needed. 😊 

Using a different pin for the second strip is possible as well, however this would only make sense if both strips show different patterns.
If that is going to be your goal, then you'd face bigger problems. An Arduino is not a multitask device, so all effects needs some sorts of interrupt so that the arduino can switch tasks very quickly. In all honesty: I have never seen this work well. The slice of time each strip gets will slow down the effects quite a bit and your code will become rather complex or messy.


ReplyQuote
jerryc
(@jerryc)
New Member
Joined: 5 months ago
Posts: 3
June 26, 2020 8:04 AM  

Reason I am asking as I've built LED strips to use as DRL and Turn signals. I've got that part of the code working. Since they run on two different pins I was wanting to have a cruise/car show mode using the all in one sketch.  


ReplyQuote
Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1373
June 27, 2020 4:55 AM  

I recall helping someone in this forum with a similar application (Turn signal and DRL), no sure how helpful this post is.

Considering your application, I'd probably stick to one strip though.
Say the strip is 40 LEDs wide, you've cut it in the middle (and reconnected +5/GND/Din) so the strip is "one strip" again.
For going left, you could use LEDs 0 - (NUM_LEDS/2), and for going right (NUMLED/2)-NUM_LEDS.
For DRL you use 0 - NUM_LEDS.

This way both left and right can "blink" at the same time, and DRL will be easier.

With 2 strips may work as well, but doing the effects on 2 strips may be a little trickier.
Can you post your current code? I don't mind taking a look.
Also: I'm not sure what effect you'd like to use? Or do you want to use all effects?


ReplyQuote
jerryc
(@jerryc)
New Member
Joined: 5 months ago
Posts: 3
June 27, 2020 3:58 PM  

You are correct in the fact that I cannot drive two separate pins at the same time. The post (Turn signal and DRL) post was very helpful. This is what I have working so far. Yes I do want to use all the effects. I understand the effects and can have them scroll across both strips. Need to delete the blinkcount as if the switches is on then blink will continue.

#include <FastLED.h>
#include <EEPROM.h>

#define NUM_LEDS 88
#define blinkcount 10

CRGB leds[NUM_LEDS];

#define PIN 2
#define BUTTONLEFT 3
#define BUTTONRIGHT 4
#define BUTTON4WAY 5
#define BUTTONCARSHOW 6

byte selectedEffect = 0;
int buttonStateLeft = 0;
int buttonStateRight = 0;
int buttonState4Way = 0;
int buttonStateCarShow = 0;
bool AnyButtonPressed = false;
bool DirectionCorrection = false;

void setup()
{

FastLED.addLeds<WS2812, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
pinMode(BUTTONLEFT, INPUT_PULLUP);
pinMode(BUTTONRIGHT, INPUT_PULLUP);
pinMode(BUTTON4WAY, INPUT_PULLUP);
pinMode(BUTTONCARSHOW, INPUT_PULLUP);
LEDS.setBrightness(254);
// fill all leds with the color white
fill_solid(leds, NUM_LEDS, CRGB(255, 255, 255));
FastLED.show();
}

void loop() {
buttonStateLeft = digitalRead(BUTTONLEFT);
buttonStateRight = digitalRead(BUTTONRIGHT);
buttonState4Way = digitalRead(BUTTON4WAY);
buttonStateCarShow = digitalRead(BUTTONCARSHOW);
AnyButtonPressed = (buttonStateLeft == LOW) || (buttonStateRight == LOW) || (buttonState4Way == LOW) || (buttonStateCarShow == LOW);

if (AnyButtonPressed) {
if (buttonStateLeft == LOW) {
DirectionCorrection = 0;
}
else {
DirectionCorrection = NUM_LEDS;
}
for (int blink = 0; blink < blinkcount; blink++) {

for (int i = 43; i < 88; i++) { // starts at LED no. 44 building up to 88
if (buttonStateLeft == LOW) {
// fill all leds with black
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0));
leds[i].setRGB(255, 130, 0); // Amber
}
else
{
if (buttonStateRight == LOW)
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0));
leds[88 - i].setRGB(255, 130, 0); // starts at LED no. 88 to i (i = 44)
}
FastLED.show();
delay(10); // Build speed from pixel to pixel
}

delay(200); // Duration of the full light
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0));

delay(300); // Duration until it starts again.

fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0));
}
}
else {
fill_solid(leds, NUM_LEDS, CRGB(255, 255, 255));

}

fill_solid(leds, NUM_LEDS, CRGB(255, 255, 255));
FastLED.show();
}

ReplyQuote



Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1373
June 28, 2020 5:28 AM  

While trying to help someone with a similar problem, something occurred to me that may be helpful ...

So with FastLED we use something like this to set a LED:

leds[i].setRGB(255, 130, 0); // Amber

We could create a function for this to flip direction, for example:

void setLED(int aLED, CRGB aColor, bool doReverse)
{
  if(doReverse) 
  {
leds[NUM_LEDS - 1 - aLED] = aColor;
}
else
{
leds[aLED] = aColor;
}
}

So instead of 

leds[i].setRGB(255, 130, 0); // Amber

We now say, for the normal direction:

setLED( i, CRGB(255, 130, 0), false ); // Amber

and for the reverse direction:

setLED( i, CRGB(255, 130, 0), true ); // Amber

Since we're talking about turn lights and DRL, I'd assume the option that only half the strip is used (turn signal), so we can even expand on this function by implementing something like "left", "right" and "both", and we can even implement a mirror effect here.

Something like this (this may be overkill):

#define LEDS_NORMAL 0
#define LEDS_REVERSE 1
#define LEDS_ONLY_LEFT 2
#define LEDS_ONLY_RIGHT 3
#define LEDS_MIRROR 4

....

void setLED(int aLED, CRGB aColor, byte flowDirection)
{
if(flowDirection == LEDS_NORMAL)  // Normal strip use
  {
    leds[aLED] = aColor;
   }
else if(flowDirection == LEDS_REVERSE) // reverse order
  {
    leds[NUM_LEDS - 1 - aLED] = aColor;
  }
else if(flowDirection == LEDS_ONLY_LEFT) // count from left, max = NUM_LEDS/2
  {
  if(aLED < (NUM_LEDS/2) )
{
leds[aLED] = aColor;
}
  }
  else if(flowDirection == LEDS_ONLY_RIGHT) // count from right, max = NUM_LEDS/2
  {
if(aLED < (NUM_LEDS/2) )
{
  leds[NUM_LEDS - 1 - aLED] = aColor;
}
  }
else if(flowDirection == LEDS_MIRROR) // mirror both sides, max = NUM_LEDS/2
  {
if(aLED < (NUM_LEDS/2) )
{
  leds[NUM_LEDS - 1 - aLED] = aColor;
  leds[aLED] = aColor;
}
  }
}

Note :

  • when using only LEFT, RIGHT or MIRROR, your LED range will of course be half of the usual range (NUM_LEDS/2). So in the calling function you will need to consider this.
  • The LEFT and RIGHT function may need a little refinement.
    Right now it only makes sure it stays in their own half, and flips the order when needed.
  • Also not: instead of separate red, green, blue, I've used the data type "CRGB" (FastLED specific).
    There is a function, also called CRGB that converts red, green and blue into this CRGB datatype.
    This also allows you to assign the color directly, and you can do things like this:
setLED( i, CRGB::DarkOrange, true ); // (similar to) Amber

A list of color names can be found here: FastLED predefined colors.


ReplyQuote
Share: