Go Down

Topic: [Solved]LED Strip with arrays (Read 707 times) previous topic - next topic

komodo127

May 09, 2019, 03:21 am Last Edit: Jun 04, 2019, 05:31 am by komodo127
Hello. I need to light groups of leds on a strip. I am using Fastled library and Neopixel.

Searching on the net I found something like this

Code: [Select]
#include <FastLED.h>
#include <Adafruit_NeoPixel.h>

#define NUM_LEDS 80
#define DATA_PIN 4
CRGB leds[NUM_LEDS];
#define COLOR_ORDER BGR
#define BRIGHTNESS  20


//THESE ARE MY LED ARRAYS
int myLEDs0[ ] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
int myLEDs1[ ] = {24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47};
int myLEDs2[ ] = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59};
int myLEDs3[ ] = {60, 61, 62, 63, 64, 65, 66, 67, 68, 83, 84, 85, 86, 86, 87, 88};
int myLEDs4[ ] = {69, 70, 77, 78, 79, 80, 81, 82, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98};
int myLEDs5[] = {71, 72, 73, 74, 75, 76, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 124, 125, 126, 127};
int myLEDs6[] = {309, 310, 311, 130, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 128, 129, 130, 131, 132};
int myLEDS7[] = {135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153};
int myLEDS8[] = {154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 191, 192, 193, 194, 195, 196, 197, 198};
int myLEDS9[] = {164, 165, 166, 167, 168, 169, 182, 183, 184, 185, 186, 187, 188, 189};
int myLEDS10[] = {170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181 };
int myLEDS11[] = {199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224};
int myLEDS12[] = {223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246};


Now I only want to build a Loop function that displays the arrays in sequence from 0 to 12. No matter what color. Any suggestion?

Thank you in advance.

[Arduino UNO]
dd if=/dev/brain of=/dev/head

PaulRB

Maybe turn those 12 arrays into a single two-dimensional. Otherwise your code will become very long and repetitive. Problem is, the arrays are not the same length, so it is not straight forward to do.

So first, let us take a step back and explain what is going on and why you need these arrays. Maybe there is a better way.

UKHeliBob

As suggested, the obvious thing to do is to put the arrays into another array thus creating a 2 dimensional array.  You could solve the problem of the child arrays being of different lengths by adding a final entry to each of a value that does not occur in any of them, say -1 and declaring all of the arrays with the same number of elements as the largest.  This would waste memory but that may not matter depending on your program.

By declaring the arrays like that you could pass the array number to a function and iterate through each entry in the array line and do what you need with the value until you find a -1.  At that point you tidy up the display and return from the function

Details of what you are trying to do would help considerably in suggesting solutions
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UKHeliBob

UPDATE :
Using pointers you don't have to waste any memory as each child array can have a different length

Example
Code: [Select]

int a1[] = {1, 2, 3, 10, 11, 12, 10, 11, 12, 10, 11, 12, 10, 11, 12, 10, 11, 12, -1};
int a2[] = {22, 33, -1};
int a3[] = {123, 456, 789, 135, 246, - 1};

int * b1[] = {a1, a2, a3};

void setup()
{
  Serial.begin(115200);
  for (int x = 0; x < 3; x++)
  {
    printArray(x);
  }
}

void loop()
{
}
void printArray(int arrayNum)
{
  byte index = 0;
  while (b1[arrayNum][index] != -1)
  {
    Serial.print(b1[arrayNum][index]);
    Serial.print(" ");
    index++;
  }
  Serial.println();
}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

komodo127

Thank you for the answers.
What we need to do for this project is setting up a station that contains boxes of different kind.
Every time we scan an item the arduino lights up the correct array of LEDs that corresponds to the correct shelf that contains the right box.

The strip of LEDs goes this way



   Box7      Box6         Box5          Box4          Box1 - 3     
______|________|_________|_________|__________    ===========Arduino

   Box8 |    Box9   |    Box10   |     Box11   |   Box12 -13     


The LED strip starts with box1 and loopsafter box5 with a cut and it is connected with a 3 poles clamp again for the second row that goes from Box6 to 10.

I Some shelves use more than one box for a total of 12 different arrays. Also Some of the LEDs are unused because of the space between the shelves.


Code: [Select]
LEDs0  =  {0, 1, 2, 3, 4, 5, 6, 7, 8};     //box 1 (Skip next 2)
LEDs1  =  {11, 12, 13, 14};          //box 2 (skip next 4)
LEDs2  =  {19, 20, 21, 22}; //box 3 (Skip next 6)  //I shelf

LEDs3  =  {29, 30, 31, 32}; //box 4 (Skip next 2)  //II shelf


LEDs4  =  {35, 36};          //box 5 (Skip next 1)  // III shelf


LEDs5  =  {38, 39};          //box 6 (No Skip)       // IV Shelf

LEDs6  =  {40, 41};          //box 7 (Cut and connect with clamp) //V shelf


LEDs7  =  {42, 43};          //box 8 (No Skip)  //VI shelf

LEDs8  =  {44, 45};          //box 9 (Skip next 7) //VII shelf

LEDs9  =  {53, 54};          //box 10 (Skip next 3) //VIII shelf
LEDs10 =  {58, 59, 60}; //box 11 (Skip next 7) //IX shelf

LEDs11 =  {68, 69, 70); //box 12 (Skip next 5)
LEDs12 =  {76, 77, 78, 79); //box 13 (End of the Strip) //X shelf


Thank you again





dd if=/dev/brain of=/dev/head

PaulRB

#5
May 15, 2019, 01:45 pm Last Edit: May 15, 2019, 01:55 pm by PaulRB
So you are saying the led index numbers corresponding to any box are contiguous? In that case, you do not need to store each led index, only the first and last index.
Code: [Select]

#define NUMBOXES 13
byte boxFirst[NUMBOXES] = {0, 11, 19, 29, 35, 38, 40, 42, 44, 53, 58, 68, 76};
byte boxLast[NUMBOXES]  = {8, 14, 22, 32, 36, 39, 41, 43, 45, 54, 60, 70, 79};

...

void setBox(byte box, CRGB colour) {
  for (byte led = boxFirst[box]; led <= boxLast[box]; led++) leds[led] = colour;
  FastLED.show();
}

...

for (byte box = 0; box < NUMBOXES; box++) {
  setBox(box, CRGB::Red);
  delay(500);
  setBox(box, CRGB::Black);
}

komodo127

Thank you. I need to try it. So in this case What I really need is the first and last LEDs to define the lenght of the shelf?

How can Assign different colors though?
dd if=/dev/brain of=/dev/head

PaulRB

Thank you. I need to try it. So in this case What I really need is the first and last LEDs to define the lenght of the shelf?
No, the length of each box. Why is the length of the self important?
How can Assign different colors though?
In several ways. You have not explained what function colour serves in your project. In fact, in the original post, you say
Quote
No matter what color.
Will the same box always be the same colour? Will colour be determined by the state of the box in some way?

komodo127

Hi, thank you gain for the answer:

I had no luck using the 2 arrays. Anyway I am back on my old solution:

Code: [Select]
#include <bitswap.h>
#include <chipsets.h>
#include <color.h>
#include <colorpalettes.h>
#include <colorutils.h>
#include <controller.h>
#include <cpp_compat.h>
#include <dmx.h>
#include <fastLED.h>
#include <fastled_config.h>
#include <fastled_delay.h>
#include <fastled_progmem.h>
#include <fastpin.h>
#include <fastspi.h>
#include <fastspi_bitbang.h>
#include <fastspi_dma.h>
#include <fastspi_nop.h>
#include <fastspi_ref.h>
#include <fastspi_types.h>
#include <hsv2rgb.h>
#include <led_sysdefs.h>
#include <lib8tion.h>
#include <noise.h>
#include <pixelset.h>
#include <pixeltypes.h>
#include <platforms.h>
#include <power_mgt.h>

#include <FastLED.h>
#define LED_PIN     4
#define NUM_LEDS    80
CRGB leds[NUM_LEDS];
void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
 
}
void loop() {
 
  leds[0] = CRGB(255, 0, 0);
  FastLED.show();
  leds[1] = CRGB(255, 0, 0);
  FastLED.show();
  leds[2] = CRGB(255, 0, 0);
  FastLED.show();
  leds[3] = CRGB(255, 0, 0);
  FastLED.show();
  leds[4] = CRGB(255, 0, 0);
  FastLED.show();
  leds[5] = CRGB(255, 0, 0);
  FastLED.show();
  leds[6] = CRGB(255, 0, 0);
  FastLED.show();
  leds[7] = CRGB(255, 0, 0);
  FastLED.show();
  leds[8] = CRGB(255, 0, 0);  //Box1
  FastLED.show();
  delay(500); 
  leds[11] = CRGB(0, 255, 0);
  FastLED.show();
  leds[12] = CRGB(0, 255, 0);
  FastLED.show();
  leds[13] = CRGB(0, 255, 0);
  FastLED.show();
  leds[14] = CRGB(0, 255, 0); //Box2
  FastLED.show();
  delay(500);
  leds[19] = CRGB(0, 0, 255);
  FastLED.show();
  leds[20] = CRGB(0, 0, 255);
  FastLED.show();
  leds[21] = CRGB(0, 0, 255);
  FastLED.show();
  leds[22] = CRGB(0, 0, 255); //Box3
  FastLED.show();
  delay(500);
  leds[29] = CRGB(150, 60, 255);
  FastLED.show();
  leds[30] = CRGB(150, 60, 255);
  FastLED.show();
  leds[31] = CRGB(150, 60, 255);
  FastLED.show();
  leds[32] = CRGB(150, 60, 255); //Box4
  FastLED.show();
  delay(500);
  leds[35] = CRGB(255, 0, 0);
  FastLED.show();
  leds[36] = CRGB(255, 0, 0); //Box5
  FastLED.show();
  delay(500);
  leds[38] = CRGB(85, 60, 180);
  FastLED.show();
  leds[39] = CRGB(85, 60, 180); //Box6
  FastLED.show();
  delay(500);
  leds[40] = CRGB(50, 255, 20);
  FastLED.show();
  leds[41] = CRGB(50, 255, 20); //Box7
  FastLED.show();
  leds[42] = CRGB(255, 0, 0);
  FastLED.show();
  leds[43] = CRGB(255, 0, 0); //Box8
  FastLED.show();
  delay(500); 
  leds[44] = CRGB(0, 255, 0);
  FastLED.show();
  leds[45] = CRGB(0, 255, 0); //Box9
  FastLED.show();
  delay(500);
  leds[53] = CRGB(0, 0, 255);
  FastLED.show();
  leds[54] = CRGB(0, 0, 255); //Box10
  FastLED.show();
  delay(500);
  leds[58] = CRGB(150, 0, 255);
  FastLED.show();
  leds[59] = CRGB(150, 0, 255);
  FastLED.show();
  leds[60] = CRGB(150, 0, 255); //Box11
  FastLED.show();
  delay(500);
  leds[68] = CRGB(255, 200, 20);
  FastLED.show();
  leds[69] = CRGB(255, 200, 20);
  FastLED.show();
  leds[70] = CRGB(255, 200, 20); //Box12
  FastLED.show();
  delay(500);
  leds[76] = CRGB(85, 60, 180);
  FastLED.show();
  leds[77] = CRGB(85, 60, 180);
  FastLED.show();
  leds[78] = CRGB(85, 60, 180);
  FastLED.show();
  leds[79] = CRGB(85, 60, 180); //Box13
  FastLED.show();
  delay(500);
}


It is Ugly and heavy but so far it works.

My goal is to turn on ONE box at the time and for ONE box I mean the LEDs relative to that box.

So if Box1 is turned on Box{2,3,4,5,6...13} are off and than move to the next one.

After that I would like to open a connection with the serial port and try to turn on each box with inputs from my pc.

thank you again :)
dd if=/dev/brain of=/dev/head

PaulRB

Quote
I had no luck using the 2 arrays.
Luck does not help with coding, no matter how much or how little you have.

I'm not going to help you with that huge mess of code. I suggested a better and easier way and you dismissed it. If you wanted my advice you would have posted your attempt with the two arrays and asked for assistance.

UKHeliBob

A small portion of your code
Code: [Select]

  leds[0] = CRGB(255, 0, 0);
  FastLED.show();
  leds[1] = CRGB(255, 0, 0);
  FastLED.show();
  leds[2] = CRGB(255, 0, 0);
  FastLED.show();
  leds[3] = CRGB(255, 0, 0);
  FastLED.show();
  leds[4] = CRGB(255, 0, 0);
  FastLED.show();
  leds[5] = CRGB(255, 0, 0);
  FastLED.show();
  leds[6] = CRGB(255, 0, 0);
  FastLED.show();
  leds[7] = CRGB(255, 0, 0);
  FastLED.show();


Things to note :

1 - there is a pattern to the number used as the index to the leds[] array, ie it goes from 0 to 7 (and higher) in steps of 1

2 - You could use a for loop to create numbers between 0 and 7 (and higher)

3 - you only need to use FastLED.show() once after all of the LEDs have been set to the required colour
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

komodo127

Luck does not help with coding, no matter how much or how little you have.

I'm not going to help you with that huge mess of code. I suggested a better and easier way and you dismissed it. If you wanted my advice you would have posted your attempt with the two arrays and asked for assistance.
Fair enough...
Tried again with the modification suggested. Unfortunately it does not work for me.
But I am going forward with this code:

Code: [Select]

#include <FastLED.h>
#include <Adafruit_NeoPixel.h>

#define NUM_LEDS 80
#define CLOCK_PIN 5
#define DATA_PIN 4
CRGB leds[NUM_LEDS];
#define COLOR_ORDER BGR
#define BRIGHTNESS  20

#define Shelves 13
byte startarr[Shelves] = {0, 11, 19, 29, 35, 38, 40, 42, 44, 53, 58, 68, 76};
byte stoparr[Shelves]  = {8, 14, 22, 32, 36, 39, 41, 43, 45, 54, 60, 70, 79};

void setup()
{
  Serial.begin(9600);
  FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  LEDS.setBrightness(BRIGHTNESS);
}

void loop(){
  if (Serial.available() >0){          
    rxChar = Serial.read();            
    Serial.flush();                  
  
  switch (rxChar) {
    
    case 'Box1':
     if (digitalRead(Shelves) == LOW){      
          digitalWrite(LED0,HIGH);      
          
 }
        else Serial.println("Shelves already On!");
 digitalWrite(Shelves,LOW);
        break;
  
 case 'Box2':
     if (digitalRead(Shelves) == LOW){      
          digitalWrite(Shelves,HIGH);      
          
 }
        else Serial.println("Shelves already On!");
 digitalWrite(Shelves,LOW);
        break;

    default:                          
      Serial.print("'");
      Serial.print((char)rxChar);
      Serial.println("' is not a command!");
    }
  }
}


Now I open the serial port and I send data with the serial port (raspberry pi)  to arduino.
I'd like to turn on a specific section with a specific input from the serial port.

Es: "Box1" ---> Array1

Thank you in advance
dd if=/dev/brain of=/dev/head

PaulRB

Quote
Unfortunately it does not work for me.
Why, what is different about your project which means this would not work? Or did you mean to say "I could not make it work"? I don't see the code I suggested in post #5 in your code above. What I do see is your code using "Shelves", which is the number of shelves, as an Arduino pin number.

Please always perform Auto Format before you post your code, otherwise it's quite difficult to read and understand

komodo127

did you mean to say "I could not make it work"?
No I mean It does not work... or at least Arduino refuses to make it work. All it does is turning on only the first leds of each array instead of having the whole array on.

Anyway I will change the code again without changing a single character from post #5 and I will let you know.

P.S. If you have a better way for me to complete this project please feel free to post it.

Thank you in advance
dd if=/dev/brain of=/dev/head

komodo127

Also this is the link for the led strip I am using for this project.

https://www.amazon.com/gp/product/B07BKNS7DJ
dd if=/dev/brain of=/dev/head

Go Up