Go Down

Topic: LED Matrix Control With delay() Function (Read 310 times) previous topic - next topic

ratherwok

Hi, I'm using the LEDMatrixControl 1.3.2 by Tyler Jones to create animations, the problem is that this program generates fastLED code with delay() functions. Is there a way to do multiple things with delay(), like using one button to cycle through modes, or 2 sets of LEDs run seperately? Would switch case work? Or do I have to do it with BWoD? is there any better ways to write complex animation code like the LEDMatrixControl program but not using delay()? Thank you!

Here is one of the codes I wrote ( I had to shorten the animation code because it exceeds the max length)

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

#define NUM_LEDS 72
#define DATA_PIN 2
#define CLOCK_PIN 3

CRGB leds[NUM_LEDS];

const byte ledPins[] = {8,9,10,11,12,13};
const byte number_of_LEDs = sizeof(ledPins);
unsigned long previousMillis[number_of_LEDs] = {};
byte indexes[number_of_LEDs] = {0};
const byte max_number_of_periods = 15;
long periods[][max_number_of_periods] =
{
  {20, 100, 20, 110, 25, 75, 40, 75, 25, 105, 25, 200, 200, -1},
  {20, 100, 40, 80, 20, 95, 50, 120, 25, 60, 10, 200, 200, -1},
  {20, 100, 30, 125, 30, 85, 100, 100, 30, 200, 200, -1},
  {20, 100, 20, 35, 125, 40, 100, 70, 25, 55, 30, 200, 200, -1},
  {20, 100, 55, 70, 25, 75, 30, 60, 25, 125, 35, 200, 200, -1},
  {20, 100, 70, 100, 25, 45, 40, 80, 25, 35, 80, 200, 200, -1},
};

void setup() {
  FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, BGR> (leds, NUM_LEDS);
  FastLED.setBrightness(10);

    for(int led = 0; led < number_of_LEDs; led++) {
    pinMode(ledPins[led], OUTPUT);
  }
}

void loop() {
  unsigned long currentMillis = millis();

 for(int led = 0; led < number_of_LEDs; led++) {
  if(currentMillis - previousMillis[led] >= periods[led][indexes[led]]) {
    digitalWrite(ledPins[led], !digitalRead(ledPins[led]));
    previousMillis[led] = currentMillis;
    indexes[led]++;
  }
  if (periods[led][indexes[led]] == -1) {
    indexes [led] = 0;
  }
 }

leds[0] = CRGB(3, 96, 250);
leds[1] = CRGB(250, 159, 3);
leds[2] = CRGB(18, 19, 246);
leds[3] = CRGB(233, 235, 5);
leds[4] = CRGB(13, 252, 45);
leds[5] = CRGB(233, 235, 5);
leds[6] = CRGB(96, 2, 251);
leds[7] = CRGB(250, 159, 3);
leds[8] = CRGB(96, 252, 2);
leds[9] = CRGB(161, 2, 251);
leds[10] = CRGB(13, 252, 45);
leds[11] = CRGB(239, 2, 207);
leds[12] = CRGB(247, 47, 4);
leds[13] = CRGB(239, 2, 207);
leds[14] = CRGB(13, 252, 45);
leds[15] = CRGB(239, 2, 207);
leds[16] = CRGB(13, 252, 45);
leds[17] = CRGB(161, 2, 251);
leds[18] = CRGB(13, 252, 45);
leds[19] = CRGB(239, 2, 207);
leds[20] = CRGB(247, 47, 4);
leds[21] = CRGB(239, 2, 207);
leds[22] = CRGB(4, 241, 208);
leds[23] = CRGB(239, 2, 207);
leds[24] = CRGB(18, 19, 246);
leds[25] = CRGB(233, 235, 5);
leds[26] = CRGB(161, 2, 251);
leds[27] = CRGB(160, 252, 2);
leds[28] = CRGB(13, 252, 45);
leds[29] = CRGB(160, 252, 2);
leds[30] = CRGB(161, 2, 251);
leds[31] = CRGB(233, 235, 5);
leds[32] = CRGB(160, 252, 2);
leds[33] = CRGB(96, 2, 251);
leds[34] = CRGB(13, 252, 45);
leds[35] = CRGB(161, 2, 251);
leds[36] = CRGB(249, 7, 53);
leds[37] = CRGB(161, 2, 251);
leds[38] = CRGB(13, 252, 45);
leds[39] = CRGB(161, 2, 251);
leds[40] = CRGB(161, 2, 251);
leds[41] = CRGB(13, 252, 45);
leds[42] = CRGB(239, 2, 207);
leds[43] = CRGB(13, 252, 45);
leds[44] = CRGB(3, 160, 250);
leds[45] = CRGB(13, 252, 45);
leds[46] = CRGB(239, 2, 207);
leds[47] = CRGB(13, 252, 45);
leds[48] = CRGB(233, 235, 5);
leds[49] = CRGB(18, 19, 246);
leds[50] = CRGB(96, 252, 2);
leds[51] = CRGB(96, 2, 251);
leds[52] = CRGB(239, 2, 207);
leds[53] = CRGB(96, 2, 251);
leds[54] = CRGB(96, 252, 2);
leds[55] = CRGB(18, 19, 246);
leds[56] = CRGB(160, 252, 2);
leds[57] = CRGB(96, 2, 251);
leds[58] = CRGB(13, 252, 45);
leds[59] = CRGB(161, 2, 251);
leds[60] = CRGB(249, 7, 53);
leds[61] = CRGB(239, 2, 207);
leds[62] = CRGB(13, 252, 45);
leds[63] = CRGB(96, 2, 251);
FastLED.show();
delay(100);
leds[0] = CRGB(14, 252, 44);
leds[1] = CRGB(249, 7, 53);
leds[2] = CRGB(3, 240, 209);
leds[3] = CRGB(249, 7, 53);
leds[4] = CRGB(233, 235, 5);
leds[5] = CRGB(248, 47, 4);
leds[6] = CRGB(3, 240, 209);
leds[7] = CRGB(249, 7, 53);
leds[8] = CRGB(250, 160, 3);
leds[9] = CRGB(3, 96, 250);
leds[10] = CRGB(233, 235, 5);
leds[11] = CRGB(18, 19, 246);
leds[12] = CRGB(240, 2, 206);
leds[13] = CRGB(18, 19, 246);
leds[14] = CRGB(233, 235, 5);
leds[15] = CRGB(3, 96, 250);
leds[16] = CRGB(233, 235, 5);
leds[17] = CRGB(18, 19, 246);
leds[18] = CRGB(160, 252, 2);
leds[19] = CRGB(18, 19, 246);
leds[20] = CRGB(240, 2, 206);
leds[21] = CRGB(18, 19, 246);
leds[22] = CRGB(160, 252, 2);
leds[23] = CRGB(18, 19, 246);
leds[24] = CRGB(3, 240, 209);
leds[25] = CRGB(249, 7, 53);
leds[26] = CRGB(3, 160, 250);
leds[27] = CRGB(248, 47, 4);
leds[28] = CRGB(160, 252, 2);
leds[29] = CRGB(248, 47, 4);
leds[30] = CRGB(3, 96, 250);
leds[31] = CRGB(248, 47, 4);
leds[32] = CRGB(248, 47, 4);
leds[33] = CRGB(3, 160, 250);
leds[34] = CRGB(250, 160, 3);
leds[35] = CRGB(3, 96, 250);
leds[36] = CRGB(161, 2, 251);
leds[37] = CRGB(3, 96, 250);
leds[38] = CRGB(233, 235, 5);
leds[39] = CRGB(3, 160, 250);
leds[40] = CRGB(3, 96, 250);
leds[41] = CRGB(250, 160, 3);
leds[42] = CRGB(18, 19, 246);
leds[43] = CRGB(233, 235, 5);
leds[44] = CRGB(14, 252, 44);
leds[45] = CRGB(233, 235, 5);
leds[46] = CRGB(96, 2, 251);
leds[47] = CRGB(233, 235, 5);
leds[48] = CRGB(248, 47, 4);
leds[49] = CRGB(3, 240, 209);
leds[50] = CRGB(250, 160, 3);
leds[51] = CRGB(3, 160, 250);
leds[52] = CRGB(96, 2, 251);
leds[53] = CRGB(3, 160, 250);
leds[54] = CRGB(250, 160, 3);
leds[55] = CRGB(3, 240, 209);
leds[56] = CRGB(248, 47, 4);
leds[57] = CRGB(3, 160, 250);
leds[58] = CRGB(250, 160, 3);
leds[59] = CRGB(3, 96, 250);
leds[60] = CRGB(161, 2, 251);
leds[61] = CRGB(3, 96, 250);
leds[62] = CRGB(233, 235, 5);
leds[63] = CRGB(3, 160, 250);
FastLED.show();
delay(100);
}

Grumpy_Mike

Quote
Or do I have to do it with BWoD?
Yes normally.

However you can write your own delay function that loops until the time up is up OR until a button is held down. That way when you press a button all the delays in effect short out and you are very quickly at the start of the lop function. Where the first task should be to wait until the button is released.

ratherwok

Yes normally.

However you can write your own delay function that loops until the time up is up OR until a button is held down. That way when you press a button all the delays in effect short out and you are very quickly at the start of the lop function. Where the first task should be to wait until the button is released.
I did it with BWoD eventually so I won't run into more problem with other code. Thank you for replying.

eric8he

What you CAN do, however, is to attach your button to an interrupt pin and attach an interrupt to call the function to the mode-switching.

Code:
Code: [Select]
attachInterrupt(digitalPinToInterrupt(<Pins 2 or 3 on Uno>), function, FALLING);
for pulled up switch that is active low

Grumpy_Mike

What you CAN do, however, is to attach your button to an interrupt pin and attach an interrupt to call the function to the mode-switching
No you can't, an interrupt routine can not change the flow of the running program except by setting flags. Then your code reads those flags and does something. So you might as well just read your button at the point you read the flag. You have no advantage using an interrupt.

Go Up