Hi everyone,
I have a question, at the end of an if statement, all local variables between {} should be de-allocated.
This is the following code I'm Running :
#include <Motor.h>
#include <stdlib.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <MemoryFree.h>
bool push = 0;
bool incchar = 0;
void setup() {
Serial.begin(9600);
/*
*/
if (push ==1){
Motor m1(SERVOS,RIGHT,0,0,"m1");
Motor m2(SERVOS,RIGHT,40,4400,"m2");
}
Serial.begin(9600);
Serial.println(getFreeMemory());
}
void loop() {
delay(100000);
}
The code of the Motor Class
Motor.h :
#ifndef __MOTOR__H__
#define __MOTOR__H__
#include <Arduino.h>
typedef enum{LEFT,RIGHT} Direct_t;
typedef enum{STEPPERS, SERVOS} Moteur_t;
class Motor{
private :
Direct_t direction;
Moteur_t type;
int speed;
float angle;
char nameBuff[3];
char* name;
public :
Motor(Moteur_t type = STEPPERS, Direct_t direction = LEFT, int speed = 0, float angle = 0,char* name = "");
~Motor();
//setters & getters
int getSpeed();
void setSpeed(int);
Direct_t getDir();
void setDir(Direct_t);
float getAngle();
void setAngle(float);
void tourner(int ID, Direct_t side, int speed);
char* getName();
void setName(char*);
Moteur_t getType();
void setType(Moteur_t);
//tourner
void tourner(String, String, double, int, int*, String*, int);
static double positions(double);
};
#endif
Motor.cpp :
#include "Motor.h"
#include <Arduino.h>
#include "DynamixelSerial.h"
//constructeur qui permet de construire des objets avec 0, 1, 2, 3 ou 4 arguments selon les besoins.
Motor::Motor(Moteur_t type, Direct_t direction, int speed, float angle, char* name) : type{ type }, direction{ direction }, speed{ speed }, angle{angle}
{
strcpy(this->nameBuff,name);
this->nameBuff[strlen(name)] = '\0';
this->name = this->nameBuff;
}
Motor::~Motor() {
//vide car pas de variables d'instances dynamiques
//Serial.println(F("Motor deleted"));
}
int Motor::getSpeed() {
return speed;
}
void Motor::setSpeed(int s) {
speed = s;
}
Direct_t Motor::getDir() {
return direction;
}
void Motor::setDir(Direct_t dir) {
direction = dir;
}
float Motor::getAngle() {
return angle;
}
void Motor::setAngle(float angle) {
this->angle = angle;
}
void Motor::tourner(int ID, Direct_t side, int speed){
}
void Motor::setName(char* name){
strcpy(this->nameBuff,name);
this->nameBuff[strlen(name)] = '\0';
this->name = this->nameBuff;
}
char* Motor::getName(){
return this->name;
}
void Motor::setType(Moteur_t type){
this->type = type;
}
Moteur_t Motor::getType(){
return this->type;
}
void Motor::tourner(String names, String directions, double angle, int vitesse, int* tab_ID, String* tab_names, int oldPositions){
int newPositions;
int ID;
for (int i=0; i<5; i++){
if (names == tab_names[i])
ID = tab_ID[i];
if (directions == "LEFT")
newPositions = oldPositions - positions(angle);
else
newPositions = oldPositions + positions(angle);
oldPositions = newPositions;
Dynamixel.moveSpeed(ID, newPositions, vitesse);
}
}
double Motor::positions(double angle){
return (angle * 3.41);
}
So logically, I should have the same thing returned from getFreeMemory() with or without the if statement.
But with the if statement I have a free memory of 1560 like when I declare m1 and m2, and without I have 1604. If you have any idea of where I am wrong I'll be very thankful
Thanks for reading ! Have a good day !
Syndorik