Pages: [1]   Go Down
Author Topic: Arduino keeps restarting sketch  (Read 1159 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, this is my first message here. I am doing a project on wireless sensor networks using Arduino (Arduino UNO to be more precise). Right now I have different sketches for different modules. One of the sketches receives information through an XBee radio and checks to see if it had already received it and if it hadn't it will send it (everything using broadcast). I am using the Arduino XBee API.

The problem I've got is that the sketch keeps restarting, so there is no way to know if I had received it already. I have read that too many Serial.print() might cause this because of RAM issues, but I have removed almost all of them, so I don't think it will be that. Here is the code, in case anyone can shed some light.

The variable names are in Spanish, so if anyone has any question about what anything does, please, don't hesitate to ask me.

Thank you!

PS: Please note that a lot of the code is commented (almost all of the Serial.print()).

Code:
#include <XBee.h>

XBee xbee;
XBeeAddress64 addr64;
ZBTxRequest zbTx;
ZBRxResponse rx = ZBRxResponse();
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
uint8_t historial[10][9] = {0};
uint8_t payload[16] = { 0 };
int indiceHistorial;

const byte posContador = 8;
const byte posSaltos = 9;
const byte posTipo = 10;
const byte posLongitud = 11;
const byte posMensaje = 12;

void setup()
{
  // Tell XBee to start Serial
  xbee = XBee();
  xbee.begin(9600);
  addr64 = XBeeAddress64(0x0, 0xFFFF);
  indiceHistorial = 0;
  delay(10000);
  Serial.flush();
  Serial.println("INICIADO");
}

void loop()
{
  recibirMensaje();
  Serial.flush();
}

void recibirMensaje()
{
  xbee.readPacket(100);
  if (xbee.getResponse().isAvailable())
  {
    if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
    {
      xbee.getResponse().getZBRxResponse(rx);
      for(int i = 0; i < 16; i++)
      {
        payload[i] = rx.getData(i);
      }
      // Check to see if the message was already received
      boolean distintos = true;
      for(int i = 0; i < 10 && distintos; i++)
      {
        int iguales = 0;
        for(int j = 0; j < 9; j++)
        {
          if(historial[i][j] == payload[j])
          {
            iguales++;
          }
        }
        
        if(iguales > 8)
        {
          distintos = false;
        }
      }
      
      // If it didn't exist already
      if(distintos)
      {
       // Resend message
        payload[posSaltos]++;
        delay(100);
        enviarMensaje();
      }
      else
      {
        Serial.println();
        Serial.println("Ya se habia recibido este mensaje");
      }
      
    }
    else
    {
      if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE)
      {
        xbee.getResponse().getZBTxStatusResponse(txStatus);
        
        if (txStatus.getDeliveryStatus() != SUCCESS)
        {
          Serial.println();
          Serial.println("REENVIANDO");
          enviarMensaje();
        }
      }
    }
  }
  /*else if (xbee.getResponse().isError())
  {
    Serial.println();

    if(xbee.getResponse().getErrorCode() == CHECKSUM_FAILURE)
    {
      Serial.println("ERROR - CHECKSUM_FAILURE");
    }
    else if(xbee.getResponse().getErrorCode() == PACKET_EXCEEDS_BYTE_ARRAY_LENGTH)
    {
      Serial.println("ERROR - PACKET_EXCEEDS_BYTE_ARRAY_LENGTH");
    }
    else if(xbee.getResponse().getErrorCode() == UNEXPECTED_START_BYTE)
    {
      Serial.println("ERROR - UNEXPECTED_START_BYTE");
    }
  }*/
}

void enviarMensaje()
{
  // First 9 positions indicate unique id
  for(int i = 0; i < 9; i++)
  {
    historial[indiceHistorial][i] = payload[i];
  }
  
  indiceHistorial++;
  
  if(indiceHistorial > 9)
  {
    indiceHistorial = 0;
  }
  
  zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
  xbee.send(zbTx);
  
  Serial.println();
  Serial.print("Historial: ");
  Serial.println(indiceHistorial);/*
  Serial.println();
  for(int i = 0; i < 10; i++)
  {
    Serial.flush();
    Serial.print("Fila ");
    Serial.print(i);
    Serial.print(": ");
    for(int j = 0; j < 9; j++)
    {
      Serial.print(historial[i][j], HEX);
    }
    Serial.println();
  }*/
}
« Last Edit: July 07, 2012, 08:41:49 am by jposuna » Logged

Offline Offline
Faraday Member
**
Karma: 65
Posts: 2529
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Are you using a little "9V" battery?
Provide some details regarding your setup, maybe a picture.
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What version of the IDE are you using. All those Serial.flush() statements are almost certainly not needed.

How is the XBee connected to the Arduino? If it is connected to the Arduino's RX and TX pins, those Serial.flush() statements MUST go.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am powering the Arduino through the USB port.

I put those Serial.flush(); since the Serial.print(); wouldn't print all the messages (it would print some, but not all). The Serial.print() is only for debugging purposes, so I can see the output on the IDE window (btw, Arduino 1.0.1). To connect the Arduino to the XBee I am using the ArduinoXBeeShield.

I am using Arduino Uno rev 3, XBee Pro S2.

Here is a picture:


Thank you!
Logged

Offline Offline
Faraday Member
**
Karma: 65
Posts: 2529
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Call it a "wild guess", but I think that VUSB is pooping out.
When too much starts happening, current demand goes up, VUSB dips and causes the Arduino to start over.
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

0
Offline Offline
Edison Member
*
Karma: 8
Posts: 1411
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Also, keep in mind that the XBee Pro takes more than 200mA when transmitting. That may cause a voltage drop.
Have you tried the same thing with a regular XBee?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I will try to put some batteries and see if that works.

I can't use a series 1 XBee since all I have are these, but I hope the solution above works.
Logged

Offline Offline
Faraday Member
**
Karma: 65
Posts: 2529
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just to be clear, I was not advocating use of a "9V battery" (once known as a "transistor battery").  They are next to useless.
You need something with more heft: a 9V battery pack - from 6 'AA' or 'C' cells, an RC pack, or similar.
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I've got some of these black cases that admit 4 AA batteries.

Thanks smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I managed to solve the problem. It had nothing to do with the memory or the power supply.

The Arduino XBee API is bugged. To anyone having the same problem as I had, I suggest you change the following function in the XBee.cpp to what I have posted (the solution is not mine):

Code:
void XBeeResponse::reset() {
init();
_apiId = 0;
_msbLength = 0;
_lsbLength = 0;
_checksum = 0;

        // FIX for reset routine
int counter = 0;
if (_frameLength < MAX_FRAME_DATA_SIZE)
counter = _frameLength;
else
counter = MAX_FRAME_DATA_SIZE;

_frameLength = 0;

_errorCode = NO_ERROR;

for (int i = 0; i < counter; i++) {
getFrameData()[i] = 0;
}
}

Source: http://code.google.com/p/xbee-arduino/issues/detail?id=23
Logged

Pages: [1]   Go Up
Jump to: