Go Down

Topic: Programme stuck in setup (Read 184 times) previous topic - next topic

Stude707

I've put together some code to log GPS data and readings from a BMP180 pressure/temperature sensor, but the code gets stuck in the stetup function. Any ideas? Thanks!

Code: [Select]
#include <SPI.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <Wire.h>


Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

#if ARDUINO >= 100
  SoftwareSerial mySerial(3, 2);
#else
  NewSoftSerial mySerial(3, 2);
#endif
Adafruit_GPS GPS(&mySerial);

#define GPSECHO false
const int chipSelect = 10;
boolean usingInterrupt = false;
const int dec_place  = 100;

const int groundpin = 18;             // analog input pin 4 -- ground
const int powerpin = 19;              // analog input pin 5 -- voltage
const int xpin = A3;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
const int zpin = A1;                  // z-axis (only on 3-axis models)

void setup()
{
  Serial.begin(115200);
  Serial.println("Into Setup");
  bmp.begin();
  Serial.println("BMP complete");
 
  GPS.begin(9600);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
  Serial.println("GPS complete");
  delay(1000);
 
  mySerial.println(PMTK_Q_RELEASE);
  Serial.println("Beginning SD test");
  pinMode(10, OUTPUT);
  if (!SD.begin(chipSelect)){
      Serial.println("Recognition of SD card reader failed!");
      return;
  }
  Serial.println("SD test complete");
 
  String hdrstr = "Humidity (%)\tTemp (dht,*C)\tTemp (bmp,*C)\tPressure (hPa)\tAltitude (m)\tX Accel \tY Accel \tZ Accel \tIR\tFull\tVisible\tLux\tTime\tDate\tFix\tQuality\tLat\tLon\tSpeed (kt)\tAngle\tAltitude (ft)\tSatellites";
  //Serial.println(hdrstr);
  File hdrFile = SD.open("datalog.txt", FILE_WRITE);
  if (hdrFile) {
    hdrFile.println(hdrstr);
    hdrFile.close();
    Serial.println(hdrstr);
  }else{
    Serial.println("FAILURE!");
  }
  Serial.println("SD tgest complete"); 
}
uint32_t timer = millis();
void loop()
{
   if (! usingInterrupt) {
    // read data from the GPS in the 'main loop'
    char c = GPS.read();
    // if you want to debug, this is a good time to do it!
    if (GPSECHO)
      if (c) UDR0 = c;
      // writing direct to UDR0 is much much faster than Serial.print
      // but only one character can be written at a time.
  }
 
  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences!
    // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
    //Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false
 
    if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
      return;  // we can fail to parse a sentence in which case we should just wait for another
  }
  if (timer > millis())  timer = millis();

  // approximately every 5 seconds or so, print out the current stats
  if (millis() - timer > 5000) {
    Serial.println("INTO TIMER LOOP");
   
    timer = millis(); // reset the timer
    File dataFile = SD.open("datalog.txt", FILE_WRITE);
    if (dataFile) {
      //Write BMP data to file
      sensors_event_t event;
      bmp.getEvent(&event);
      dataFile.print("Pressure:    ");
      dataFile.print(event.pressure);
      dataFile.println(" hPa");
      float temperature;
      bmp.getTemperature(&temperature);
      dataFile.print("Temperature: ");
      dataFile.print(temperature);
      dataFile.println(" C");
      float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
      dataFile.print("Altitude:    ");
      dataFile.print(bmp.pressureToAltitude(seaLevelPressure,
                                        event.pressure,
                                        temperature));
      dataFile.println(" m");
      dataFile.println("");
      dataFile.print("\t");
      //Write GPS data to file
      dataFile.print(GPS.hour);
      dataFile.print(":");
      dataFile.print(GPS.minute);
      dataFile.print(":");
      dataFile.print(GPS.seconds);
      dataFile.print(".");
      dataFile.print(GPS.milliseconds);
      dataFile.print("\t");
      dataFile.print(GPS.day);
      dataFile.print("/");
      dataFile.print(GPS.month);
      dataFile.print("/20");
      dataFile.print(GPS.year);
      dataFile.print("\t");
      dataFile.print((int)GPS.fix);
      dataFile.print("\t");
      if (GPS.fix) {
        Serial.println("Fix Achieved!");
        dataFile.print((int)GPS.fixquality);
        dataFile.print("\t");
        dataFile.print(GPS.latitude);
        dataFile.print("\t");
        dataFile.print(GPS.lat);
        dataFile.print("\t");
        dataFile.print(GPS.longitude);
        dataFile.print("\t");
        dataFile.print(GPS.lon);
        dataFile.print("\t");
        dataFile.print(GPS.speed);
        dataFile.print("\t");
        dataFile.print(GPS.angle);
        dataFile.print("\t");
        dataFile.print(GPS.altitude);
        dataFile.print("\t");
        dataFile.println(GPS.satellites);
      }else {
        dataFile.println(GPS.fixquality);
      }
      dataFile.close();
      Serial.println("Wrote to file");
    }
  }
}

econjack

I don't know squat about the GPS library, but these two lines make me wonder...

Code: [Select]

  Serial.begin(115200);              // Serial com rate
  Serial.println("Into Setup");
  bmp.begin();
  Serial.println("BMP complete");
 
  GPS.begin(9600);                   // Same com rate???

el_supremo

What does your debugging output tell you? It also might be worth telling us what it prints.

Pete

Stude707

Thanks for the quick replies guys!

When I open up the serial monitor I get something like this:

Into Setup
Into Setup
Into Setup
BMP complete
IntïR?½Setup
Into Setup
BMP complete
IntïR?½Setup
Into Setup
BMP complete
IntïR?½Setup
Into Setup
BMP complete
IntïR?½Setup
Into Setup
BMP complete
IntïR?½Setup
Into Setup
BMP complete
IntïR?½Setup

and it is repeated like that. I've tried matching the com rates but I get the same results. Should I try something else to debug it?

UKHeliBob

My best guess would be that you are running out of memory which will not be helped by the extensive use of String objects as opposed to C style strings.  As a first test reduce the length of hdrstr to a single character.  Do you get any different results ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

el_supremo

There's only one String object in the code. Change this:
Code: [Select]
String hdrstr =
to this:
Code: [Select]
char * hdrstr =

Pete

Stude707

Ah okay, interesting. I've changed it to "char * hdrstr =" and I do get somewhat different results, but I think I'm still low on memory. I'm getting this:

Into Setup
BMP complete
GPS complete
BeginnInto Setup
BMP complete
GPS complete
Beginning SD teInto Setup
BMP complete
GPS complete
Beginning SD teInto Setup
BMP complete
GPS complete
BeginnInto Setup


Stude707

Also I'm using an Uno R3. Do I need a more powerful unit?

UKHeliBob

Define what you mean by more powerful

More memory ?
Faster processor ?
More pins ?
More than one hardware serial port ?
etc, etc
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

wildbill

You can save yourself some RAM by pushing constant serial printed strings into progmem. Change stuff like this:
Code: [Select]

  Serial.println("BMP complete");

to
Code: [Select]

  Serial.println(F("BMP complete"));

Go Up