Code "soft bricks" my board

I am trying to program my Adafruit Feather M0 RFM96 LoRa Radio - 433MHz - RadioFruit with this code. It compiles and uploads but when it's done uploading, the board doesn't do anything it's supposed to and stops being recognized by the computer. I am able to get the board back by uploading a blink code with verbose upload but my below code doesn't ever work on my board. I have seen many posts about how to get my board working again but not much on why some codes "break" the boards for a bit. This code is a "Frankenstein" of some other codes, all of which work, but when I try to duct tape it all together, it never comes alive. I'm trying to data log, transmit location through LORA and move a servo when the time comes. I'm wondering what it is about this code that my MCU doesn't like.

#include "Adafruit_APDS9960.h"
#include <Adafruit_BMP280.h>
#include <Adafruit_LIS3MDL.h>
#include <Adafruit_LSM6DS33.h>
#include <Adafruit_GPS.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_SHT31.h>
#include <RH_RF95.h>
#include "RTClib.h"
#include "avr/dtostrf.h"
#include <SPI.h>
#include <SD.h>
#include <Servo.h>
#include <Wire.h>
#define GPSSerial Serial1
#define GPSECHO false
Adafruit_GPS GPS(&GPSSerial);
Servo servo;
RTC_PCF8523 rtc;
const int chipSelect = 10;

Adafruit_APDS9960 apds; // proximity, light, color, gesture
Adafruit_LSM6DS33 lsm6ds33; // accelerometer, gyroscope
Adafruit_LIS3MDL lis3mdl;   // magnetometer
Adafruit_SHT31 sht30;  // humidity
Adafruit_BMP280 bmp; // use I2C interface

#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 3

#define RF95_FREQ 433.0

// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);

float temperature, pressure, altitude, speed;
float magnetic_x, magnetic_y, magnetic_z;
float accel_x, accel_y, accel_z;
float gyro_x, gyro_y, gyro_z;
int MaxAlt = 0;
int MaxSpd = 0;
int MaxTem = 0;
int MinTem = 0;
float Lo = GPS.longitude;
float La = GPS.latitude;
int Al = GPS.altitude;
int Sp = GPS.speed;
int humidity = sht30.readHumidity();

void setup() {
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed");
  }

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
  }

  if (! rtc.initialized() || rtc.lostPower()) {
    Serial.println("RTC is NOT initialized, let's set the time!");
  }
  if(!apds.begin()){
    Serial.println("failed to initialize device! Please check your wiring.");
  }
  apds.begin();
  apds.enableColor(true);  
  sht30.begin();
  servo.attach(5);
  servo.write(90);
  bmp.begin();
  lis3mdl.begin_I2C();
  lsm6ds33.begin_I2C();
  rtc.start();  
  SD.begin(chipSelect);

  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
                Adafruit_BMP280::SAMPLING_X2,
                Adafruit_BMP280::SAMPLING_X16,
                Adafruit_BMP280::FILTER_X16,
                Adafruit_BMP280::STANDBY_MS_500);
  pinMode(RFM95_RST, OUTPUT);
  digitalWrite(RFM95_RST, HIGH);

  Serial.begin(115200);
  GPS.begin(9600);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
  delay(1000);
  GPSSerial.println(PMTK_Q_RELEASE);

  // manual reset
  digitalWrite(RFM95_RST, LOW);
  delay(10);
  digitalWrite(RFM95_RST, HIGH);
  delay(10);

  while (!rf95.init()) {
    while (1);
  }

  if (!rf95.setFrequency(RF95_FREQ)) {
    while (1);
  }
 
  rf95.setTxPower(23, false);
}

void loop(){

DateTime now = rtc.now();
lis3mdl.read();
sensors_event_t temp_event, pressure_event; 
sensors_event_t accel;
sensors_event_t gyro;
sensors_event_t temp;
lsm6ds33.getEvent(&accel, &gyro, &temp);

delay(1000);  

char c = GPS.read();
  if (GPS.newNMEAreceived()) {
    Serial.println(GPS.lastNMEA());
    if (!GPS.parse(GPS.lastNMEA()))
      return;
  }
    if (GPS.fix) {
      Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
      Serial.print(", ");
      Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
      Serial.print(GPS.speed); Serial.println("kn");
      Serial.print(GPS.altitude); Serial.println("m");
  }

  altitude = GPS.altitude;
  if (altitude > MaxAlt) {MaxAlt = altitude; }
  speed = GPS.speed;
  if (speed > MaxSpd) {MaxAlt = speed; }  
  temperature = bmp.readTemperature();
  if (temperature > MaxTem) {MaxTem = temperature; } 
  temperature = bmp.readTemperature();
  if (temperature < MinTem) {MinTem = temperature; }   
  
  delay(1000);
  char radiopacket[2] = "N";
  Serial.println(radiopacket);
  radiopacket[1] = 0;
  rf95.send((uint8_t *)radiopacket, 2);
  rf95.waitPacketSent();
  
  delay(1000);
  char Lat[1] = {};
  dtostrf(La, 9, 4, Lat);
  Serial.print("Sending "); Serial.println(Lat);
  rf95.send((uint8_t *) &Lat,9);
  delay(10);
  rf95.waitPacketSent();

  delay(1000);
  char Lon[1] = {};
  dtostrf(Lo, 9, 4, Lon);
  Serial.print("Sending "); Serial.println(Lon);
  rf95.send((uint8_t *) &Lon,9);
  delay(10);
  rf95.waitPacketSent();
  
  delay(1000);
  char Spd[1] = {};
  dtostrf(Sp, 3, 0, Spd);
  Serial.print("Sending "); Serial.println(Spd);
  rf95.send((uint8_t *) &Spd,3);
  delay(10);
  rf95.waitPacketSent();

  delay(1000);
  char Alt[1] = {};
  dtostrf(Al, 6, 0, Alt);
  Serial.print("Sending "); Serial.println(Alt);
  rf95.send((uint8_t *) &Alt,6);
  delay(10);
  rf95.waitPacketSent();

  String dataString = "";
 
  dataString += String(int(now.month())) += "/";
  dataString += String(int(now.day())) += "/";
  dataString += String(int(now.year())) += " ";
  dataString += String(int(now.hour())) += ":";
  dataString += String(int(now.minute())) += ":";
  dataString += String(int(now.second())) += " ";
  dataString += String(GPS.latitude, 4);
  dataString += String(GPS.lat) += ", ";
  dataString += String(GPS.longitude, 4);
  dataString += String(GPS.lon) += " ";
  dataString += String(bmp.readTemperature()) += " C Min:";
  dataString += String(MinTem) += " C Max:";
  dataString += String(MaxTem) += " C Humidity:";    
  dataString += String(sht30.readHumidity()) += "% ";
  dataString += String(bmp.readPressure()) += " Pa ";
  dataString += String(GPS.speed) += " kn ";
  dataString += String(MaxSpd) += " kn MAX ";
  dataString += String(GPS.altitude) += " m ";
  dataString += String(MaxAlt) += " m MAX MagX:";
  dataString += String(lis3mdl.x) += " MagY:";
  dataString += String(lis3mdl.y) += " MagZ:";
  dataString += String(lis3mdl.z) += " uTesla Acc x:";
  dataString += String(accel.acceleration.x) += " Acc y:";
  dataString += String(accel.acceleration.y) += " Acc z:";
  dataString += String(accel.acceleration.z) += " m/s^2 Gyro x:";
  dataString += String(gyro.gyro.x) += " Gyro y:";
  dataString += String(gyro.gyro.y) += " Gyro z:";
  dataString += String(gyro.gyro.z) += " dps Proximity:";
  dataString += String(apds.readProximity());

/*  File dataFile = SD.open("datalog.txt", FILE_WRITE); //Opens new text file on SD card called datalog.txt

  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
  } */

  Serial.println(dataString);

  if (GPS.altitude < altitude && MaxAlt > 4000 && GPS.altitude < 3000) {
    servo.write(15); // moves servo to 70 degrees
  }
}

Hi,
I'm not familiar with the ADAFRUIT FEATHER M0 RFM96 LORA RADIO - 433MHZ.

Do you get anything on the serial monitor screen?
Have you got.

Serial.begin(115200);

To begin the serial comms?
Sorry I see it, but it should be one of the first statements in the void setup() function, not near the end AFTER all your Serial.print commands.

Tom... :grinning: :+1: :coffee: :australia:

The if statements are executing the .begin() functions of the libraries, you should not call those a second time.

I would write the

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");

as the first command in setup()
and then add Serial.println() along
all the following function-calls to see how long the code gets executed

And as a personal standard I always add a HeartBeatBlinker blinking the onboard-LED in loop. So I have a visual feedback if the code is running.

void PrintFileNameDateTime() {
  Serial.println("Code running comes from file ");
  Serial.println(__FILE__);
  Serial.print("  compiled ");
  Serial.print(__DATE__);
  Serial.print(" ");
  Serial.println(__TIME__);  
}


boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - periodStartTime >= TimePeriod )
  {
    periodStartTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}

unsigned long MyTestTimer = 0;                   // variables MUST be of type unsigned long
const byte    OnBoard_LED = 2;


void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 
  }
}



void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();

}


void loop() {
  BlinkHeartBeatLED(OnBoard_LED,100);

best regards Stefan

Hi,

Same here, it can tell you a lot about how your code is running.

Tom... :grinning: :+1: :coffee: :australia:

Tom I am getting nothing on the serial monitor screen. The code compiles and uploads, then the board disconnects and does not reconnect. It then doesn't show up on the "ports" list and Arduino states, "Error opening serial port 'COM24'. I moved the "Serial.begin(115200)" to the start of the void setup() function. I also removed the if statements between the setup and the loop. Here is the new code. It behaves the same as the old code. Since I get no serial monitor, I have not tried the HeartBeatBlinker yet

#include "Adafruit_APDS9960.h"
#include <Adafruit_BMP280.h>
#include <Adafruit_LIS3MDL.h>
#include <Adafruit_LSM6DS33.h>
#include <Adafruit_GPS.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_SHT31.h>
#include <RH_RF95.h>
#include "RTClib.h"
#include "avr/dtostrf.h"
#include <SPI.h>
#include <SD.h>
#include <Servo.h>
#include <Wire.h>
#define GPSSerial Serial1
#define GPSECHO false
Adafruit_GPS GPS(&GPSSerial);
Servo servo;
RTC_PCF8523 rtc;
const int chipSelect = 10;

Adafruit_APDS9960 apds; // proximity, light, color, gesture
Adafruit_LSM6DS33 lsm6ds33; // accelerometer, gyroscope
Adafruit_LIS3MDL lis3mdl;   // magnetometer
Adafruit_SHT31 sht30;  // humidity
Adafruit_BMP280 bmp; // use I2C interface

#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 3

#define RF95_FREQ 433.0

// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);

float temperature, pressure, altitude, speed;
float magnetic_x, magnetic_y, magnetic_z;
float accel_x, accel_y, accel_z;
float gyro_x, gyro_y, gyro_z;
int MaxAlt = 0;
int MaxSpd = 0;
int MaxTem = 0;
int MinTem = 0;
float Lo = GPS.longitude;
float La = GPS.latitude;
int Al = GPS.altitude;
int Sp = GPS.speed;
int humidity = sht30.readHumidity();

void setup() {
  Serial.begin(115200);
  apds.begin();
  apds.enableColor(true);  
  sht30.begin();
  servo.attach(5);
  servo.write(90);
  bmp.begin();
  lis3mdl.begin_I2C();
  lsm6ds33.begin_I2C();
  rtc.start();  
  SD.begin(chipSelect);

  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
                Adafruit_BMP280::SAMPLING_X2,
                Adafruit_BMP280::SAMPLING_X16,
                Adafruit_BMP280::FILTER_X16,
                Adafruit_BMP280::STANDBY_MS_500);
  pinMode(RFM95_RST, OUTPUT);
  digitalWrite(RFM95_RST, HIGH);

  GPS.begin(9600);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
  delay(1000);
  GPSSerial.println(PMTK_Q_RELEASE);

  digitalWrite(RFM95_RST, LOW);
  delay(10);
  digitalWrite(RFM95_RST, HIGH);
  delay(10);
 
  rf95.setTxPower(23, false);
}

void loop(){

DateTime now = rtc.now();
lis3mdl.read();
sensors_event_t temp_event, pressure_event; 
sensors_event_t accel;
sensors_event_t gyro;
sensors_event_t temp;
lsm6ds33.getEvent(&accel, &gyro, &temp);

delay(1000);  

char c = GPS.read();
  if (GPS.newNMEAreceived()) {
    Serial.println(GPS.lastNMEA());
    if (!GPS.parse(GPS.lastNMEA()))
      return;
  }
    if (GPS.fix) {
      Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
      Serial.print(", ");
      Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
      Serial.print(GPS.speed); Serial.println("kn");
      Serial.print(GPS.altitude); Serial.println("m");
  }

  altitude = GPS.altitude;
  if (altitude > MaxAlt) {MaxAlt = altitude; }
  speed = GPS.speed;
  if (speed > MaxSpd) {MaxAlt = speed; }  
  temperature = bmp.readTemperature();
  if (temperature > MaxTem) {MaxTem = temperature; } 
  temperature = bmp.readTemperature();
  if (temperature < MinTem) {MinTem = temperature; }   
  
  delay(1000);
  char radiopacket[2] = "N";
  Serial.println(radiopacket);
  radiopacket[1] = 0;
  rf95.send((uint8_t *)radiopacket, 2);
  rf95.waitPacketSent();
  
  delay(1000);
  char Lat[1] = {};
  dtostrf(La, 9, 4, Lat);
  Serial.print("Sending "); Serial.println(Lat);
  rf95.send((uint8_t *) &Lat,9);
  delay(10);
  rf95.waitPacketSent();

  delay(1000);
  char Lon[1] = {};
  dtostrf(Lo, 9, 4, Lon);
  Serial.print("Sending "); Serial.println(Lon);
  rf95.send((uint8_t *) &Lon,9);
  delay(10);
  rf95.waitPacketSent();
  
  delay(1000);
  char Spd[1] = {};
  dtostrf(Sp, 3, 0, Spd);
  Serial.print("Sending "); Serial.println(Spd);
  rf95.send((uint8_t *) &Spd,3);
  delay(10);
  rf95.waitPacketSent();

  delay(1000);
  char Alt[1] = {};
  dtostrf(Al, 6, 0, Alt);
  Serial.print("Sending "); Serial.println(Alt);
  rf95.send((uint8_t *) &Alt,6);
  delay(10);
  rf95.waitPacketSent();

  String dataString = "";
 
  dataString += String(int(now.month())) += "/";
  dataString += String(int(now.day())) += "/";
  dataString += String(int(now.year())) += " ";
  dataString += String(int(now.hour())) += ":";
  dataString += String(int(now.minute())) += ":";
  dataString += String(int(now.second())) += " ";
  dataString += String(GPS.latitude, 4);
  dataString += String(GPS.lat) += ", ";
  dataString += String(GPS.longitude, 4);
  dataString += String(GPS.lon) += " ";
  dataString += String(bmp.readTemperature()) += " C Min:";
  dataString += String(MinTem) += " C Max:";
  dataString += String(MaxTem) += " C Humidity:";    
  dataString += String(sht30.readHumidity()) += "% ";
  dataString += String(bmp.readPressure()) += " Pa ";
  dataString += String(GPS.speed) += " kn ";
  dataString += String(MaxSpd) += " kn MAX ";
  dataString += String(GPS.altitude) += " m ";
  dataString += String(MaxAlt) += " m MAX MagX:";
  dataString += String(lis3mdl.x) += " MagY:";
  dataString += String(lis3mdl.y) += " MagZ:";
  dataString += String(lis3mdl.z) += " uTesla Acc x:";
  dataString += String(accel.acceleration.x) += " Acc y:";
  dataString += String(accel.acceleration.y) += " Acc z:";
  dataString += String(accel.acceleration.z) += " m/s^2 Gyro x:";
  dataString += String(gyro.gyro.x) += " Gyro y:";
  dataString += String(gyro.gyro.y) += " Gyro z:";
  dataString += String(gyro.gyro.z) += " dps Proximity:";
  dataString += String(apds.readProximity());

/*  File dataFile = SD.open("datalog.txt", FILE_WRITE); //Opens new text file on SD card called datalog.txt

  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
  } */

  Serial.println(dataString);

  if (GPS.altitude < altitude && MaxAlt > 4000 && GPS.altitude < 3000) {
    servo.write(15); // moves servo to 70 degrees
  }
}

You should add not only serial.begin but additional

Serial.println("Setup-Start");

  Serial.begin(115200);
  Serial.println("Setup-Start");

and then more
serial.println() after every single command

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start"););

  apds.begin();
  Serial.println("ads begin done"););
  apds.enableColor(true);  

  sht30.begin();
  Serial.println("sht30 begin done"););
  
  servo.attach(5);
  Serial.println("servo.attach(5) done"););

  servo.write(90);

I thought this is obvious

best regards Stefan

Hi,
Try with this edit of your setup().

void setup() {
  Serial.begin(115200);
  Serial.println("SETUP INITIALISATION BEGINS");  // Print start of setup.
  apds.begin();
  apds.enableColor(true);  
  sht30.begin();
  servo.attach(5);
  servo.write(90);
  bmp.begin();
  lis3mdl.begin_I2C();
  lsm6ds33.begin_I2C();
  rtc.start();  
  SD.begin(chipSelect);

  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
                Adafruit_BMP280::SAMPLING_X2,
                Adafruit_BMP280::SAMPLING_X16,
                Adafruit_BMP280::FILTER_X16,
                Adafruit_BMP280::STANDBY_MS_500);
  pinMode(RFM95_RST, OUTPUT);
  digitalWrite(RFM95_RST, HIGH);

  GPS.begin(9600);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
  delay(1000);
  GPSSerial.println(PMTK_Q_RELEASE);

  digitalWrite(RFM95_RST, LOW);
  delay(10);
  digitalWrite(RFM95_RST, HIGH);
  delay(10);
 
  rf95.setTxPower(23, false);
 Serial.println("SETUP INTIALISATION COMPLETE"); // Print finish of setup.
delay(1000);  //pause for 1second to see monitor prints.
}

It adds some serial monitor data as the setup starts and finishes, to help establishing if your code gets this far.
Make sure your serial monitor is set for 115200 baud.

Tom... :grinning: :+1: :coffee: :australia:

The serial monitor doesn't every show up because after the code uploads, the board disconnects and doesn't reconnect. The board does not get recognized by my computer or a secondary computer. When I unplug the board, there is no "disconnect" sound from my computer and when I plug it back in, the board lights up but the computer does not recognize a device has been plugged back in. So upon uploading the code, the serial monitor will not even show up.

Hi,
Have you had other code or this code successfully running on this board before?
Have you tried to run a bit of example code from the IDE or some code you know does work?

Tom... :grinning: :+1: :coffee: :australia:

Yes, whenever I "brick" this board with my desired code, I upload a simple blink program to get it back to normal. The board works as expected with other code.

Hi

Did you write your code in stages?
I hope so...
If you did, where did this problem occur?
What did you add or change that got this to occur?
You should have some copies of your code in various stages that have worked.
Have you gone back and checked to see if they still work?

Tom... :grinning: :+1: :coffee: :australia:
PS, I'm off to bed.. very late here.. :sleeping: :sleeping: :sleeping: :sleeping: :sleeping:

Please give the code-version that has Serial.print right behind
serial.begin a try.
I highly doubt that your code does upload and then immediately crashed even before the four commands

  Serial.begin(115200);
  Serial.println("Setup-Start"););

  apds.begin();
  Serial.println("ads begin done"););

are beeing executed.

If it does crash even before

void setup() {
  Serial.begin(115200);

the reasons for crashing are completely DIFFERENT than crashing later

and even then for finding the real reason it is an important information that the crash occurs even befor

void setup() {
  Serial.begin(115200);

best regards Stefan

Put a line with Serial.flush(); after each Serial.print() in setup, that will allow the serial data to transmit before the code crashes.

I tried

Serial.begin(115200);
Serial.println("Setup-Start");
Serial.flush();

apds.begin();
Serial.println("ads begin done");
Serial.flush();

And

Serial.begin(115200);
Serial.println("Setup-Start");

apds.begin();
Serial.println("ads begin done");

I had the Serial monitor open both times and both times I got the same result. The monitor went grey as it does when uploading, and stayed grey. Nothing printed either time and Arduino said, "Error opening serial port 'COM24' as well as the full error report which is here

Sketch uses 82944 bytes (31%) of program storage space. Maximum is 262144 bytes.
Atmel SMART device 0x10010005 found
Device : ATSAMD21G18A
Chip ID : 10010005
Version : v2.0 [Arduino:XYZ] Mar 5 2016 17:46:52
Address : 8192
Pages : 3968
Page Size : 64 bytes
Total Size : 248KB
Planes : 1
Lock Regions : 16
Locked : none
Security : false
Boot Flash : true
BOD : true
BOR : true
Arduino : FAST_CHIP_ERASE
Arduino : FAST_MULTI_PAGE_WRITE
Arduino : CAN_CHECKSUM_MEMORY_BUFFER
Erase flash
done in 0.913 seconds

Write 84056 bytes to flash (1314 pages)
[==============================] 100% (1314/1314 pages)
done in 0.653 seconds

Verify 84056 bytes of flash with checksum.
Verify successful
done in 0.056 seconds
CPU reset.
processing.app.SerialException: Error opening serial port 'COM24'.
at processing.app.Serial.(Serial.java:152)
at processing.app.Serial.(Serial.java:82)
at processing.app.SerialMonitor$2.(SerialMonitor.java:132)
at processing.app.SerialMonitor.open(SerialMonitor.java:132)
at processing.app.AbstractMonitor.resume(AbstractMonitor.java:132)
at processing.app.Editor.resumeOrCloseSerialMonitor(Editor.java:2120)
at processing.app.Editor.access$1300(Editor.java:117)
at processing.app.Editor$UploadHandler.run(Editor.java:2089)
at java.lang.Thread.run(Thread.java:748)
Caused by: jssc.SerialPortException: Port name - COM24; Method name - openPort(); Exception type - Port not found.
at jssc.SerialPort.openPort(SerialPort.java:167)
at processing.app.Serial.(Serial.java:141)
... 8 more
Error opening serial port 'COM24'.

Aha. Hm .... Mr. spock would say

The messages claim uploading was successful but the serial-monitor can't be opened.
What does the device-manager of your computer say?
Is Com24 listed in the devicemanager?
If you close the serial monitor and re-open the serial-monitor and then press the reset-button of the device what does happen?

close the serial-monitor of the Arduino-IDE.
unplug and re-plug the board
Open some other serial terminal-program on the comport shown in the device-manager

does this other serial terminal show anything received?

best regards Stefan

These are all constructors running before setup starts. Try to comment them out one after the other to see when you get an output at the serial monitor. And you might see which library is the cause for your problems.
Regards, Franz-Peter

HI,
What controller are you using?
What version of the IDE?
What OS?

Tom.... :grinning::+1: :coffee: :australia:

Not sure how the compiler is going to handle initializing global variables using the GPS and sht30 before the begin() functions are executed, you might want to declare the variables and do the initialization in setup.

IT'S ALLIIIIVVVEEEE!!!!! I "re-Frankenstein'd" it, moving methodically and systematically, fixing any errors along the way. I was able to get rid of some variables, some "BMP" lines and didn't use the "Adafruit_Sensor.h" library at all. I have not really looked back and compared the two codes yet but this one works as expected.

#include <SPI.h>
#include <Wire.h>
#include <RH_RF95.h>
#include "Adafruit_APDS9960.h"
#include <Adafruit_GPS.h>
#include <Adafruit_SHT31.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_LIS3MDL.h>
#include <Adafruit_LSM6DS33.h>
#include "RTClib.h"
#include "avr/dtostrf.h"
#define GPSSerial Serial1
#include <Servo.h>
RTC_PCF8523 rtc;
#include <SD.h>
Adafruit_GPS GPS(&GPSSerial);
#define GPSECHO false
Servo servo;

Adafruit_SHT31 sht30;  // humidity
Adafruit_BMP280 bmp280; // use I2C interface
Adafruit_LIS3MDL lis3mdl;   // magnetometer
Adafruit_LSM6DS33 lsm6ds33; // accelerometer, gyroscope
Adafruit_APDS9960 apds; // proximity, light, color, gesture

#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 3

#define RF95_FREQ 433.0

// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);

int MaxAlt = 0;
int MaxSpd = 0;
int MaxTem = 0;
int MinTem = 10000;


void setup() {

  sht30.begin();
  bmp280.begin();
  lis3mdl.begin_I2C();
  lsm6ds33.begin_I2C();
  apds.begin();
  apds.enableColor(true);
  servo.attach(5);
  servo.write(90);
  rtc.start();
  SD.begin(10);  
  pinMode(RFM95_RST, OUTPUT);
  digitalWrite(RFM95_RST, HIGH);

  Serial.begin(115200);
  GPS.begin(9600);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
  delay(1000);
  GPSSerial.println(PMTK_Q_RELEASE);

  delay(100);

  // manual reset
  digitalWrite(RFM95_RST, LOW);
  delay(10);
  digitalWrite(RFM95_RST, HIGH);
  delay(10);

  while (!rf95.init()) {
    while (1);
  }

  if (!rf95.setFrequency(RF95_FREQ)) {
    while (1);
  }

  bmp280.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                     Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                     Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                     Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                     Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */

  rf95.setTxPower(23, false);
  if (!SD.begin(10)) {
    Serial.println("Card failed");
  }
  if (!apds.begin()) {
    Serial.println("failed to initialize device! Please check your wiring.");
  }
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
  }
  if (! rtc.initialized() || rtc.lostPower()) {
    Serial.println("RTC is NOT initialized, let's set the time!");
  }
}

void loop() {

  if (GPS.altitude > MaxAlt) {MaxAlt = GPS.altitude; }
  if (GPS.speed > MaxSpd) {MaxAlt = GPS.speed; }  
  if (bmp280.readTemperature() > MaxTem) {MaxTem = bmp280.readTemperature(); } 
  if (bmp280.readTemperature() < MinTem) {MinTem = bmp280.readTemperature(); }

  sensors_event_t temp_event, pressure_event;
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t temp;
  lsm6ds33.getEvent(&accel, &gyro, &temp);
  lis3mdl.read();
  DateTime now = rtc.now();

  float magnetic_x, magnetic_y, magnetic_z;
  float Lo = GPS.longitude;
  float La = GPS.latitude;
  int Al = GPS.altitude;
  int Sp = GPS.speed;
  int humidity = sht30.readHumidity();
  float temperature = temp_event.temperature;
  float accel_x, accel_y, accel_z;
  float gyro_x, gyro_y, gyro_z;

  delay(1000);

  char c = GPS.read();
  if (GPS.newNMEAreceived()) {
    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 (GPS.fix) {
    Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
    Serial.print(", ");
    Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
    Serial.print(GPS.speed); Serial.println("kn");
    Serial.print(GPS.altitude); Serial.println("m");
  }

  delay(1000);
  char radiopacket[2] = "N";
  Serial.println(radiopacket);
  radiopacket[1] = 0;
  rf95.send((uint8_t *)radiopacket, 2);
  rf95.waitPacketSent();

  delay(1000);
  char Lat[1] = {};
  dtostrf(La, 9, 4, Lat);
  Serial.print("Sending "); Serial.println(Lat);
  rf95.send((uint8_t *) &Lat, 9);
  delay(10);
  rf95.waitPacketSent();

  delay(1000);
  char Lon[1] = {};
  dtostrf(Lo, 9, 4, Lon);
  Serial.print("Sending "); Serial.println(Lon);
  rf95.send((uint8_t *) &Lon, 9);
  delay(10);
  rf95.waitPacketSent();

  delay(1000);
  char Spd[1] = {};
  dtostrf(Sp, 3, 0, Spd);
  Serial.print("Sending "); Serial.println(Spd);
  rf95.send((uint8_t *) &Spd, 3);
  delay(10);
  rf95.waitPacketSent();

  delay(1000);
  char Alt[1] = {};
  dtostrf(Al, 6, 0, Alt);
  Serial.print("Sending "); Serial.println(Alt);
  rf95.send((uint8_t *) &Alt, 6);
  delay(10);
  rf95.waitPacketSent();

  String dataString = "";

  dataString += String(int(now.month())) += "/";
  dataString += String(int(now.day())) += "/";
  dataString += String(int(now.year())) += " ";
  dataString += String(int(now.hour())) += ":";
  dataString += String(int(now.minute())) += ":";
  dataString += String(int(now.second())) += " ";
  dataString += String(GPS.latitude, 4);
  dataString += String(GPS.lat) += ", ";
  dataString += String(GPS.longitude, 4);
  dataString += String(GPS.lon) += " ";
  dataString += String(bmp280.readTemperature()) += " C Min:";
  dataString += String(MinTem) += " C Max:";
  dataString += String(MaxTem) += " C Humidity:";
  dataString += String(sht30.readHumidity()) += "% ";
  dataString += String(bmp280.readPressure()) += " Pa ";
  dataString += String(GPS.speed) += " kn ";
  dataString += String(MaxSpd) += " kn MAX ";
  dataString += String(GPS.altitude) += " m ";
  dataString += String(MaxAlt) += " m MAX MagX:";
  dataString += String(lis3mdl.x) += " MagY:";
  dataString += String(lis3mdl.y) += " MagZ:";
  dataString += String(lis3mdl.z) += " uTesla Acc x:";
  dataString += String(accel.acceleration.x) += " Acc y:";
  dataString += String(accel.acceleration.y) += " Acc z:";
  dataString += String(accel.acceleration.z) += " m/s^2 Gyro x:";
  dataString += String(gyro.gyro.x) += " Gyro y:";
  dataString += String(gyro.gyro.y) += " Gyro z:";
  dataString += String(gyro.gyro.z) += " dps Proximity:";
  dataString += String(apds.readProximity());

  File dataFile = SD.open("datalog.txt", FILE_WRITE); //Opens new text file on SD card called datalog.txt

  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
  }

  Serial.println(dataString);

   if (MaxAlt > 4000 && GPS.altitude < 3000) {
  servo.write(15); // moves servo to 70 degrees
   }

}