0

Serial deletion wierdness
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Serial deletion wierdness

by Lord Azrael on Wed Nov 05, 2008 12:11 pm

Hi, I'm using a Diecimila to control various I/O and motors on a robot. I'm using C-functions and for debug purposes I put some Serial.println calls in them to see if the programme ever reached the functions. I got it all working then decided I'd reduce code size by deleting or commenting out the Serial.println lines. This resulted in the robot becoming completely unresponsive. i tried putting them back in again and it worked fine. Then i tried removing just one Serial.println and resulted in the robot endlessly looping through the function it was removed from as well as any subservient function calls made by that function. Again when replaced it works fine.

Does anyone know any reasons why this might happen? Any Solutions?
Thanks in advance,
Will :?
Lord Azrael
 
Posts: 3
Joined: Wed Nov 05, 2008 12:02 pm

Re: Serial deletion wierdness

by westfw on Wed Nov 05, 2008 1:32 pm

Post your code?

For one thing, Serial.println() is quite slow; at 9600bps it takes approximately one millisecond for each character that is printed, so having a lot of serial.println calls is about like having a lot of calls to delay(); such delays might in fact be needed for controller slow external devices like motors, and this could account for the bot becoming unresponsive... I don't know about the "endlessly looping" function, though...
westfw
 
Posts: 1582
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Serial deletion wierdness

by Lord Azrael on Wed Nov 05, 2008 3:34 pm

Hi, the code seems to jump directly into the function indicated by the /-----------------------/ and rolls through that interminably. this behaviour seems to be caused by deleting or commenting out the line marked by /******************/. The code worked fine before adding all the debug lines (I added them to fine tune some of the delays) with Serial.println() and worked fine after their addition. when any of them are removed however, the programme behaves unpredictably and most definitely does not work.
I realise it's probably quite hard to delve thru a mass of code like this, the code itself works fine it's only when a Serial.println() is cut out.
Thanks,
Will

Code: Select all | TOGGLE FULL SIZE
/**********************************************************
code to drive robot in simple obstacle avoidance mode.  when it bumps into anything it will reverse and turn.
if the rear bump sensor is pressed at any point except when reversing then it will shutdown.
the robot uses lego mindstorms motors and bump sensors.  the arduino decimila board returns randomish 1s or 0s when a
switch (bumpsensor) is open circuit, but never more than 3 1s in a row unless physically closed (hence the checking routines)
*****************************************************************************************/

#define TURNPERIOD 2 //times 255
#define REVPERIOD 500 //times 2 - reverse period
#define DETACHPERIOD 750

int lmotfwd = 5; //left motor forwards
int lmotbwd = 3;
int rmotfwd = 9;
int rmotbwd = 6;
int bbs = 2;  //backbumpsensor
int rbs = 7;  //rightbumpsensor
int lbs = 4;

void shutdown(void);
int goforwards(void);
int lbstest(int lbs); //test left bump sensor
int rbstest(int rbs);
int bbstest(int bbs);
int halt(void);
int backup(void);
int leftavoid(void);
int rightavoid(void);

void setup()
{
  Serial.begin(9600);
  pinMode(rbs, INPUT);
  pinMode(lbs, INPUT);
  pinMode(bbs, INPUT);
  pinMode(rmotfwd, OUTPUT);
  pinMode(lmotfwd, OUTPUT);
  pinMode(rmotbwd, OUTPUT);
  pinMode(lmotbwd, OUTPUT);
  delay(50);
}

void loop()
{
  int stop, left, right,back;
goforwards(); 
if((left = digitalRead(lbs)))
left = lbstest(left);
if(left)
stop = leftavoid();
if(stop) // if bbs pressed at certain points, stop motors and quit
{
  halt();
  shutdown();
}

if((right = digitalRead(rbs)))
right = rbstest(right);
if(right)
stop = rightavoid();
if(stop) //if bbs pressed at certain points, stop motors and quit
{
  halt();
  shutdown();
}

if(back = (digitalRead(bbs)))
back = bbstest(back);
if(back)
{
  halt();
  shutdown();
}
}

int goforwards(void)
{
  Serial.println("goforwards");
  analogWrite(rmotfwd, 255);
  analogWrite(rmotbwd, 0);
  analogWrite(lmotbwd, 0);
  analogWrite(lmotfwd, 255);
  return 0;
}


int lbstest(int left)
{
  int lbs1 = 0;
  int lbs2 = 0;
  Serial.println("lbstest");      /************************/
  lbs1 = digitalRead(lbs);
  delay(5);
  lbs2 = digitalRead(lbs);
  if(lbs1 && lbs2)
  {
    left = 1;
 
  }
  else
  {
    left = 0;
  }
    return left;
 
}

int leftavoid()
{
  int x, error, stop, back;
  int lmotf = 0;
  int lmotb = 255;
   Serial.println("leftavoid");
  error = backup();  //backup a certain distance while checking bbs
  if(error)
  return error;  //if bbs then exit
  for(x=0;x<255;x++) //gradually turn (smoothly)
  {
    lmotf++;
    lmotb--;
    analogWrite(lmotfwd, lmotf);
    analogWrite(lmotbwd, lmotb);
    if(lmotf >255)  //boundary checking
    analogWrite(lmotfwd, 255);
    if(lmotb < 0)
    analogWrite(lmotbwd, 0);
    delay(TURNPERIOD);
    if((back = digitalRead(bbs))) //if bbs detected at this point end programme
    stop = bbstest(back);/*-------------------------------------*/
    if(stop)
    return stop;
  }
  return 0;
}

int rightavoid()
{
  int x, error, stop, back;
  int rmotf = 0;
  int rmotb = 255;
   Serial.println("right avoid");
  error = backup();  //backup a certain distance while checking bbs
  if(error)
  return error;  //if bbs then exit
  for(x=0;x<255;x++) //gradually turn (smoothly)
  {
    rmotf++;
    rmotb--;
    analogWrite(rmotfwd, rmotf);
    analogWrite(rmotbwd, rmotb);
    if(rmotf >255)  //boundary checking
    analogWrite(rmotfwd, 255);
    if(rmotb < 0)
    analogWrite(rmotbwd, 0);
    delay(TURNPERIOD);
    if((back = digitalRead(bbs))) //if bbs detected at this point end programme
    stop = bbstest(back);
    if(stop)
    return stop;
  }
  return 0;
}

int backup()  //reverse and if bbs trig'd then go forwards and turn
{
  int error, back;
  long int x;
    Serial.println("backup");
  analogWrite(lmotbwd, 255);
  analogWrite(lmotfwd, 0);
  analogWrite(rmotbwd, 255);
  analogWrite(rmotfwd, 0);
  for(x=0;x<REVPERIOD;x++)
  {
    delay(1);
    if((back = digitalRead(bbs)))
    back = bbstest(back);
    if(back)
    {
      goforwards();
      delay(DETACHPERIOD);
      return 0;
    }
  }
  return 0;
}

int rbstest(int right)
{
  int rbs1 = 0;
  int rbs2 = 0;
    Serial.println("rbstest");
  rbs1 = digitalRead(rbs);
  delay(5);
  rbs2 = digitalRead(rbs);
  if(right && rbs1 && rbs2)
  {
    right = 1;
  return right;
  }
  else
  {
    right = 0;
    return right;
  }
}

int bbstest(int back)
{
  int bbs1 = 0;
  int bbs2 = 0;
  Serial.println("bbstest");
  bbs1 = digitalRead(bbs);
  delay(5);
  bbs2 = digitalRead(bbs);
  if(bbs1 && bbs2)
  {
    back = 1;
  }
  else
  {
    back = 0;
  }
  return back;
 
}

int halt()
{
  analogWrite(lmotfwd, 0);
  analogWrite(lmotbwd, 0);
  analogWrite(rmotfwd, 0);
  analogWrite(rmotbwd, 0);
  Serial.println("stopping");
  return 0;
}


void shutdown()
{
  while(1)
  {
    ;
  }
}
Lord Azrael
 
Posts: 3
Joined: Wed Nov 05, 2008 12:02 pm

Re: Serial deletion wierdness

by Lord Azrael on Wed Nov 05, 2008 3:41 pm

Code: Select all | TOGGLE FULL SIZE
int leftavoid()
{
  int x, error, stop, back;
  int lmotf = 0;
  int lmotb = 255;
   Serial.println("leftavoid");
  error = backup();  //backup a certain distance while checking bbs
  if(error)
  return error;  //if bbs then exit
  for(x=0;x<255;x++) //gradually turn (smoothly)
  {
    lmotf++;
    lmotb--;
    analogWrite(lmotfwd, lmotf);
    analogWrite(lmotbwd, lmotb);
    if(lmotf >255)  //boundary checking
    analogWrite(lmotfwd, 255);
    if(lmotb < 0)
    analogWrite(lmotbwd, 0);
    delay(TURNPERIOD);
    if((back = digitalRead(bbs))) //if bbs detected at this point end programme
    stop = bbstest(back);/*-------------------------------------*/
    if(stop)
    return stop;
  }
  return 0;
}

int rightavoid()
{
  int x, error, stop, back;
  int rmotf = 0;
  int rmotb = 255;
   Serial.println("right avoid");
  error = backup();  //backup a certain distance while checking bbs
  if(error)
  return error;  //if bbs then exit
  for(x=0;x<255;x++) //gradually turn (smoothly)
  {
    rmotf++;
    rmotb--;
    analogWrite(rmotfwd, rmotf);
    analogWrite(rmotbwd, rmotb);
    if(rmotf >255)  //boundary checking
    analogWrite(rmotfwd, 255);
    if(rmotb < 0)
    analogWrite(rmotbwd, 0);
    delay(TURNPERIOD);
    if((back = digitalRead(bbs))) //if bbs detected at this point end programme
    stop = bbstest(back);
    if(stop)
    return stop;
  }
  return 0;
}

int backup()
{
  int error, back;
  long int x;
    Serial.println("backup");
  analogWrite(lmotbwd, 255);
  analogWrite(lmotfwd, 0);
  analogWrite(rmotbwd, 255);
  analogWrite(rmotfwd, 0);
  for(x=0;x<REVPERIOD;x++)
  {
    delay(1);
    if((back = digitalRead(bbs)))
    back = bbstest(back);
    if(back)
    {
      goforwards();
      delay(DETACHPERIOD);
      return 0;
    }
  }
  return 0;
}

int rbstest(int right)
{
  int rbs1 = 0;
  int rbs2 = 0;
    Serial.println("rbstest");
  rbs1 = digitalRead(rbs);
  delay(5);
  rbs2 = digitalRead(rbs);
  if(right && rbs1 && rbs2)
  {
    right = 1;
  return right;
  }
  else
  {
    right = 0;
    return right;
  }
}

int bbstest(int back)
{
  int bbs1 = 0;
  int bbs2 = 0;
  Serial.println("bbstest");
  bbs1 = digitalRead(bbs);
  delay(5);
  bbs2 = digitalRead(bbs);
  if(bbs1 && bbs2)
  {
    back = 1;
 
  }
  else
  {
    back = 0;
  }
  return back;
 
}

int halt()
{
  analogWrite(lmotfwd, 0);
  analogWrite(lmotbwd, 0);
  analogWrite(rmotfwd, 0);
  analogWrite(rmotbwd, 0);
  Serial.println("stopping");
  return 0;
}


void shutdown()
{
  while(1)
  {
    ;
  }
}
Lord Azrael
 
Posts: 3
Joined: Wed Nov 05, 2008 12:02 pm

Please be positive and constructive with your questions and comments.