MMA7361 Accelerometer

Hi,

I made a sketch to comminunicate via 2 Xbee. All of this (rx_routine and tx_routine) work very well. I was adding the MMA7361 library to my project when evrything stop working (Communication worked ONCE per execution than nothing else).

I have already tested this library (alone) and everything was working fine. I found that if I remove the line //accelero.calibrate();, everything would be working fine again (except the accelerometer).

I checked in the library why it would cause that but I couldn’t see anything. (Just to be sure, I removed all the Serial.print, since I wasnt using this serial port, and wasnt working more)

Why it could do such thing??

Have any suggestion??

here is my code

#include <AcceleroMMA7361.h>


#define rx_lenght  14

//Accelerometer variables 
AcceleroMMA7361 accelero;
int x;
int y;
int z;
int x_angle =0;
int y_angle =0;

unsigned char RxBuffer[rx_lenght], Rx_checksum;
unsigned char TxBuffer[20];
unsigned char checksum;
int valid_message_rx;    
int led = 27;            //LED on pin 27
int mess_received=0;
unsigned char TxXsum,TxXsum2;
int j;

unsigned long time_1=0, time_2=0, time;
unsigned long rs_1, rs_2;

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  Serial1.begin(9600);
  time_1 = millis();
  rs_1 = millis();
  pinMode(led, OUTPUT);

  accelero.begin(36, 37, 38, 39, A0, A1, A2);
  accelero.setARefVoltage(5); //sets the AREF voltage to 5V
  accelero.setSensitivity(LOW); //sets the sensitivity to +/-6G
  //accelero.calibrate();

}

void loop() {

  rx_routine();
  tx_routine();

  rs_2 = millis();
  if(rs_2 - rs_1 >= 1000){
    rs_1 = millis();
    x = accelero.getXAccel();
    y = accelero.getYAccel();
    z = accelero.getZAccel();

  }
}

void serialEvent1() {

  if(Serial1.available()>=rx_lenght) {
    for(int n=0; n <= (rx_lenght-1); n++){
      RxBuffer[n] = Serial1.read();
    }
    mess_received = 1;
  } 
}


void rx_routine(){

  if(mess_received == 1){
    time_1 = millis(); 
    checksum = 0;
    for(int i=3; i <= 12; i++){
      checksum = checksum + RxBuffer[i];
    }
    Rx_checksum = 255 - checksum;
    if(RxBuffer[0] == 0x7E){
      if(RxBuffer[3] == 0x81){
        if(Rx_checksum == RxBuffer[rx_lenght-1]){
          digitalWrite(led, HIGH);
          valid_message_rx = 1;
        }
        else{
          valid_message_rx = 0;
          digitalWrite(led, LOW);
        }
      }
      else{
        valid_message_rx = 0;
        digitalWrite(led, LOW);
      }
    }
    else{
      valid_message_rx = 0;
      digitalWrite(led, LOW);
    }
    mess_received = 0;
  }

  time_2 = millis();           //Time out after a loss communication
  if(time_2 - time_1 >= 500){
    valid_message_rx = 0;
    digitalWrite(led, LOW);
  }

}

void tx_routine(){

  if(valid_message_rx == 1){

    TxBuffer[0] = 0x7E;
    TxBuffer[1] = 0x00;
    TxBuffer[2] = 0x10;
    TxBuffer[3] = 0x01;
    TxBuffer[4] = 0x00;
    TxBuffer[5] = 0x00;
    TxBuffer[6] = 0x01;
    TxBuffer[7] = 0x01;
    TxBuffer[8] = 0x55;
    TxBuffer[9] = x;
    TxBuffer[10] = y;
    TxBuffer[11] = 0x22;
    TxBuffer[12] = 0x22;
    TxBuffer[13] = 0x22;
    TxBuffer[14] = 0x22;
    TxBuffer[15] = 0x22;
    TxBuffer[16] = 0x22;
    TxBuffer[17] = 0x22;
    TxBuffer[18] = 0x55;

    TxXsum2 = 0;
    for(j=3; j <= 18; j++){
      TxXsum2 += TxBuffer[j];
    }
    TxXsum = 0xFF - TxXsum2;
    TxBuffer[19] = TxXsum;

    Serial1.write(TxBuffer,sizeof(TxBuffer)); 
    valid_message_rx = 0;

  }

}

The calibrate function looks like it uses Serial, maybe try commenting out those lines in AcceleroMMA7361.cpp?

Yup, as I mentionned in my post, I removed all the Serial.print. But it make no difference. In my sketch I use Serial1, not Serial.

Thank you anyway

EDIT*****

So, This is the problem I have after including accel.calibrate:

7E,0,A,81,0,1,24,0,0,80,80,80,0,FF,D9 IN READING FOR SERIAL round passed 100ms D9,7E,0,A,81,0,1,24,0,0,80,80,80,FF,CF IN READING FOR SERIAL round passed 100ms 0,D9,7E,0,A,81,0,1,24,0,0,80,80,FF,4F IN READING FOR SERIAL round passed 100ms 7F,0,DA,7E,0,A,81,0,1,24,0,0,80,FF,51 round passed 100ms 7F,0,DA,7E,0,A,81,0,1,24,0,0,80,FF,51 IN READING FOR SERIAL round passed 100ms 80,7F,0,DA,7E,0,A,81,0,1,24,0,0,FF,F7 IN READING FOR SERIAL round passed 100ms 80,80,7F,0,DA,7E,0,A,81,0,1,24,0,FF,F7 IN READING FOR SERIAL round passed 100ms 0,80,80,7F,0,DA,7E,0,A,81,0,1,24,FF,78 IN READING FOR SERIAL round passed 100ms 0,0,80,80,7F,0,DA,7E,0,A,81,0,1,FF,1C IN READING FOR SERIAL round passed 100ms 24,0,0,80,80,7F,0,DA,7E,0,A,81,0,FF,9D round passed 100ms 24,0,0,80,80,7F,0,DA,7E,0,A,81,0,FF,9D IN READING FOR SERIAL round passed 100ms 1,24,0,0,80,80,7F,0,DA,7E,0,A,81,FF,9D IN READING FOR SERIAL

You can see that the 7E (start delimiter) will shift to the right every two or three packets.

If I removed the accel.callibrate, I will receive clean data like this:

7E,0,A,81,0,1,24,0,0,80,80,7F,0,DA,DA round passed 100ms 7E,0,A,81,0,1,24,0,0,80,80,7F,0,DA,DA IN READING FOR SERIAL round passed 100ms 7E,0,A,81,0,1,24,0,0,80,80,7F,0,DA,DA IN READING FOR SERIAL round passed 100ms 7E,0,A,81,0,1,24,0,0,80,80,7F,0,DA,DA IN READING FOR SERIAL round passed 100ms 7E,0,A,81,0,1,24,0,0,80,80,7F,0,DA,DA IN READING FOR SERIAL round passed 100ms 7E,0,A,81,0,1,24,0,0,80,80,7F,0,DA,DA IN READING FOR SERIAL round passed 100ms

Someone know why???

Thanks

EDIT 2****

I finally made it work..

Initialize the Serial1 Port AFTER calibrating the MMA7361 sensor.

I can't find the answer of why is it doing this, so I am asking you...does anyone knows???

Thanks