Hello Guys.
I already saw a lot of examples in the foruns, and after try all things that I consider to be used in my codes, I'm writing here to ask for help. Sorry if my english is not soo good.
I'm trying to use an Arduino Leonardo to control some stepper motors. I'm using Adafruit featherwings bords as drivers. When I try to declare all my feathers with static allocation I have a problem of SRAM memory. I think that the solution for my case should be change the Arduino board for another with more memory. But I'm trying to use dynamic allocation of memory.
I'm using the void display_freeram() to show me how much I have of free memory after the loop. Also in this code I'm working from 0x60 untin 0x90, but in my full project I will have more than this and that is why my Leonardo can't storage all the feathers declaration.
I use the new operator to call the AdafruitMotorShield method and the delete operator to free the memory, but somehow the memory is becaming small and small in each interaction of my lood section.
Bellow is my code:
#include <Adafruit_MotorShield.h>
void setup() {
Serial.begin(9600);
}
void loop() {
for (int k=0x60; k<=0x69;k++){
Serial.print("k = ");
Serial.println(k);
mover(k);
}
Serial.print("After: ");
display_freeram();
}
void mover(uint8_t k){
Adafruit_MotorShield *AFMS = new Adafruit_MotorShield(k);
AFMS->begin();
Adafruit_StepperMotor *M1 = AFMS->getStepper(200, 1);
Adafruit_StepperMotor *M2 = AFMS->getStepper(200, 2);
M1->setSpeed(1000);
M2->setSpeed(1000);
M1->step(10, FORWARD, DOUBLE);
M2->step(10, FORWARD, DOUBLE);
M1->step(10, BACKWARD, DOUBLE);
M2->step(10, BACKWARD, DOUBLE);
delete AFMS;
}
void display_freeram() {
Serial.print(F("- SRAM left: "));
Serial.println(freeRam());
}
int freeRam() {
extern int __heap_start,*__brkval;
int v;
return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);
}