Trouble in reading speed of accelerometer

Hello guys,

Im doing a project using a Adafruit adxl377 + gps + Adafruit fona:

The resume of what i am doing is:

  1. getting readings from analogs of accel.
  2. if some value reach 10g for example he must send the gps coord. via text msg.
  3. ploting those values in another computer with google earth.

Im using the tinygps library for the gps.

I did this program and the main problem is the loop function its calling readLocation(); that has a long delay cause of the millis(); use i guess, so the reading time of the accelerometer its compromissed. I dont know what its the best way to fix it, call the readLocation(); only when the analogread reach some value ?

I really dont usually program and need some light here. :sob:

#include "TinyGPS.h"
#include "Adafruit_FONA.h"
#include "stdlib.h"


TinyGPS gps;

//Definicao RX/TX/RST FONA
#define FONA_RX 11
#define FONA_TX 10
#define FONA_RST 3

//Definicao RX/TX GPS
#define GPS_TX_DIGITAL_OUT_PIN 5
#define GPS_RX_DIGITAL_OUT_PIN 6


#ifdef __AVR__
#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
#else
HardwareSerial *fonaSerial = &Serial1;
#endif

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);

uint8_t type = FONA800L;

int scale = 200;
unsigned int ac1, ac2, ac3;
char sendto[21] = "88341799";
char message[140];
long startMillis;
long secondsToFirstLocation = 0;
float scaledX, scaledY, scaledZ;

#define DEBUG

float latitude = 0.0;
float longitude = 0.0;

char lats[16];
char lons[16];

void setup()

{


Serial.begin(115200);
  Serial.println(F("FONA basic test"));
  Serial.println(F("Initializing....(May take 3 seconds)"));

  fonaSerial->begin(4800);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find FONA"));
    while (1);
  }
  type = fona.type();
  Serial.println(F("FONA is OK"));
  Serial.print(F("Found "));
  
  


  
  #ifdef DEBUG
  Serial.begin(19200);
  #endif
  
  // Serial1 GPS
  Serial1.begin(9600);
  // Serial2 ADXL377
  Serial2.begin(9600);
  // Serial3 sms
  Serial3.begin(115200);

  
  // prevent controller pins 5 and 6 from interfering with the comms from GPS
  pinMode(GPS_TX_DIGITAL_OUT_PIN, INPUT);
  pinMode(GPS_RX_DIGITAL_OUT_PIN, INPUT);
  
  startMillis = millis();
  Serial.println("Starting");
}

void flushSerial() {
  while (Serial3.available())
    Serial3.read();
}

void loop()
{
 //Reset da array das coordenadas GPS
 memset(message,0,sizeof(message));

  readLocation();

  int rawX = analogRead(A0);
  int rawY = analogRead(A1);
  int rawZ = analogRead(A2);

  // Scale accelerometer ADC readings into common units
  // Scale map depends on if using a 5V or 3.3V microcontroller
  //float scaledX, scaledY, scaledZ; // Scaled values for each axis
  
    scaledX = mapf(rawX, 0, 1023, -scale, scale);
    scaledY = mapf(rawY, 0, 1023, -scale, scale);
    scaledZ = mapf(rawZ, 0, 1023, -scale, scale);

Serial.print("X: ");
  Serial.println(rawX);
  Serial.print("Y: ");
  Serial.println(rawY);
  Serial.print("Z: ");
  Serial.println(rawZ);
  Serial.println();

  // Print out scaled X,Y,Z accelerometer readings
  Serial.print("X: ");
  if(scaledX < 0) scaledX = -scaledX;
  Serial.print(scaledX);
  Serial.println(" g");
  Serial.print("Y: ");
  if(scaledY < 0) scaledY = -scaledY;
  Serial.print(scaledY);
  Serial.println(" g");
  Serial.print("Z: ");
  if(scaledZ < 0) scaledZ = -scaledZ;
  Serial.print(scaledZ);

    
  Serial.println(" g");
  Serial.println();






}

float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}


//--------------------------------------------------------------------------------------------
void readLocation(){
  
  bool newData = false;
  unsigned long chars = 0;
  unsigned short sentences, failed;

  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (Serial1.available())
    {
      int c = Serial1.read();
//      Serial.print((char)c); // if you uncomment this you will see the raw data from the GPS
      ++chars;
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
    }
  }
  
  if (newData)
  {
    // we have a location fix so output the lat / long and time to acquire
    if(secondsToFirstLocation == 0){
      secondsToFirstLocation = (millis() - startMillis) / 1000;
      Serial.print("Acquired in:");
      Serial.print(secondsToFirstLocation);
      Serial.println("s");
    }
    
    unsigned long age;
    gps.f_get_position(&latitude, &longitude, &age);
    
    latitude == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : latitude;
    longitude == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : longitude;


    Serial.print("Location: ");
    Serial.print(latitude, 6);
    Serial.print(" , ");
    Serial.print(longitude, 6);
    Serial.println("");
    
    dtostrf(latitude, 10, 6, lats);
    strcat(message, lats);
    strncat(message," ",2);
    
    dtostrf(longitude, 10, 6, lons); 
    strcat(message, lons);   
    
        
    Serial.println (message);   
        
    
    
    if (scaledX > 3.0) fona.sendSMS(sendto, message); 
    if (scaledY > 3.0) fona.sendSMS(sendto, message);
    if (scaledZ > 3.0) fona.sendSMS(sendto, message);
    

 
  if (chars == 0){
    // if you haven't got any chars then likely a wiring issue
    Serial.println("Check wiring");
  }
  else if(secondsToFirstLocation == 0){
    // still working
  }

  
}




}

Don't call readLocation() every time through loop(). That just slows down loop().

Look at the tinyGPS library samples. The code in loop() should check to see if there is a character available. If there is, read it and parse it. If a message is complete, save the new location. That takes very little time and will assure that if you need a location it is already set.

Then you can read the accelerometers and send the location if the limits are exceeded.

johnwasser:
Don't call readLocation() every time through loop(). That just slows down loop().

Look at the tinyGPS library samples. The code in loop() should check to see if there is a character available. If there is, read it and parse it. If a message is complete, save the new location. That takes very little time and will assure that if you need a location it is already set.

Then you can read the accelerometers and send the location if the limits are exceeded.

What of the 3 examples you mean the "simple test" ?

khaoz87:
What of the 3 examples you mean the "simple test" ?

Yikes! I didn't expect the examples to be so convoluted!
What I was expecting from an example was:

void loop() {
  if (ss.available()) {
    char c = ss.read();
    if (gps.encode(c)) {
      float flat, flon;
      unsigned long age;
      gps.f_get_position(&flat, &flon, &age);
      Serial.print("LAT=");
      Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
      Serial.print(" LON=");
      Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
      Serial.print(" SAT=");
      Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
      Serial.print(" PREC=");
      Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
    }
  }
}

I will try that out and post results later, thanks ! :slight_smile: