LED stroboscope and dynamic memory

hello everybody, I am very new to Arduino. I am using it to create a LED strobe, with two sets of LEDs. One of them is the FLASH set, with an adjustable frequency and intensity. The second with only adjustable intensity, without strobe(flashing).

Here is a code I wrote, it worked pretty well, but it crashes after the third loop. And adding too many variables uses way too much dynamic memory. Would you have any tip?

I found a program on a forum, wich has about the same intent. But I cannot understant a lot, if not most of it. As it uses way less dynamic memory, could anybody give me an advice to maybe inspire from it? (As it has similarities)

Regards

Programme_REF__intFLASH_TEST.ino (937 Bytes)

test_code_import__forum.ino (19.4 KB)

Probably should not by using dynamic memory on an arduino, it does not have much ram.

jules2001:
Here is a code I wrote, it worked pretty well, but it crashes after the third loop.

How is the crash manifested?

jules2001:
hello everybody, I am very new to Arduino. I am using it to create a LED strobe, with two sets of LEDs. One of them is the FLASH set, with an adjustable frequency and intensity. The second with only adjustable intensity, without strobe(flashing).

Here is a code I wrote, it worked pretty well, but it crashes after the third loop. And adding too many variables uses way too much dynamic memory. Would you have any tip?

I found a program on a forum, wich has about the same intent. But I cannot understant a lot, if not most of it. As it uses way less dynamic memory, could anybody give me an advice to maybe inspire from it? (As it has similarities)

Regards

Interesting project. The only way to change the intensity/brightness of a LED it to change the current by changing the value of the current limiting resistor. Is this what you are doing?

Or are you rapidly turning the LED on and off so your eyes see less light from the LED. The light output stays the same during the time the LED is on, you only believe it is less light.

If the latter is what you are doing, then the only difference between reducing the on-time for the LED and blinking the LED is the time on and time off. so you can do both by just changing a time variable value.

Paul

Why do you say it crashes after the third loop? You specifically told it to run three times and then never run the code that turns the LEDs on again.

you are using the same name for the the main variable in both loops. i'm surprised this even compiles. in the nested loop dont use "int i" again. use some other letter to name your loop variable so it doesn't conflict with the outer loop.

how does the code know what "i" you are referring to?

i can assure memory isn't a problem with a sketch this small.

Thank you all for replying

Very stupid question about the crash! I just forgot to change one of the values in one of the for loops.
The memory starts to be problematic when the number of values (of frequency, time, intensity) gets higher (more than150).

About the same variable i in the loops, it dosesnt seem to cause any pb, but I'll try to change it.

To change the intensity of the flashing LEDs, I am now using the square wave generator to reduce the ON time while the flash is on. Not easy to exlpain, it's like a frequency in a lower frequency.

How could I change my variables so they don't use dynamic memory? using PROGMEM?

Regards

This one does three of the five patterns and then stops. Is this the one that "crashes" after the third loop?!?

// DÉF VARIABES SORTIES
int LEDambiance = 11;
int LEDflash = 10;


//DÉF TABLEAUX
int Duree[5] = {5, 10, 4, 7, 8};
int Ambiance[5] = {100, 30, 100, 7, 9};
int Frequence[5] = {20, 5, 40, 30, 20};
int IntensiteFLASH[5] = {50, 70, 30, 35, 50};


//DÉF CONDITION DE DÉBUT/FIN
boolean STARTSTOP;


void setup()
{
  pinMode(LEDflash, OUTPUT); // set pour la LED de flash


  STARTSTOP = 1; // confirme le début de la séquence
}


void loop()
{
  if (STARTSTOP)// début de la séquence
  {
    for (int i = 0; i < 3; i++) //boucle des tableaux
    {
      for (int compteur = 0; compteur <= (Frequence[i]*Duree[i]); compteur++) // boucle pour les FLASHS
      {
        //BLOC DE FLASH ET AMBIANCE
        analogWrite(LEDambiance, (Ambiance[i] * 2.55)); // ambiance ON
        analogWrite(LEDflash, (IntensiteFLASH[i] * 2.55)); //Flash ON
        delay(500 / Frequence[i]);
        digitalWrite(LEDflash, 0); //Flash OFF
        delay(500 / Frequence[i]);
      }
    }
  }
  analogWrite (LEDambiance, 0); // AMBIANCE OFF
  STARTSTOP = 0; // FIN DE LA SÉQUENCE
}

jules2001:
How could I change my variables so they don't use dynamic memory? using PROGMEM?

You can't store variables in PROGMEM, only constants. The difference being that the value for a variable can be changed in the sketch, the value of a constant cannot.

Presumably you are referring to the arrays:

//DÉF TABLEAUX
int Duree[5] = {5, 10, 4, 7, 8};
int Ambiance[5] = {100, 30, 100, 7, 9};
int Frequence[5] = {20, 5, 40, 30, 20};
int IntensiteFLASH[5] = {50, 70, 30, 35, 50};

If the values are always within the range of 0 through 255, you can save some memory by storing them as byte instead of int.
To put into PROGMEM, define as a const and specify PROGMEM:

//DÉF TABLEAUX
const byte Duree[5] PROGMEM = {5, 10, 4, 7, 8};
const byte Ambiance[5] PROGMEM = {100, 30, 100, 7, 9};
const byte Frequence[5] PROGMEM = {20, 5, 40, 30, 20};
const byte IntensiteFLASH[5] PROGMEM = {50, 70, 30, 35, 50};

Then change all the references to the arrays to specify they are in PROGMEM

arrayName[i] //original way to reference element i of arrayName

pgm_read_byte(&arrayName[i]) //reference to element i of arrayName stored in PROGMEM, arrayName[] defined as byte 

pgm_read_word(&arrayName[i]) //use this if arrayName[] is defined as int