Controlling lots of leds at different interval time

Hello.
I have a problem with the SRAM in following program and fail to find a solution. I wish to use the arrays for over 1000 pins, but as you can see, the size of the array greater than 700 create SRAM memory problems. I tried to define array “intervals” and array “previousMillis” as unsigned integer and in to the program to multiplie by ten values “interval” and “previousMillis”?in order to have aproximatively 650 seconds for “interval”, but I don’t managed to solve the problem, moreover, decreased the maximum size of the array that i can use.

byte ledPin[] = {33,35,
33,35,
33,35,
...................

unsigned long ofTime =10000000;
byte onTime =100;
unsigned long interval[]={1000,1000,
2000,2000,
3000,3000,
.......................

byte ledState[] = {LOW,LOW,
LOW,LOW,
LOW,LOW,
......................

for (int idx = 0 ; idx < 700 ; idx++)  // for all defined pins...
      pinMode(ledPin[idx],OUTPUT) ;  //  ..set them to output
...............................

corectmillis=((millis() - startmillis)+rulat)*corectie;
..............................................................

for ( idx = 0 ; idx < 700 ; idx++ ) { // pentru toti pini definiti in array fa urmatoarele :
    
    if (corectmillis - previousMillis[idx] > interval[idx]) 
    {
      previousMillis[idx] = corectmillis;   

          if (ledState[idx] == HIGH)
              {
                ledState[idx] = LOW;
                interval[idx] = ofTime;
                digitalWrite (ledPin[idx], ledState[idx]);
              }
          else
                if (ledState[idx] == LOW)
                    {
                      ledState[idx] = HIGH;
                      interval[idx] = onTime;
                    } 
    digitalWrite (ledPin[idx], ledState[idx]);

    }
byte ledPin[700] = {33,35,
33,35,
33,35,
33,39,

Presumably, these are constants - put them in program (flash) memory).

char ledState[700] = {LOW,LOW,
LOW,LOW,

Using eight bits of memory to store one bit of information is crazy.

also, these look a lot like multiples of a constant (1000).

unsigned long interval[700]={1000,1000,
2000,2000,
3000,3000,
.............
.............
.............
350000,350000};

you can probably condense that too

Some random thoughts:

Led state can be read from pin, perhaps not needed to store state value at all ?

You seem to have stored an incremental pattern, which can be done with a loop I imagine.

Question: what is the intended operation of the leds ? (behaviour)

Firstly thank you all.

Sorry for my first post (i don't specify clear the use of program). I intend to use program to control 40 relay at some point in a song. Duration of song is maximum 10 minutes. This time could be different time for all 1000 positions or equal time for some specific positions from "ledPin[1000]" array (this time don't change, in reality want to activate specific relay defined by ledPin[1000] at specific time defined by "interval[1000]", but in this program (is not written by me) "interval[1000]" change his value (

if (ledState[idx] == HIGH)
              {
                ledState[idx] = LOW;
               interval[idx] = ofTime;
                digitalWrite (ledPin[idx], ledState[idx]);
              }
          else
                if (ledState[idx] == LOW)
                    {
                      ledState[idx] = HIGH; 
                    interval[idx] = onTime;
                    }

) so i need some ideea to reduce the SRAM used)

  1. To Awol: Thanks for the replies. For the first solution somewhat understand what to do, but for the second, although it should be easier, because of my ignorance, I do not know how to declare the ledstate[1000] array to occupy 1 bit for each of the 1000 location of the array and in addition to using this information in the program:
if (ledState[idx] == HIGH)
              {
              ledState[idx] = LOW;
                interval[idx] = ofTime;
                digitalWrite (ledPin[idx], ledState[idx]);
              }
          else
                if (ledState[idx] == LOW)
                    {
                      ledState[idx] = HIGH;
                      interval[idx] = onTime;
                    }

I try to define HIGH=1 and LOW=0 but this produce an unpredictable response.

I do not know how to declare the ledstate[1000] array to occupy 1 bit for each of the 1000 location of the array

That's OK because you can't declare an array of anything smaller than a byte. However, you could declare a byte array of 125 entries, and get creative with bitRead, bitWrite and the divide and modulo operators.

I think I have a solution for you, very simple solution, but it relies on a couple files. Are you up to adding a few files to your sketch that all but one you'll not really look at?

Here's the end product. Its a c++ object that is a blinking LED. You set the onTime, the cycle time (period) & the pin. Then let it go. You can spawn as many of these blinking lights as you want. They all blink in the background and you don't have to worry about them once you set 'em up and blinking.

#ifndef blinker_h
#define blinker_h

#include "idlers.h"
#include "timeObj.h"

// Some defaults in case the user just doesn't care..
#define defPin 13
#define defOnMs 50
#define defPeriodMs 400

class blinker : public idler, timeObj {

public:
    blinker(int inPin=defPin,float inOnMs=defOnMs, float inPeriodMs=defPeriodMs);

    void setBlink(boolean onOff);       // Start or stop the blinking..
    virtual void idle(void);            // What to do in our spare time?

    void setTimes(float inOnMs, float inPeriodMs);  // Want to change the blink?

private:
        boolean  init;
        timeObj* onTimer;
    boolean  running;
    boolean  pinHigh;
    int      pin;
};

#endif

What this gives you..

LED1 = blinker(pin1,20,300); // On pin1 light on for 20ms, then repeat every 300 ms. LED2 = blinker(pin2,10,300); // On pin2 light on for 10ms, then repeat every 300 ms.

setup() { }

loop{ idle(); //other stuff you want to do.. }

If the above makes sense, I can add the rest of the files for you to dump into your project.

hope this helps.

-jim lee

Instead of:

if (ledState[idx] == LOW)

Perhaps something like this could work ?

if(digitalRead(ledPin[idx]) == LOW) or

temp = digitalRead(ledPin[idx]);

if(temp == LOW)

Potentially eliminating that entire state array ?

In order to reduce the dimension of interval[idx] and previousMillis[idx] I try to define these arrays to unsigned int (for me is OK if a could use this program for 65535 * 10 microseconds). So I try to manipulate in the program de value of interval[idx] and previousMillis[idx], by multiplying these values ??by 10 and dividing by ten the value of corectmillis , but I think I’m wrong somewhere and I can not figure out where because the program does not work correctly.
Please help!!!
This is the code where I make the main changes:

.................
for ( idx = 0 ; idx < 8 ; idx++ ) { // pentru toti pini definiti in array fa urmatoarele :
    
    if (corectmillis - previousMillis[idx]*10 > interval[idx]*10) 
    {
      corectmillispezece = corectmillis/10;
      intcorectmillis = (int)corectmillispezece;
      previousMillis[idx] = intcorectmillis;   

          if (ledState[idx] == HIGH)
.............................

If i replace the above code whit this:

.................
for ( idx = 0 ; idx < 8 ; idx++ ) { // pentru toti pini definiti in array fa urmatoarele :
    
    if (corectmillis - previousMillis[idx] > interval[idx]) 
    {

      previousMillis[idx] = corectmillis;   

          if (ledState[idx] == HIGH)
.............................

The program seems to work well for 65 seconds (the maximum value of unsigned int).

BulldogLowell:
also, these look a lot like multiples of a constant (1000).
Code:

unsigned long interval[700]={1000,1000,

2000,2000,
3000,3000,

350000,350000};



you can probably condense that too

Thanks… I did not see the forest for the trees :frowning: .
As you probably noticed two array of unsigned ( interval and previousMillis ) with 1,000 positions occupy themselves all available memory (previousMillis [1000] has the same size as the interval [1000]). So me struggling in vain to reduce the size for other arrays (problem not solved). After reading the Your message several times, finally lit bulb (better late than never :)). Because I needed the two pins to be driven simultaneously at each time interval, does not justify the array interval and previousMillis to contain times in duplicate (and therefore I won 4 k memory ). Sorry again to those who, from a poor explanation of the problem, I headed to other solutions (very good indeed, but difficult to implement for an unknown like me).