setup keeps looping when #define value is too big

I have a problem changing the STEPS value used in the code, only value of 500 works normaly, other values make arduino behave in weird ways. Next to the values I have commented what happens when they are used in code. I don’t know if this is a bug, so I didn’t report it yet, wanted to ask you guys for your opinion. Arduino Uno, software version 1.8.9. Tried to run arduino as administrator, tried to reinstall software, problem still persists.

Values and what happens when they’re used:

//#define STEPS 10000 // prints setup once and doesnt do anything else
//#define STEPS 2048 // prints setup once and doesnt do anything else
//#define STEPS 1000 // prints “setup” ~40 times, then keeps printing “b” letter, motor spins very little
//#define STEPS 500 // code runs normaly as it should, motor spins

Code:

#define DIR_PIN          4
#define STEP_PIN         3
#define ENABLE_PIN       2

//#define STEPS 10000 // prints setup once and doesnt do anything else
//#define STEPS 2048 // prints setup once and doesnt do anything else
//#define STEPS 1000 // prints "setup" ~40 times, then keeps printing "b" letter, motor spins very little
#define STEPS 500 // code runs normaly as it should, motor spins

void setup() {
    Serial.begin(115200);
  pinMode(STEP_PIN,   OUTPUT);
  pinMode(DIR_PIN,    OUTPUT);
  pinMode(ENABLE_PIN, OUTPUT);
  Serial.println("setup");
}


void constantAccel() {
  int delays[STEPS];
  //  float angle = 1;
  //  float accel = 0.01;
  //  float c0 = 2000 * sqrt( 2 * angle / accel ) * 0.67703; //2000=1/0.002000
  float lastDelay = 0;
  float c0 = 10000;
  int highSpeed = 1000;
  for (int i = 0; i < STEPS; i++) {
    float d = c0;
    if ( i > 0 )
      d = lastDelay - (2 * lastDelay) / (4 * i + 1);
    if ( d < highSpeed )
      d = highSpeed;
    delays[i] = d;
    lastDelay = d;
  }
  
  // use delays from the array, forward
  for (int i = 0; i < STEPS; i++) {
    digitalWrite(STEP_PIN, HIGH);
    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds( delays[i] );
    Serial.println(delays[i]);
  }

  // use delays from the array, backward
  for (int i = 0; i < STEPS; i++) {
    digitalWrite(STEP_PIN, HIGH);
    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds( delays[STEPS - i - 1] );
    Serial.println( delays[STEPS - i - 1]  );
  }
}

void loop() {

  digitalWrite(DIR_PIN, LOW);
  constantAccel();
  digitalWrite(DIR_PIN, HIGH);
  constantAccel();

  while (true);
}

You need to understand that the microcontroller on your Uno has very limited resources. Your Uno only has 2048 bytes of SRAM. An int is 2 bytes. So if STEPS is set to 1000 then the delays array alone uses 2000 bytes of your SRAM, leaving only 48 bytes for all other requirements of your code.

Oh, I didn’t get any kind of warning message from compiler, so I didn’t think this could be the case. Thanks for the answer.

The Arduino IDE only informs you of memory used by global variables. If your global variables exceed 75% of the available memory it will give you a warning. If they exceed 100% of the available memory then compilaiton fails. But it doesn't tell you about memory usage of local variables such as your delays array. You need to keep an eye on those yourself.

I'll keep that in mind, thanks :slight_smile: