setup() code being skipped

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jrwst36
 
Posts: 39
Joined: Sun Apr 17, 2011 8:22 am

setup() code being skipped

Post by jrwst36 »

My project uses GPS, WiFi, and a SD card storage, and the components are a Feather M0 with WiFi, SD wing (Adalogger), and Ultimate GPS wing.

I want to create a new file upon start of the program and the file name will use the date and time collected by the GPS. I have a while loop in the setup to make sure the GPS actually captures the date and time before proceeding. However, the once the GPS does get the correct date and time, the program goes straight to the loop() portion of the program. I.e., the rest of the startup code is skipped. I'm baffled, and would appreciate any advice. My code is below.

Code: Select all

/* WiFi Logger
Version 1
Date: 9/12/2021
Author: Jesse Williams

This program records wifi SSID names and strengths, and the GPS location of the hardware

Components:
Microntroller: Adafruit Feather M0 + WiFi https://www.adafruit.com/product/3061
Datalogger: Adalogger https://www.adafruit.com/product/2922
GPS: Adafruit Ultimate GPS https://www.adafruit.com/product/3133
Carrier board: Adafruit wing tripler https://www.adafruit.com/product/3417

*/


//////////////////
//  Wifi setup  //
//////////////////
#include <SPI.h>
#include <WiFi101.h>


////////////////
//  SD setup  //
////////////////
#include <SD.h>
const int chipSelect = 10;


/////////////////
//  GPS setup  //
/////////////////
#include <Adafruit_GPS.h>
#define GPSSerial Serial1
Adafruit_GPS GPS(&GPSSerial);
#define GPSECHO false
uint32_t timer = millis();


/////////////
//  Setup  //
/////////////
void setup() {
  //////////////////////////////
  // Start serial connection  //
  //////////////////////////////
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Starting");


  /////////////////
  //  Start GPS  //
  /////////////////
  GPS.begin(9600);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
  delay(1000);

  
  ////////////////
  //  Start SD  //
  ////////////////
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }


  //////////////////
  //  Start wifi  //
  //////////////////
  WiFi.setPins(8,7,4,2);
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }
  
  // Take GPS reading

  Serial.println("Starting GPS");
  int GPS_year = 100;
  
  // The GPS year must be between 20 and 50
  while(GPS_year < 20 || GPS_year > 50){
    char c = GPS.read();
    if (GPS.newNMEAreceived()) {
      Serial.print(GPS.lastNMEA());
      if (!GPS.parse(GPS.lastNMEA()))
        return;
    }

    GPS_year = GPS.year;
    Serial.println(GPS.year);
    Serial.println(GPS.year, DEC);
    delay(100);
  }

  // THE REST OF THE SETUP CODE DOES NOT GET EXECUTED.

  // Wait a 2 seconds to let the GPS get the date
  Serial.println("Waiting");
  Serial.println(GPS_year);
  delay(10000);
  Serial.println("Done waiting");

  // More code to be populated below

  // Get the datetime


  // Make a new file with using the datetime as the filename

}

void loop() {
  //Serial.println("New Loop");
  // Take GPS Reading
  char c = GPS.read();
  // 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 trying to print out data
    Serial.print(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
  }
  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) {
    Serial.println("Reading in loop");
    timer = millis(); // reset the timer
    Serial.print("\nTime: ");
    if (GPS.hour < 10) { Serial.print('0'); }
    Serial.print(GPS.hour, DEC); Serial.print(':');
    if (GPS.minute < 10) { Serial.print('0'); }
    Serial.print(GPS.minute, DEC); Serial.print(':');
    if (GPS.seconds < 10) { Serial.print('0'); }
    Serial.print(GPS.seconds, DEC); Serial.print('.');
    if (GPS.milliseconds < 10) {
      Serial.print("00");
    } 
    else if (GPS.milliseconds > 9 && GPS.milliseconds < 100) {
      Serial.print("0");
    }
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");
    Serial.print(GPS.day, DEC); Serial.print('/');
    Serial.print(GPS.month, DEC); Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: "); Serial.print((int)GPS.fix);
    Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
    
    if (GPS.fix) {
      Serial.print("Location: ");
      Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
      Serial.print(", ");
      Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
      Serial.print("Speed (knots): "); Serial.println(GPS.speed);
      Serial.print("Angle: "); Serial.println(GPS.angle);
      Serial.print("Altitude: "); Serial.println(GPS.altitude);
      Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
    }
  }

  // If fix >= 1 proceed with the rest of the program

  // Scan wifi SSID

  // For SSID in list

  // Write SSID name, strength, lat, long to SD

}



User avatar
dastels
 
Posts: 15658
Joined: Tue Oct 20, 2015 3:22 pm

Re: setup() code being skipped

Post by dastels »

I suspect this:

Code: Select all

if (!GPS.parse(GPS.lastNMEA()))
        return;
is exiting setup(), and thus moving on to repeatedly call loop().

Dave

User avatar
jrwst36
 
Posts: 39
Joined: Sun Apr 17, 2011 8:22 am

Re: setup() code being skipped

Post by jrwst36 »

Doh!

Thank you.

User avatar
dastels
 
Posts: 15658
Joined: Tue Oct 20, 2015 3:22 pm

Re: setup() code being skipped

Post by dastels »

My pleasure. Some times all that's needed is a fresh set of eyes on it. I'm always happy to provide them.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”