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");
}
}