clearing SRAM stack?

hi i having trouble with a code for a robot
I think but m not to sure that I’m running out of SRAM from the recursive methods im calling
or may be I’m doing something else wrong?
because after a little while the Arduino crashes or freezes

any way below is my code for the robot

#include <Arduino.h>.
boolean test =false; 
int testwait =100;
/************************************************/
int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;
/***********************************************/
int fsensor =A0;
int Lsensor =A1;
int Rsensor =A2;
int Lbump= A4;
int Rbump =A5;
/**********************************************/
int FSvalue =0;
int LSvalue =0;
int RSvalue =0;
int LBvalue =0;
int RBvalue =0;
/*********************************************/

int pwm_speed; 
/********************************************/
//Distances
int fwall =75;
int rwall=  40;
int lwall = 40;
int BUMP =550;

int clear =50;
int rvering =50;
int lvering =50;
/********************************************/
//Speed
int stp;
int fast = 255;
int slow = 100;
/********************************************/
//delays
int reverse = 500;
int rev = 30;
int dist = 30;
int turn90 = 260;
int turn180 = 500;
int turn45 =125;
int wallDelay =50;
/****************************************/

/********************************************/
void setup() {                
  
    pinMode(M1, OUTPUT); 
    pinMode(M2, OUTPUT);   
    Serial.begin(9600);   
    pinMode(13, OUTPUT);      
}        

 void loop() {
 
  digitalWrite(13,HIGH); //just an led for debugging 
  if (test){
    analogWrite(E1, 0);
    analogWrite(E2, 0);
    Serial.println(" Testing");
    delay(testwait);
   
    Serial.println("");
    FSvalue = analogRead(fsensor);
    RSvalue = analogRead(Rsensor);
    LSvalue = analogRead(Lsensor);
 
    Serial.print(" F sensor ");
    Serial.print(FSvalue);
    Serial.print(" R sensor ");
    Serial.print(RSvalue);
    Serial.print(" L sensor ");
    Serial.print(LSvalue);

  }
 flags();
}

void flags(){
 Serial.print(" flags ");
  if (test){
    Serial.print(" Testing ");
    analogWrite(E1, 0);
    analogWrite(E2, 0);
    delay(testwait);
  }


 /*******************************************************************************************************************************/
 //wall flags
 stop();
 if (FSvalue>fwall && RSvalue<rwall && LSvalue<lwall){ //wall in ahead but not to the left or right
    Reverse(reverse);
    FWall();
 }else if (analogRead(fsensor)>fwall && analogRead(Rsensor)>rwall && analogRead(Lsensor)<lwall){ //wall ahead and to the right
    Reverse(reverse);
    FWall();
 }else if (analogRead(fsensor)>fwall &&analogRead(Rsensor)<rwall && analogRead(Lsensor)>lwall){ //wall ahead and to the left
    Reverse(reverse);
    FLWall();
 }else if (analogRead(fsensor)>fwall &&analogRead(Rsensor)>rwall && analogRead(Lsensor)>lwall){ //wall ahead left and right
    Reverse(reverse);
    FRLWall();
 // veering flags
 }else if (analogRead(fsensor)<clear  &&analogRead(Rsensor)>rvering && analogRead(Lsensor)<lvering){ 
    Reverse(rev);
    turnLeft(turn45);
 }else if (analogRead(fsensor)<clear  && analogRead(Rsensor)<rvering && analogRead(Lsensor)>lvering){
    Reverse(rev);
    turnRight(turn45);
  }else if (analogRead(fsensor)<clear){
    Clear(dist);
 }else {Slow(dist);}

}



void stop(){
 analogWrite(E1, stp);
  analogWrite(E2, stp);
 
}

 
 
void Slow(int dist){
  digitalWrite(13,LOW);
  Serial.print(" slow");
  digitalWrite(M1,HIGH); 
  digitalWrite(M2, LOW);
  analogWrite(E1, slow);
  analogWrite(E2, slow);
  delay(dist);
 
  } 

void Clear(int distance){
  Serial.print(" clear");
  digitalWrite(M1,HIGH); 
  digitalWrite(M2, LOW);
  analogWrite(E1, fast);
  analogWrite(E2, fast);
  delay(distance);
  
  } 
 

void FWall(){
  digitalWrite(13,LOW);
  Serial.print(" F wall"); 
   digitalWrite(M1,LOW); 
   digitalWrite(M2, HIGH);
   analogWrite(E1, 0);
   analogWrite(E2, 0);
   delay(wallDelay);
   turnLeft(turn90);
   }


void FLWall(){
  digitalWrite(13,LOW);
  Serial.print(" F L wall");
   digitalWrite(M1,LOW); 
   digitalWrite(M2, HIGH);
   analogWrite(E1, 0);
   analogWrite(E2, 0);
   delay(wallDelay);
   turnRight(turn90);
   }

void FRLWall(){
   digitalWrite(13,LOW);
   Serial.print(" F R L  wall");
   digitalWrite(M1,LOW); 
   digitalWrite(M2, HIGH);
   analogWrite(E1, 0);
   analogWrite(E2, 0);
   delay(wallDelay);
   turnLeft(turn180);
  }

void turnRight(int turnDelay){
    Serial.print(" turn Right");
    digitalWrite(M1,LOW); 
    digitalWrite(M2, LOW);
    analogWrite(E1, fast);
    analogWrite(E2, fast);
    delay(turnDelay);
   } 
  
  void turnLeft(int turnDelay){
  Serial.print(" turn Left");
    digitalWrite(M1,HIGH); 
    digitalWrite(M2, HIGH);
    analogWrite(E1, fast);
    analogWrite(E2, fast);
    delay(turnDelay);
    } 
  
 

  
  void Reverse(int rev){
    digitalWrite(13,LOW);
  Serial.print(" Reverse");
  digitalWrite(M1,LOW); 
   digitalWrite(M2, HIGH);
    analogWrite(E1, fast);
    analogWrite(E2, fast);
  delay(rev);
   }

Moderator edit: Please read this

I can see no obvious reason for this to crash, I also can't see any recursive calls.


Rob

You could try this free memory function to see if you have a RAM issue:

// this function will return the number of bytes currently free in RAM      
extern int  __bss_end; 
extern int  *__brkval; 
int freemem()
{ 
 int free_memory; 
 if((int)__brkval == 0) 
   free_memory = ((int)&free_memory) - ((int)&__bss_end); 
 else 
   free_memory = ((int)&free_memory) - ((int)__brkval); 
 return free_memory; 
}

You might want to rethink your strategy of having variables/functions which only differ in name by case:

int fwall =75;
int rwall=  40;
int lwall = 40;

...

void FWall(){
...
}

I think but m not to sure that I'm running out of SRAM from the recursive methods im calling

Can you say exactly which functions you believe are being called recursively?

... clearing SRAM stack?

You can't do that, nor would you want to.

sorry indirectly recursive calls
loop calls flags which calls the corresponding method depending on the flag set and
that calls another method which then goes back to flags until finished then back to loop and so on even though loop is "loop" it self is a recursive call :slight_smile:

thanks for the ram check method ill give that ago tonight and see whats hapning

is there any thing else that would cause the Arduino to crash ?
it looks like it gets stuck in a method and doesn't go any further....

loop calls flags which calls the corresponding method depending on the flag set and that calls another method which then goes back to flags until finished then back to loop and so on even though loop is "loop" it self is a recursive call

If that really is the case, and given that none of us can see the recursion, then I would suggest that your code is way too convoluted and difficult to maintain.

That said I can only see a single call to flags() so I still don't see any recursion.


Rob

I’m very sorry
i posted the wrong code
how ever both of these do the same thing they both chrash :confused:

#include <Arduino.h>.
boolean test =false;
int testwait =10;
//
int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;
/
/
int fsensor =A0;
int Lsensor =A1;
int Rsensor =A2;
int Lbump= A4;
int Rbump =A5;
/
/
int FSvalue =0;
int LSvalue =0;
int RSvalue =0;
int LBvalue =0;
int RBvalue =0;
/
/

int pwm_speed;
/********************************************/
//Distances
int fwall =75;
int rwall= 40;
int lwall = 40;
int BUMP =550;

int clear =72;
int rvering =50;
int lvering =50;
//
//Speed
int stp;
int fast = 180;
int slow = 100;
/
/
//delays
int reverse = 500;
int rev = 30;
int dist = 30;
int turn90 = 220;
int turn180 = 500;
int turn45 =125;
int wallDelay =50;
/****************************************/

/********************************************/
void setup() {

pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
Serial.begin(9600);
pinMode(13, OUTPUT);
}

void loop() {

digitalWrite(13,HIGH); //just an led for debugging
if (test){
analogWrite(E1, 0);
analogWrite(E2, 0);
Serial.println(" Testing");
delay(testwait);

Serial.println("");
FSvalue = analogRead(fsensor);
RSvalue = analogRead(Rsensor);
LSvalue = analogRead(Lsensor);

Serial.print(" F sensor “);
Serial.print(FSvalue);
Serial.print(” R sensor “);
Serial.print(RSvalue);
Serial.print(” L sensor ");
Serial.print(LSvalue);

}
flags();
}

void flags(){
Serial.print(" flags “);
if (test){
Serial.print(” Testing ");
analogWrite(E1, 0);
analogWrite(E2, 0);
delay(testwait);
}

/*******************************************************************************************************************************/
//wall flags
stop();
if (FSvalue>fwall && RSvalue<rwall && LSvalue<lwall){ //wall in ahead but not to the left or right
Reverse(reverse);
FWall();
}else if (analogRead(fsensor)>fwall && analogRead(Rsensor)>rwall && analogRead(Lsensor)<lwall){ //wall ahead and to the right
Reverse(reverse);
FWall();
}else if (analogRead(fsensor)>fwall &&analogRead(Rsensor)<rwall && analogRead(Lsensor)>lwall){ //wall ahead and to the left
Reverse(reverse);
FLWall();
}else if (analogRead(fsensor)>fwall &&analogRead(Rsensor)>rwall && analogRead(Lsensor)>lwall){ //wall ahead left and right
Reverse(reverse);
FRLWall();
// veering flags
}else if (analogRead(fsensor)<clear &&analogRead(Rsensor)>rvering && analogRead(Lsensor)<lvering){
verleft(turn45);
}else if (analogRead(fsensor)<clear && analogRead(Rsensor)<rvering && analogRead(Lsensor)>lvering){
verRight(turn45);
}else if (analogRead(fsensor)<clear){
Clear(dist);
}else {Slow(dist);}

}

void stop(){
analogWrite(E1, stp);
analogWrite(E2, stp);

}

void Slow(int dist){
digitalWrite(13,LOW);
Serial.print(" slow");
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
analogWrite(E1, slow);
analogWrite(E2, slow);
delay(dist);
flags();
}

void Clear(int distance){
Serial.print(" clear");
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
analogWrite(E1, fast);
analogWrite(E2, fast);
delay(distance);
flags();
}

void FWall(){
digitalWrite(13,LOW);
Serial.print(" F wall");
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
analogWrite(E1, 0);
analogWrite(E2, 0);
delay(wallDelay);
turnLeft(turn90);
flags();
}

void FLWall(){
digitalWrite(13,LOW);
Serial.print(" F L wall");
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
analogWrite(E1, 0);
analogWrite(E2, 0);
delay(wallDelay);
turnRight(turn90);
flags();
}

void FRLWall(){
digitalWrite(13,LOW);
Serial.print(" F R L wall");
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
analogWrite(E1, 0);
analogWrite(E2, 0);
delay(wallDelay);
turnLeft(turn180);
flags();
}

void turnRight(int turnDelay){
Serial.print(" turn Right");
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, fast);
analogWrite(E2, fast);
delay(turnDelay);
}

void turnLeft(int turnDelay){
Serial.print(" turn Left");
digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, fast);
analogWrite(E2, fast);
delay(turnDelay);
}

void verleft(int turndelay){
Serial.print(“verLeft”);
reverse();
turnleft(turndelay);
flags();
}

void verright(int turndelay){
Serial.print(“verRight”);
reverse();
turnRight(turndelay);
flags();

}

void Reverse(int rev){
digitalWrite(13,LOW);
Serial.print(" Reverse");
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
analogWrite(E1, fast);
analogWrite(E2, fast);
delay(rev);
}

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.

So, flags() calls a bunch of functions. Each of those functions calls flags(). Eventually, you run out of stack space, because nothing ever returns and pops anything off the stack. It seems pretty obvious what you need to do.

Yeah, clear the SRAM stack! Oh, wait ...

@op Just joking.

Refactor without the recursive calls. I'm not sure what they are trying to do.

is there any thing else that would cause the Arduino to crash ?

Your calls are what is doing it.