Trouble in reading speed of accelerometer


hello guys,

im doing project using adafruit adxl377 + gps + adafruit fona:

the resume of doing is:

1. getting readings analogs of accel.
2. if value reach 10g example must send gps coord. via text msg.
3. ploting values in computer google earth.

im using tinygps library gps.

i did program , main problem loop function calling readlocation(); has long delay cause of millis(); use guess, reading time of accelerometer compromissed. dont know best way fix it, call readlocation(); when analogread reach value ?

i dont program , need light here.  :smiley-cry:



code: [select]
#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 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 , 6 interfering comms 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 common units
  // scale map depends on if using 5v or 3.3v microcontroller
  //float scaledx, scaledy, scaledz; // scaled values 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;

  // 1 second parse gps data , report key values
  (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (serial1.available())
    {
      int c = serial1.read();
//      serial.print((char)c); // if uncomment see raw data gps
      ++chars;
      if (gps.encode(c)) // did new valid sentence come in?
        newdata = true;
    }
  }
 
  if (newdata)
  {
    // have location fix output lat / long , time 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 haven't got chars wiring issue
    serial.println("check wiring");
  }
  else if(secondstofirstlocation == 0){
    // still working
  }

 
}




}

don't call readlocation() every time through loop().  that slows down loop().

look @ tinygps library samples.  the code in loop() should check see if there character available.  if there is, read , parse it.  if message complete, save new location.  that takes little time , assure if need location set.

then can read accelerometers , send location if limits exceeded.


Arduino Forum > Using Arduino > Programming Questions > Trouble in reading speed of accelerometer


arduino

Comments