0

Setup Mystery
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Setup Mystery

by RocketNut on Wed Sep 28, 2016 2:27 pm

Salutations

I have been having trouble with the sketch I am working on It keep messing up the USB serial channel. Now a new one has pop up. The attached screen shot shows this new problem. As you can see in the serial monitor only the SERA DataLogger Master Sketch is displayed. Then it hangs and not showing the next message. Some where between the first and second message the USB serial protocol gets mess up. TOOLS -> PORT get erased, so it does not have a port to use. I have to reset the board and run a simple sketch (Blink supplied example) several times to get the board (Adafruit Feather M0 Adalogger) back on line.

Setup error.jpg
Setup error.jpg (87.17 KiB) Viewed 140 times


Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include <SD.h>


// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// Set the pins used
#define cardSelect 4

File logfile;


// Define Pyro FiringPins & Control Arrays
const int PyroPin[] = {14,15,16,17,18,19};
int PyroStatus[] = {0,0,0,0,0,0}; //PyroFire pulse control
const int BoardLED = 13;
const int SIM_LED = 13;

// Sensor reading variables
float G_X;
float G_Y;
float G_Z;
float GPS_Lat;
float GPS_Lon;
float Speed;
float Alt;

// Define Status varables
boolean Safety_Key = true;
boolean Arm = false;
boolean USB_Cable = true;
boolean Logging = false;
boolean SIM_Card = true;


// Define misc varables
long int Sample = 1;
long int SampleStartTime;
String Data_String = "No Data";
int UserSketchStep = 1;
char filename[15];




//==============================================
void FirePyro(int PyroNum,int PyroFireTime)
{
 digitalWrite(PyroPin[PyroNum], HIGH);
 PyroStatus[PyroNum] = PyroFireTime;
}

//==============================================
 void ResetPyro()
 {
  for(int Z = 0 ; Z<= 5 ; Z++)
  {

   if(PyroStatus[Z] > 0 && PyroStatus[Z] <= millis())
   {
    digitalWrite(PyroPin[Z], LOW);
    PyroStatus[Z] = 0;
   }
  }
 }
     
    //==============================================
void ParseCommand(String com)
{

 // Command format "command(part1)>(delimiter)varables(part2)" Expample -> Start loging at 100 samples per second.
//     the comand string is "StartLoging>100"
//

 String Part1;
 String Part2;

 // Check for command / data serperator
 int ComSeparator = com.indexOf('>'); // Check for command / data serperator
     
//Remove any spaces
  com.replace(" ", "");
     
// Separate the command from the entried com string.
 Part1 = com.substring(0, com.indexOf(">"));
 Part2 = com.substring(com.indexOf(">")+1);

//>>>> Debug Point
//      Serial.print("Command String || Part1 > ");Serial.print(Part1);
//     Serial.print(" || Part2 > ");Serial.print(Part2);Serial.print("(");Serial.print(Part2.length());Serial.print(")");
//      Serial.print(" || ComSeparator > ");Serial.println(com.indexOf('>'));
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


//==================================================
//==================================================
// Command FileName
//
 if(Part1.equalsIgnoreCase("filename"))
 {
  String Temp =Part2;

  Part1 = Temp.substring(0, Temp.indexOf("."));
  Part2 = Temp.substring(Temp.indexOf(".")+1);
  if(ComSeparator<0)
  {
   Serial.println("Command Missing '>' Command/Data Separator");
   return;
  }
  else if(Temp.length() == 0)
  {
   Serial.println("File Name Missing");
   return;
  }
  else if(Part1.length() > 8 || Part2.length() > 3)
  {
   Serial.println("Too Many Characters In Name or Extenrsion (Name Max 8 Char || Ext Max 3 Char)");
   return;
  }
  else if(!SIM_Card)
  {
    Serial.print("Error >> Check SIM Card ||");Serial.println(SIM_Card);
    return;
  }

// SIM is ready. Let's check to see if the file already exists on the SIM Card 
  if (SD.exists(Temp))
  {
    Serial.print(Temp);Serial.println(" Already Exits");
    do           // Wait for answer then check if it's valed
    {
     Serial.print("Over Write The Old File Y/N/ESC > ");
     while(!Serial.available())
     {
     }
     char c = Serial.read();
     if(c == 'N' || c == 'n' || c == 27)
     {
      if(c != 27)
      {
       Serial.println("No");
      }
      else
      {
       Serial.println("");
      }
       return;
     }
     else if(c == 'Y' || c == 'y' || c == 27)
     {
      Serial.println("");
      SD.remove(Temp);
      NewFileName(Temp);
      return;
     }
     else
     {
      Serial.println("??");
     }
    } while(1); // Bad char was entered so redo the question.
   }
   else
   {
    NewFileName(Temp);
    return;
   }
  }
//==================================================
//==================================================
// Command firepyro
//
 else if(Part1.equalsIgnoreCase("firepyro"))
  {
   if(ComSeparator<0)
   {
    Serial.println("Command Missing '>' Command/Data Separator");
    return;
   }

// Check Safety if not pulled then abort command
   if((Safety_Key = digitalRead(Safety_Key)))
   {
    Serial.println("Safety Key Inserted Can Not Fire Pyro(s)");
    return;
   }
   int PyroFireTime = millis() +1500;
   if(Part2.length() == 0)
   {
    Serial.println("Firing Pyro << Pyro Number Missing");
    return;
    }
    else if(Part2.length() == 1)
    {
     FirePyro((Part2.toInt() - 1) ,PyroFireTime);
     Serial.print("   Firing Pyro ");Serial.print(Part2);Serial.println(" || ");
     return;
    }
    else
    {
// Parse out what pyros to fire then if every thing checks out and proper fire the pyros
     boolean error = false;
     int PyroNum;
     boolean Pyro[] = {false,false,false,false,false,false};
       
         
     Part1 = Part2;
     while (Part1.length() > 0)
     {
      if(Part1.indexOf(",")> 0)
      {
       Part2 = Part1.substring(0 , Part1.indexOf(","));
       Part1 = Part1.substring(Part1.indexOf(",")+1);
      }
      else
      {
       Part2 = Part1;
       Part1 = "";
      }
      PyroNum = Part2.toInt() - 1;
           
      if(PyroNum >= 0 && PyroNum <= 5)
      {
       Pyro[PyroNum] = true;
      }       
      else
      {
       PyroNum + 1;
       Serial.print("Firing Pyro > Pyro Num Error ");Serial.print(PyroNum);Serial.println(" ? ");
//Exit function due to a error       
        Part2 = "";
        return;
       }
      }

// If no errors then fire pyros
      if(error == false)
      {
       for(int x = 0;x <= 5;x++)
       {
        if(Pyro[x] == true)
        {
         FirePyro(x,PyroFireTime);
        }
       }
      }
     }
// Return to main loop
     return;
  }
// Undefined Command ==================================================================
  else
  {
    Serial.print("Command >> ");Serial.print(com);Serial.println(" << Not Reconginzed");
    return;
  }
 }

//======================================
boolean NewFileName(String Name)
{
 Serial.print("NewFileName Function");
 
 logfile = SD.open(Name, FILE_WRITE);
 if( ! logfile )
 {
  Serial.print("Couldnt create ");
  Serial.println(Name);
  return false;
 }
 else
 {
  Serial.print("Logging File ");
  Serial.println(filename);
  return true;     
 }
}

//==============================================
void DataStringGen()
{
// Generate data string

float SampleTime = float (millis() - SampleStartTime) / 1000.00000;

  Data_String =  Sample;Data_String += ",";
  Data_String += SampleTime;Data_String += ",";
  Data_String += GPS_Lat;Data_String += ",";
  Data_String += GPS_Lon;Data_String += ",";
  Data_String += Speed;Data_String += ",";
  Data_String += Alt;Data_String += ",";
  Data_String += G_X;Data_String += ",";
  Data_String += G_Y;Data_String += ",";
  Data_String += G_Z;Data_String += ",";
  for(int Z=0;Z<=5;Z++)
  {
    Data_String += digitalRead(PyroPin[Z]);
  }
  Data_String += ",";
  Data_String += Safety_Key;
}


void setup()
{

// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
  Serial.begin(115200);
  while (!Serial)
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
   Serial.println("SERA DataLogger Master Sketch");
     
// Assign Pyro Firing Pins int thisPin;
   for(int thisPin = 0; thisPin < 7; thisPin++)
   {
    pinMode(PyroPin[thisPin], OUTPUT);
   }

   Serial.println("Pyro Pin Assigned");
   
// see if the card is present and can be initialized:
   if (!SD.begin(cardSelect))
   {
     Serial.println("Card init. failed!");
     
   }

   strcpy(filename, "TSTLOG00.TXT");
   for (uint8_t i = 0; i < 100; i++)
   {
     filename[6] = '0' + i/10;
     filename[7] = '0' + i%10;
// create if does not exist, do not open existing, write, sync after write
     if (! SD.exists(filename))
     {
       break;
     }
   }

   NewFileName(filename);
   
   pinMode(BoardLED, OUTPUT);
   pinMode(SIM_LED, OUTPUT);
   
   Serial.println("Ready!");
   SampleStartTime = millis() + 5000;
}

    uint8_t i=0;
    String command;

    void loop()
    {
     
       while(Serial.available())
       {
        char c = Serial.read();
        if ( c == '\n')
       {
        ParseCommand(command);
        command = "";
       }
       else
       {
        command += c;
       }
      }
     
      ResetPyro();   // Check pyro pins to be turned off

      digitalWrite(BoardLED, LOW);
      for(int thisPin = 3; thisPin < 6; thisPin++)
      {
      digitalWrite(PyroPin[thisPin],HIGH);
      delay(250);
      digitalWrite(PyroPin[thisPin],LOW);
      }
      DataStringGen();
      digitalWrite(SIM_LED, HIGH);
      delay(200);
      logfile.println(Data_String);
      Serial.println(Data_String);
      digitalWrite(SIM_LED, LOW);
     
      digitalWrite(BoardLED, HIGH);
      delay(250);
      Sample ++;
    }

RocketNut
 
Posts: 103
Joined: Tue Jun 07, 2016 1:23 pm

Re: Setup Mystery

by franklin97355 on Wed Sep 28, 2016 5:02 pm

Take the for loop, add some print statements and see where it is failing then work on that. Simplify your code and get it working one part at a time.

franklin97355
 
Posts: 19437
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Setup Mystery

by RocketNut on Wed Sep 28, 2016 6:19 pm

When I started this project I started with a sketch that I found at https://learn.adafruit.com/adafruit-feather-m0-adalogger/using-the-sd-card. I just didn't brother changing the string until today.

If you compare the two you will find they have the same setup code, except the string. It works with example string \r\nAnalog logger test, an does not work with the string \r\nSERA DataLogger Master Sketch !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


This is the code that works

Setup error 1.jpg
Setup error 1.jpg (49.43 KiB) Viewed 119 times


Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include <SD.h>


// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// Set the pins used
#define cardSelect 4

File logfile;


// Define Pyro FiringPins & Control Arrays
const int PyroPin[] = {14,15,16,17,18,19};
int PyroStatus[] = {0,0,0,0,0,0}; //PyroFire pulse control
const int BoardLED = 13;
const int SIM_LED = 13;

// Sensor reading variables
float G_X;
float G_Y;
float G_Z;
float GPS_Lat;
float GPS_Lon;
float Speed;
float Alt;

// Define Status varables
boolean Safety_Key = true;
boolean Arm = false;
boolean USB_Cable = true;
boolean Logging = false;
boolean SIM_Card = true;


// Define misc varables
long int Sample = 1;
long int SampleStartTime;
String Data_String = "No Data";
int UserSketchStep = 1;
char filename[15];



// blink out an error code
void error(uint8_t errno) {
  while(1) {
  uint8_t i;
  for (i=0; i<errno; i++)
  {
   digitalWrite(BoardLED, HIGH);
   delay(100);
   digitalWrite(BoardLED, LOW);
   delay(100);
  }
  for (i=errno; i<10; i++)
  {
   delay(200);
  }
 }
}

//==============================================
void FirePyro(int PyroNum,int PyroFireTime)
{
 digitalWrite(PyroPin[PyroNum], HIGH);
 PyroStatus[PyroNum] = PyroFireTime;
}

//==============================================
 void ResetPyro()
 {
  for(int Z = 0 ; Z<= 5 ; Z++)
  {

   if(PyroStatus[Z] > 0 && PyroStatus[Z] <= millis())
   {
    digitalWrite(PyroPin[Z], LOW);
    PyroStatus[Z] = 0;
   }
  }
 }
     
    //==============================================
void ParseCommand(String com)
{
 String Part1;
 String Part2;
 int ComSeparator = com.indexOf('>'); // Check for command / data serperator
     
//Remove any spaces
  com.replace(" ", "");

// Command format "command(part1)>(delimiter)varables(part2)" Expample -> Start loging at 100 samples per second.
//     the comand string is "StartLoging>100"
//
// Check for command / data serperator
     
// Separate the command from the entried com string.
 Part1 = com.substring(0, com.indexOf(">"));
 Part2 = com.substring(com.indexOf(">")+1);

 //>>>> Debug Point
//      Serial.print("Command String || Part1 > ");Serial.print(Part1);
//     Serial.print(" || Part2 > ");Serial.print(Part2);Serial.print("(");Serial.print(Part2.length());Serial.print(")");
//      Serial.print(" || ComSeparator > ");Serial.println(com.indexOf('>'));
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


//==================================================
//==================================================
// Command FileName
//
 if(Part1.equalsIgnoreCase("filename"))
 {
  String Temp =Part2;

  Part1 = Temp.substring(0, Temp.indexOf("."));
  Part2 = Temp.substring(Temp.indexOf(".")+1);
  if(ComSeparator<1)
  {
   Serial.println("Command Missing '>' Command/Data Separator");
   return;
  }
  else if(Temp.length() == 0)
  {
   Serial.println("File Name Missing");
   return;
  }
  else if(Part1.length() > 8 || Part2.length() > 3)
  {
   Serial.println("Too Many Characters In Name or Extenrsion (Name Max 8 Char || Ext Max 3 Char)");
   return;
  }
   else if(!SIM_Card)
   {
    Serial.print("Error >> Check SIM Card ||");Serial.println(SIM_Card);
    return;
   }
   if (! SD.exists(Temp))
   {
    Serial.print("File ");Serial.print(Temp);Serial.println(" Already Exits");
   }
 }

//==================================================
//==================================================
// Command firepyro
//
  else if(Part1.equalsIgnoreCase("firepyro"))
  {
   if(ComSeparator<1)
   {
    Serial.println("Command Missing '>' Command/Data Separator");
    return;
   }

// Check Safety if not pulled then abort command
   if((Safety_Key = digitalRead(Safety_Key)))
   {
    Serial.println("Safety Key Inserted Can Not Fire Pyro(s)");
    return;
   }
   int PyroFireTime = millis() +1500;
   if(Part2.length() == 0)
   {
    Serial.println("Firing Pyro << Pyro Number Missing");
    return;
    }
    else if(Part2.length() == 1)
    {
     FirePyro((Part2.toInt() - 1) ,PyroFireTime);
     Serial.print("   Firing Pyro ");Serial.print(Part2);Serial.println(" || ");
     return;
    }
    else
    {
// Parse out what pyros to fire then if every thing checks out and proper fire the pyros
     boolean error = false;
     int PyroNum;
     boolean Pyro[] = {false,false,false,false,false,false};
       
         
     Part1 = Part2;
     while (Part1.length() > 0)
     {
      if(Part1.indexOf(",")> 0)
      {
       Part2 = Part1.substring(0 , Part1.indexOf(","));
       Part1 = Part1.substring(Part1.indexOf(",")+1);
      }
      else
      {
       Part2 = Part1;
       Part1 = "";
      }
      PyroNum = Part2.toInt() - 1;
           
      if(PyroNum >= 0 && PyroNum <= 5)
      {
       Pyro[PyroNum] = true;
      }       
      else
      {
       PyroNum + 1;
       Serial.print("Firing Pyro > Pyro Num Error ");Serial.print(PyroNum);Serial.println(" ? ");
//Exit function due to a error       
        Part2 = "";
        return;
       }
      }

// If no errors then fire pyros
      if(error == false)
      {
       for(int x = 0;x <= 5;x++)
       {
        if(Pyro[x] == true)
        {
         FirePyro(x,PyroFireTime);
        }
       }
      }
     }
// Return to main loop
     return;
}
    // Undefined Command ==================================================================
     else
     {
        Serial.print("Command >> ");Serial.print(com);Serial.println(" << Not Reconginzed");
        return;
     }
    }

    // This line is not needed if you have Adafruit SAMD board package 1.6.2+
    //   #define Serial SerialUSB

//==============================================
void DataStringGen()
{
// Generate data string

float SampleTime = float (millis() - SampleStartTime) / 1000.00000;

  Data_String =  Sample;Data_String += ",";
  Data_String += SampleTime;Data_String += ",";
  Data_String += GPS_Lat;Data_String += ",";
  Data_String += GPS_Lon;Data_String += ",";
  Data_String += Speed;Data_String += ",";
  Data_String += Alt;Data_String += ",";
  Data_String += G_X;Data_String += ",";
  Data_String += G_Y;Data_String += ",";
  Data_String += G_Z;Data_String += ",";
  for(int Z=0;Z<=5;Z++)
  {
    Data_String += digitalRead(PyroPin[Z]);
  }
  Data_String += ",";
  Data_String += Safety_Key;
}


void setup()
{

// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
  Serial.begin(115200);
  while (!Serial)
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
  Serial.println("\r\nAnalog logger test");
     
// Assign Pyro Firing Pins int thisPin;
   for(int thisPin = 0; thisPin < 7; thisPin++)
   {
    pinMode(PyroPin[thisPin], OUTPUT);
   }
// see if the card is present and can be initialized:
   if (!SD.begin(cardSelect))
   {
     Serial.println("Card init. failed!");
     error(2);
   }

   strcpy(filename, "TSTLOG00.TXT");
   for (uint8_t i = 0; i < 100; i++)
   {
     filename[6] = '0' + i/10;
     filename[7] = '0' + i%10;
// create if does not exist, do not open existing, write, sync after write
     if (! SD.exists(filename))
     {
       break;
     }
   }

   logfile = SD.open(filename, FILE_WRITE);
   if( ! logfile )
   {
     Serial.print("Couldnt create ");
     Serial.println(filename);
     error(3);
   }
   Serial.print("Writing to ");
   Serial.println(filename);

   pinMode(BoardLED, OUTPUT);
   pinMode(SIM_LED, OUTPUT);
   Serial.println("Ready!");
   SampleStartTime = millis() + 5000;
}

    uint8_t i=0;
    String command;

    void loop()
    {
     
       while(Serial.available())
       {
        char c = Serial.read();
        if ( c == '\n')
       {
        ParseCommand(command);
        command = "";
       }
       else
       {
        command += c;
       }
      }
     
      ResetPyro();   // Check pyro pins to be turned off

      digitalWrite(BoardLED, LOW);
      for(int thisPin = 0; thisPin < 6; thisPin++)
      {
      digitalWrite(PyroPin[thisPin],HIGH);
      delay(250);
      digitalWrite(PyroPin[thisPin],LOW);
      }
      DataStringGen();
      digitalWrite(SIM_LED, HIGH);
      delay(200);
      logfile.println(Data_String);
      Serial.println(Data_String);
      digitalWrite(SIM_LED, LOW);
     
      digitalWrite(BoardLED, HIGH);
      delay(250);
      Sample ++;
    }



This code does not work

Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include <SD.h>


// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// Set the pins used
#define cardSelect 4

File logfile;


// Define Pyro FiringPins & Control Arrays
const int PyroPin[] = {14,15,16,17,18,19};
int PyroStatus[] = {0,0,0,0,0,0}; //PyroFire pulse control
const int BoardLED = 13;
const int SIM_LED = 13;

// Sensor reading variables
float G_X;
float G_Y;
float G_Z;
float GPS_Lat;
float GPS_Lon;
float Speed;
float Alt;

// Define Status varables
boolean Safety_Key = true;
boolean Arm = false;
boolean USB_Cable = true;
boolean Logging = false;
boolean SIM_Card = true;


// Define misc varables
long int Sample = 1;
long int SampleStartTime;
String Data_String = "No Data";
int UserSketchStep = 1;
char filename[15];




//==============================================
void FirePyro(int PyroNum,int PyroFireTime)
{
 digitalWrite(PyroPin[PyroNum], HIGH);
 PyroStatus[PyroNum] = PyroFireTime;
}

//==============================================
 void ResetPyro()
 {
  for(int Z = 0 ; Z<= 5 ; Z++)
  {

   if(PyroStatus[Z] > 0 && PyroStatus[Z] <= millis())
   {
    digitalWrite(PyroPin[Z], LOW);
    PyroStatus[Z] = 0;
   }
  }
 }
     
    //==============================================
void ParseCommand(String com)
{

 // Command format "command(part1)>(delimiter)varables(part2)" Expample -> Start loging at 100 samples per second.
//     the comand string is "StartLoging>100"
//

 String Part1;
 String Part2;

 // Check for command / data serperator
 int ComSeparator = com.indexOf('>'); // Check for command / data serperator
     
//Remove any spaces
  com.replace(" ", "");
     
// Separate the command from the entried com string.
 Part1 = com.substring(0, com.indexOf(">"));
 Part2 = com.substring(com.indexOf(">")+1);

//>>>> Debug Point
//      Serial.print("Command String || Part1 > ");Serial.print(Part1);
//     Serial.print(" || Part2 > ");Serial.print(Part2);Serial.print("(");Serial.print(Part2.length());Serial.print(")");
//      Serial.print(" || ComSeparator > ");Serial.println(com.indexOf('>'));
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


//==================================================
//==================================================
// Command FileName
//
 if(Part1.equalsIgnoreCase("filename"))
 {
  String Temp =Part2;

  Part1 = Temp.substring(0, Temp.indexOf("."));
  Part2 = Temp.substring(Temp.indexOf(".")+1);
  if(ComSeparator<0)
  {
   Serial.println("Command Missing '>' Command/Data Separator");
   return;
  }
  else if(Temp.length() == 0)
  {
   Serial.println("File Name Missing");
   return;
  }
  else if(Part1.length() > 8 || Part2.length() > 3)
  {
   Serial.println("Too Many Characters In Name or Extenrsion (Name Max 8 Char || Ext Max 3 Char)");
   return;
  }
  else if(!SIM_Card)
  {
    Serial.print("Error >> Check SIM Card ||");Serial.println(SIM_Card);
    return;
  }

// SIM is ready. Let's check to see if the file already exists on the SIM Card 
  if (SD.exists(Temp))
  {
    Serial.print(Temp);Serial.println(" Already Exits");
    do           // Wait for answer then check if it's valed
    {
     Serial.print("Over Write The Old File Y/N/ESC > ");
     while(!Serial.available())
     {
     }
     char c = Serial.read();
     if(c == 'N' || c == 'n' || c == 27)
     {
      if(c != 27)
      {
       Serial.println("No");
      }
      else
      {
       Serial.println("");
      }
       return;
     }
     else if(c == 'Y' || c == 'y' || c == 27)
     {
      Serial.println("");
      SD.remove(Temp);
      NewFileName(Temp);
      return;
     }
     else
     {
      Serial.println("??");
     }
    } while(1); // Bad char was entered so redo the question.
   }
   else
   {
    NewFileName(Temp);
    return;
   }
  }
//==================================================
//==================================================
// Command firepyro
//
 else if(Part1.equalsIgnoreCase("firepyro"))
  {
   if(ComSeparator<0)
   {
    Serial.println("Command Missing '>' Command/Data Separator");
    return;
   }

// Check Safety if not pulled then abort command
   if((Safety_Key = digitalRead(Safety_Key)))
   {
    Serial.println("Safety Key Inserted Can Not Fire Pyro(s)");
    return;
   }
   int PyroFireTime = millis() +1500;
   if(Part2.length() == 0)
   {
    Serial.println("Firing Pyro << Pyro Number Missing");
    return;
    }
    else if(Part2.length() == 1)
    {
     FirePyro((Part2.toInt() - 1) ,PyroFireTime);
     Serial.print("   Firing Pyro ");Serial.print(Part2);Serial.println(" || ");
     return;
    }
    else
    {
// Parse out what pyros to fire then if every thing checks out and proper fire the pyros
     boolean error = false;
     int PyroNum;
     boolean Pyro[] = {false,false,false,false,false,false};
       
         
     Part1 = Part2;
     while (Part1.length() > 0)
     {
      if(Part1.indexOf(",")> 0)
      {
       Part2 = Part1.substring(0 , Part1.indexOf(","));
       Part1 = Part1.substring(Part1.indexOf(",")+1);
      }
      else
      {
       Part2 = Part1;
       Part1 = "";
      }
      PyroNum = Part2.toInt() - 1;
           
      if(PyroNum >= 0 && PyroNum <= 5)
      {
       Pyro[PyroNum] = true;
      }       
      else
      {
       PyroNum + 1;
       Serial.print("Firing Pyro > Pyro Num Error ");Serial.print(PyroNum);Serial.println(" ? ");
//Exit function due to a error       
        Part2 = "";
        return;
       }
      }

// If no errors then fire pyros
      if(error == false)
      {
       for(int x = 0;x <= 5;x++)
       {
        if(Pyro[x] == true)
        {
         FirePyro(x,PyroFireTime);
        }
       }
      }
     }
// Return to main loop
     return;
  }
// Undefined Command ==================================================================
  else
  {
    Serial.print("Command >> ");Serial.print(com);Serial.println(" << Not Reconginzed");
    return;
  }
 }

//======================================
boolean NewFileName(String Name)
{
 Serial.print("NewFileName Function");
 
 logfile = SD.open(Name, FILE_WRITE);
 if( ! logfile )
 {
  Serial.print("Couldnt create ");
  Serial.println(Name);
  return false;
 }
 else
 {
  Serial.print("Logging File ");
  Serial.println(filename);
  return true;     
 }
}

//==============================================
void DataStringGen()
{
// Generate data string

float SampleTime = float (millis() - SampleStartTime) / 1000.00000;

  Data_String =  Sample;Data_String += ",";
  Data_String += SampleTime;Data_String += ",";
  Data_String += GPS_Lat;Data_String += ",";
  Data_String += GPS_Lon;Data_String += ",";
  Data_String += Speed;Data_String += ",";
  Data_String += Alt;Data_String += ",";
  Data_String += G_X;Data_String += ",";
  Data_String += G_Y;Data_String += ",";
  Data_String += G_Z;Data_String += ",";
  for(int Z=0;Z<=5;Z++)
  {
    Data_String += digitalRead(PyroPin[Z]);
  }
  Data_String += ",";
  Data_String += Safety_Key;
}


void setup()
{

// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
  Serial.begin(115200);
  while (!Serial)
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
   Serial.println("\r\nSERA DataLogger Master Sketch");
     
// Assign Pyro Firing Pins int thisPin;
   for(int thisPin = 0; thisPin < 7; thisPin++)
   {
    pinMode(PyroPin[thisPin], OUTPUT);
   }
   
// see if the card is present and can be initialized:
   if (!SD.begin(cardSelect))
   {
     Serial.println("Card init. failed!");
     
   }

   strcpy(filename, "TSTLOG00.TXT");
   for (uint8_t i = 0; i < 100; i++)
   {
     filename[6] = '0' + i/10;
     filename[7] = '0' + i%10;
// create if does not exist, do not open existing, write, sync after write
     if (! SD.exists(filename))
     {
       break;
     }
   }

  logfile = SD.open(filename, FILE_WRITE);
   if( ! logfile )
   {
     Serial.print("Couldnt create ");
     Serial.println(filename);
   }
   Serial.print("Writing to ");
   Serial.println(filename);
   
   pinMode(BoardLED, OUTPUT);
   pinMode(SIM_LED, OUTPUT);
   
   Serial.println("Ready!");
   SampleStartTime = millis() + 5000;
}

    uint8_t i=0;
    String command;

    void loop()
    {
     
       while(Serial.available())
       {
        char c = Serial.read();
        if ( c == '\n')
       {
        ParseCommand(command);
        command = "";
       }
       else
       {
        command += c;
       }
      }
     
      ResetPyro();   // Check pyro pins to be turned off

      digitalWrite(BoardLED, LOW);
      for(int thisPin = 3; thisPin < 6; thisPin++)
      {
      digitalWrite(PyroPin[thisPin],HIGH);
      delay(250);
      digitalWrite(PyroPin[thisPin],LOW);
      }
      DataStringGen();
      digitalWrite(SIM_LED, HIGH);
      delay(200);
      logfile.println(Data_String);
      Serial.println(Data_String);
      digitalWrite(SIM_LED, LOW);
     
      digitalWrite(BoardLED, HIGH);
      delay(250);
      Sample ++;
    }

RocketNut
 
Posts: 103
Joined: Tue Jun 07, 2016 1:23 pm

Please be positive and constructive with your questions and comments.