2

I am using a Fona 808 shield with an Arduino Uno and the LSM303 accelerometer. The accelerometer is connected via I2C and the Fona uses software serial.

My project requires the accelerometer to be continuously polled and occasionally talk with the fona. I wrote a program that just used the fona, but once I added the accelerometer code the program would hang a few seconds after comms had been opened with the Fona. I have tested both the accelerometer and the fona shield separately, and they work fine. So, I wrote another smaller (test) program (which is attached below) to find the origin of the program, which I identified to be:

fonaSerial->begin(4800);

fona.begin(*fonaSerial);

After these lines of code are added, the problem arises. As I said before, this is what I see a few seconds after comms have been opened with the Fona. http://pasteboard.co/A16708fo3.png

My test program reads the value from the accelerometer, calculates the magnitude of the acceleration and if it's over 20, it prints "Over 20", while also initialising Fona serial.

My question is: Why don't the accelerometer (I2C) and the Fona (Serial) work together and how this be fixed?

This is quite urgent so a quick responses are greatly appreciated!

My Code:

#include <Adafruit_FONA.h>
#include <Adafruit_Sensor.h>
#include <My2_Adafruit_LSM303_U.h>
#include <Wire.h>


#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4


#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;


Adafruit_FONA fona = Adafruit_FONA(FONA_RST);


volatile byte accelMagnitude;

Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(54321);

void setup() {
  // put your setup code here, to run once:

    accel.begin();

    Serial.begin(115200);


    fonaSerial->begin(4800);
    fona.begin(*fonaSerial);

}


void ReadAccel(){

//Get a new accelerometer sensor event
  sensors_event_t event;
 // Serial.println(F("Accel---------"));
  accel.getEvent(&event);

  accelMagnitude = sqrt( sq(event.acceleration.x) + sq(event.acceleration.y) + sq(event.acceleration.z));

}


void loop() {
    // put your main code here, to run repeatedly:

    ReadAccel();

    if (accelMagnitude > 20){

        while (accelMagnitude > 20){

            Serial.println("waiting");
            ReadAccel();
        }

        Serial.println("Over 20");

    }
}
  • Try adding some additional Serial.print commands, so you can see where the programs gets stuck. – Gerben Feb 19 '17 at 15:39
  • The code works fine for about 4 seconds after opening comm with ATs and then it just stops completely. – nford64 Feb 19 '17 at 15:42
  • 1
    How do you know that is stops completely? It could be that the accelerometer is returning false data, and that the arduino code is still running fine. That's why I suggested adding additional Serial.println commands. When debugging some code, I sometimes have a Serial.println between every line of code, so I know exactly where the problem occurs. – Gerben Feb 19 '17 at 15:49
  • I added Serial.println("Hello"); into the loop. Hello is printed continuously and then stops after 4 seconds. – nford64 Feb 19 '17 at 15:52
  • 1
    Thank you, Thank you, Thank you, Thank you!!!!! The connection to the cellular network seems to be the problem. I added a 10 sec delay before starting the loop to allow the Fona to connect to the network! – nford64 Feb 19 '17 at 16:45
0

As Gerben suggests, it would not be a good practice to just add a delay and call it a day.

1) Have a large electrolytic capacitor between Vcc and GND as near as you can to the FONA and

2) FONA has getNetworkStatus(); make a small control loop and query it until you get status 1 or 5, depending on what network are you on. From FONA's example:

if (n == 0) Serial.println(F("Not registered"));
if (n == 1) Serial.println(F("Registered (home)"));
if (n == 2) Serial.println(F("Not registered (searching)"));
if (n == 3) Serial.println(F("Denied"));
if (n == 4) Serial.println(F("Unknown"));
if (n == 5) Serial.println(F("Registered roaming"));

And then proceed with the rest of your code. You can make additional checks for signal quality with getRSSI();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.