Confusing code error

Hello

I am using a prewritten code for a NANO ble sense and copy-pasted it but it doesn't seem to work. I'm sure I am doing something obviously wrong but I'm by no means a programmer which is why I was relying on someone else but I hope you guys can help me out. I'll copy the code in then the error details below that.

Thanks in advance :slightly_smiling_face:

/****************************************************************************
 * Data Logger v30
 * 28-Jan-21
 * Ruud Kapteijn
 * Available under GPL3+
 *
 * Uses the AHRS Madgwick code from Kris Winer (ref AHRS.h)
 * Original code by Kris Winer
 * date: Nov 1, 2014
 * code: https://github.com/j-mcc1993/LSM9DS1/blob/master/LSM9DS1_BasicAHRS_Nano33.ino
 * discussion threa: https://github.com/kriswiner/LSM9DS1/issues/14
 */
#include <Arduino_LSM9DS1.h>
#include <Arduino_LPS22HB.h> //Include library to read Pressure
#include <SPI.h>
#include <SD.h>
// #include <RF24.h>

#include "Ublox_NMEA_GPS.h"
#include "AHRS.h"
#include "AHRS_gvars.h"

#define LEDR        (22u)
#define LEDG        (23u)
#define LEDB        (24u)
#define LEDPWR      (25u)

// GPS Vars
Ublox_NMEA_GPS myGPS;
boolean fix = false;
int satellites, prev_satellites;

// potentionmeter
int potmeter = 0;

// IMU Vars
// float ax, ay, az;
// float gx, gy, gz;
// float mx, my, mz;

// Baro vars
float pressure;

// SD Vars
File dataFile;
int chipSelect = 10;
int seqnr = 0;
String fileName;
int counter = 0;
int byteCounter = 0;
String dataString;
long last_write = 0;

/*
// transmitter vars
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";     //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.
long last_write = 0;
char tx_buffer[256];
String dataString;
int counter = 0;
int stringLength = 0;
const char ping[] = "ping";
*/

// statistic vars
boolean first_fix = true;
long    start_loops;
long    count_loops=0;
long    count_RMC = 0;
long    count_GGA = 0;

void setup() {                                    // switch buildin LED off
  digitalWrite(LEDPWR, HIGH); digitalWrite(LEDR, HIGH); digitalWrite(LEDG, HIGH);  digitalWrite(LEDB, HIGH);
  digitalWrite(LEDR, LOW);                        // switch buildin LED to red -> not initialized.

  Serial.begin(9600);                             // Open serial communications and wait for port to open:
  // while (!Serial);                                // wait until monitor is opened
  Serial.println("INFO: ** Telemetry V10 **");

  myGPS.init();
  Serial.println("INFO: GPS Started");

  if (!IMU.begin()) {
    Serial.println("ERROR: Failed to initialize IMU! halted.");
    while (true);
  }
  Serial.println("INFO: IMU Started!");

  if (!BARO.begin()) {        //Initialize Pressure sensor
    Serial.println("ERROR: Failed to initialize Pressure Sensor! halted.");
    while (1);
  }
  Serial.println("INFO: BARO Started!");

  if (AHRS_setup())
    Serial.println("INFO: AHRS setup ok");
  else {
    Serial.println("ERROR AHRS setup failed. Halted!");
    while(true);
  }

/*
  radio.begin();                  //Starting the Wireless communication
  radio.openWritingPipe(address); //Setting the address where we will send the data
  radio.setPALevel(RF24_PA_MIN);  //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
  radio.stopListening();          //This sets the module as transmitter
  Serial.println("INFO: Transmitter started");
*/

  if (!SD.begin(chipSelect)) {              // Setup SD Card
   Serial.println("ERROR: SD Card failed or not present! Halted.");
   while (1);
  }
  Serial.println("INFO: SD card initialized");
  seqnr = 1;                              // Check on existing files
  fileName = "DL" + String(seqnr) + ".CSV";
  while(SD.exists(fileName)) {
    seqnr++;
    fileName = "DL" + String(seqnr) + ".CSV";
    delay(200); // Make sure file access is done
  }
  dataFile = SD.open(fileName, FILE_WRITE);   // Create new CSV file with appropriate headers
  dataFile.println("Nr,Mills,Date,UTC,Lat,Lon,Sog,Cog,Alt,Sat,Fix,Pm,Ax,Ay,Az,Gx,Gy,Gz,Mx,My,Mz,Pres,Pitch,Roll,Yaw");
  Serial.println("INFO: new data file created, ready for logging.");      // Done with SD Card Init

  digitalWrite(LEDR, HIGH);                       // switch buildin LED off
  digitalWrite(LEDB, LOW);                        // switch buildin LED to blue -> looking for satellites.
  Serial.println("Initialization completed");
}

void loop() {                                     // run continuously
  myGPS.update();
  prev_satellites = satellites;
  satellites = myGPS.getSIV().toInt();
  if (satellites != prev_satellites)
    Serial.println("SIV: " + String(satellites) + " Pitch: " + String(pitch));

  if (satellites > 2) {                          // return TRUE if fix
    if (!fix) Serial.println("INFO: Fix created!");
    fix = true;
    digitalWrite(LEDB, HIGH);                       // switch buildin LED off
    digitalWrite(LEDG, LOW);                        // switch buildin LED to green -> (potential) fix.
    if (first_fix) {
      first_fix = false;
      start_loops = millis();
      count_loops = 0;
      count_RMC = 0;
      count_GGA = 0;
    }
  } else {
    if (fix) Serial.println("INFO: Fix lost!");
    fix = false;
    digitalWrite(LEDG, HIGH);                       // switch buildin LED off
    digitalWrite(LEDB, LOW);                        // switch buildin LED to blue -> looking for satellites.
  }

  potmeter = analogRead(7);                           // read value of potential meter via ADC pin 7
  getIMUData();
  pressure = BARO.readPressure();

  AHRS_update();

  // write line to datafile
  if (dataFile && fix && millis() - last_write > 150) {
    counter += 1;
    dataString = createDataString();
    // dataString = "dit is een lange test data string met een hele boel bytes";
    // Serial.print("INFO: write to SD card: ");
    // Serial.println(dataString);
    dataFile.println(dataString);
    byteCounter += dataString.length();
    if (byteCounter > 1024) {
      // Serial.println("INFO: flush buffer to SD card");
      dataFile.flush();
      byteCounter = 0;
    }
  }

/*
  if (fix && millis() - last_write > 250) {     // send data string
    last_write = millis();
    counter += 1;
    dataString = createDataString() + '\n';
    // Serial.print("INFO: write to SD card: ");
    for (int i = 0; i < dataString.length(); i+=32) {
      String section = dataString.substring(i, i + 32);
      section.toCharArray(tx_buffer, sizeof(tx_buffer));
      radio.write(&tx_buffer, section.length());           //Sending the message to receiver
    }
  }
*/

  count_loops++;
  if (count_loops % 1000 == 0) {
    Serial.println("loops: "+String(count_loops)+", satellites: "+myGPS.getSIV()+", pitch: "+String(pitch));
  }
  if (count_loops % 10000 ==0) {
    Serial.println("loops: "+String(count_loops)+", avg loop: "+String((millis() - start_loops)/count_loops)+" ms, avg RMC: "+String((millis() - start_loops)/count_RMC)+" ms, avg GGA: "+String((millis() - start_loops)/count_GGA));
    // radio.write(&ping, sizeof(ping));               //Sending the message to receiver
  }
}

boolean getIMUData() {
  //Accelerometer values
  if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(ax, ay, az);
  }
  //Gyroscope values
  if (IMU.gyroscopeAvailable()) {
    IMU.readGyroscope(gx, gy, gz);
  }
  //Magnetometer values
  if (IMU.magneticFieldAvailable()) {
    IMU.readMagneticField(mx, my, mz);
  }
}

String createDataString() {
  String ds = "";

  ds += String(counter) + ",";
  ds += String(millis()) + ",";
  ds += myGPS.getDAT() + ",";
  ds += myGPS.getTIM() + ",";
  ds += myGPS.getLAT() + ",";
  ds += myGPS.getLON() + ",";
  ds += myGPS.getSOG() + ",";
  ds += myGPS.getCOG() + ",";
  ds += myGPS.getALT() + ",";
  ds += myGPS.getSIV() + ",";
  ds += myGPS.getFIX() + ",";

  ds += String(potmeter) + ",";

  ds += String(ax) + ",";
  ds += String(ay) + ",";
  ds += String(az) + ",";
  ds += String(gx) + ",";
  ds += String(gy) + ",";
  ds += String(gz) + ",";
  ds += String(mx) + ",";
  ds += String(my) + ",";
  ds += String(mz) + ",";

  ds += String(pressure) + ",";

  ds += String(pitch) + ",";
  ds += String(roll) + ",";
  ds += String(yaw);

  return(ds);
}``

Error Message:
Arduino: 1.8.15 (Windows Store 1.8.49.0) (Windows 10), Board: "Arduino Nano 33 BLE"

sketch\datalogger_ino.ino.cpp.o: In function `createDataString()':

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:226: undefined reference to `Ublox_NMEA_GPS::getDAT()'

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:227: undefined reference to `Ublox_NMEA_GPS::getTIM()'

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:228: undefined reference to `Ublox_NMEA_GPS::getLAT()'

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:229: undefined reference to `Ublox_NMEA_GPS::getLON()'

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:230: undefined reference to `Ublox_NMEA_GPS::getSOG()'

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:231: undefined reference to `Ublox_NMEA_GPS::getCOG()'

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:232: undefined reference to `Ublox_NMEA_GPS::getALT()'

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:233: undefined reference to `Ublox_NMEA_GPS::getSIV()'

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:234: undefined reference to `Ublox_NMEA_GPS::getFIX()'

sketch\datalogger_ino.ino.cpp.o: In function `__static_initialization_and_destruction_0':

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:31: undefined reference to `Ublox_NMEA_GPS::Ublox_NMEA_GPS()'

sketch\datalogger_ino.ino.cpp.o: In function `setup':

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:83: undefined reference to `Ublox_NMEA_GPS::init()'

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:98: undefined reference to `AHRS_setup()'

sketch\datalogger_ino.ino.cpp.o: In function `loop':

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:135: undefined reference to `Ublox_NMEA_GPS::update()'

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:137: undefined reference to `Ublox_NMEA_GPS::getSIV()'

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:164: undefined reference to `AHRS_update()'

C:\Users\MATTT\Documents\Arduino\datalogger_ino/datalogger_ino.ino:198: undefined reference to `Ublox_NMEA_GPS::getSIV()'

collect2.exe: error: ld returned 1 exit status

exit status 1

Error compiling for board Arduino Nano 33 BLE.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

It tells you that you would get more details like this. Why don’t you give it a shot

Have you got the same version of the Ublox_NMEA_GPS library installed as the originator of the sketch ?

I downloaded the .h files needed with the source code so I would think it's up to date. That said I think this is all from 2014 and it isn't on the Arduino library...

This is likely the problem. The problem is that, even though it's the only file mentioned in the "No such file or directory" error message, the header file is only one part of the library, providing its declarations. You also need the source files, which provide the definitions. When those definitions are missing, you get this "undefined reference to" error.

The fix is to delete the incomplete library installation and then install the complete library. You can learn about installing libraries here:

Please let me know if you have any questions or problems.

That's excellent, thank you. I actually made the .h files myself from the .c code in each download (because they weren't available in the library manager). - Could that be the problem?

It's not clear what you mean by that.

I visited the link in the sketch's header comment (LSM9DS1/LSM9DS1_BasicAHRS_Nano33.ino at master · j-mcc1993/LSM9DS1 · GitHub), but it doesn't seem to match with the code you shared.

Please post a link to where you downloaded these .c files from.

Where have you put the .c files?

I got it all from here:

And was also identically posted elsewhere. You're right though, it doesn't match the code in the comment link.

I have tried two methods. First I tried using CodeBlocks to make .h versions of the .c files. Secondly I used the 'Add File' feature on the Arduino IDE which created an attached tab. My apologies I am new to all of this so have probably used incorrect terminology, not to mention whatever else I have done wrong!

Ah, I think I see now. There is some problem (I guess it's a bug in Arduino Project Hub itself?) that causes the .h files of the sketch provided by the project author to be renamed with a .c file extension when you download it.

Fortunately, the code is also available on GitHub, which actually works correctly. Please try this:

  1. Download the sketch here: https://github.com/RuudKapteijn/DataLogger/archive/refs/heads/main.zip
  2. Unzip the downloaded "DataLogger-main.zip" file.
  3. Rename the unzipped "DataLogger-main" folder to "DataLogger".
  4. Open that "DataLogger" sketch in the Arduino IDE.

Hopefully that sketch will compile as expected.

It worked, thank you SO much for your help. Much appreciated

You're welcome. I'm glad to hear it's working now. Enjoy!
Per

So upon further review the SD card was not working so I decided to try uploading to the NANO again (no code changed) and now have this error message:

Arduino: 1.8.15 (Windows Store 1.8.49.0) (Windows 10), Board: "Arduino Nano 33 BLE"

In file included from C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/mbed/platform/include/platform/platform.h:26:0,

             from C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/mbed/platform/include/platform/FileHandle.h:25,

             from C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/macros.h:41,

             from C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\variants\ARDUINO_NANO33BLE/pins_arduino.h:3,

             from C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/Arduino.h:76,

             from sketch\DataLogger.ino.cpp:1:

C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/mbed/platform/include/platform/mbed_retarget.h:66:18: error: expected unqualified-id before numeric constant

#define O_RDONLY 0 ///< Open for reading

              ^

C:\Users\MATTT\Documents\Arduino\libraries\SD\src/utility/SdFat.h:55:15: note: in expansion of macro 'O_RDONLY'

uint8_t const O_RDONLY = O_READ;

           ^~~~~~~~

C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/mbed/platform/include/platform/mbed_retarget.h:67:18: error: expected unqualified-id before numeric constant

#define O_WRONLY 1 ///< Open for writing

              ^

C:\Users\MATTT\Documents\Arduino\libraries\SD\src/utility/SdFat.h:59:15: note: in expansion of macro 'O_WRONLY'

uint8_t const O_WRONLY = O_WRITE;

           ^~~~~~~~

C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/mbed/platform/include/platform/mbed_retarget.h:68:18: error: expected unqualified-id before numeric constant

#define O_RDWR 2 ///< Open for reading and writing

              ^

C:\Users\MATTT\Documents\Arduino\libraries\SD\src/utility/SdFat.h:61:15: note: in expansion of macro 'O_RDWR'

uint8_t const O_RDWR = (O_READ | O_WRITE);

           ^~~~~~

C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/mbed/platform/include/platform/mbed_retarget.h:66:18: error: expected unqualified-id before numeric constant

#define O_RDONLY 0 ///< Open for reading

              ^

C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/mbed/platform/include/platform/mbed_retarget.h:76:22: note: in expansion of macro 'O_RDONLY'

#define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)

                  ^~~~~~~~

C:\Users\MATTT\Documents\Arduino\libraries\SD\src/utility/SdFat.h:63:15: note: in expansion of macro 'O_ACCMODE'

uint8_t const O_ACCMODE = (O_READ | O_WRITE);

           ^~~~~~~~~

C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/mbed/platform/include/platform/mbed_retarget.h:66:18: error: expected ')' before numeric constant

#define O_RDONLY 0 ///< Open for reading

              ^

C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/mbed/platform/include/platform/mbed_retarget.h:76:22: note: in expansion of macro 'O_RDONLY'

#define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)

                  ^~~~~~~~

C:\Users\MATTT\Documents\Arduino\libraries\SD\src/utility/SdFat.h:63:15: note: in expansion of macro 'O_ACCMODE'

uint8_t const O_ACCMODE = (O_READ | O_WRITE);

           ^~~~~~~~~

C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/mbed/platform/include/platform/mbed_retarget.h:70:20: error: expected unqualified-id before numeric constant

#define O_APPEND 0x0008 ///< Set file offset to end of file prior to each write

                ^

C:\Users\MATTT\Documents\Arduino\libraries\SD\src/utility/SdFat.h:65:15: note: in expansion of macro 'O_APPEND'

uint8_t const O_APPEND = 0X04;

           ^~~~~~~~

C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/mbed/platform/include/platform/mbed_retarget.h:71:20: error: expected unqualified-id before numeric constant

#define O_CREAT 0x0200 ///< Create file if it does not exist

                ^

C:\Users\MATTT\Documents\Arduino\libraries\SD\src/utility/SdFat.h:69:15: note: in expansion of macro 'O_CREAT'

uint8_t const O_CREAT = 0X10;

           ^~~~~~~

C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/mbed/platform/include/platform/mbed_retarget.h:73:20: error: expected unqualified-id before numeric constant

#define O_EXCL 0x0800 ///< Fail if file exists

                ^

C:\Users\MATTT\Documents\Arduino\libraries\SD\src/utility/SdFat.h:71:15: note: in expansion of macro 'O_EXCL'

uint8_t const O_EXCL = 0X20;

           ^~~~~~

C:\Users\MATTT\Documents\ArduinoData\packages\arduino\hardware\mbed_nano\2.3.1\cores\arduino/mbed/platform/include/platform/mbed_retarget.h:72:20: error: expected unqualified-id before numeric constant

#define O_TRUNC 0x0400 ///< Truncate file to zero length

                ^

C:\Users\MATTT\Documents\Arduino\libraries\SD\src/utility/SdFat.h:73:15: note: in expansion of macro 'O_TRUNC'

uint8_t const O_TRUNC = 0X40;

           ^~~~~~~

Multiple libraries were found for "SD.h"

Used: C:\Users\MATTT\Documents\Arduino\libraries\SD

Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\libraries\SD

exit status 1

Error compiling for board Arduino Nano 33 BLE.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I just tried compiling the sketch from https://github.com/RuudKapteijn/DataLogger/archive/refs/heads/main.zip and did not experience those errors.

It might be that you are using an outdated version of the SD library. Please do this:

  1. Select Sketch > Include Library > Manage Libraries... from the Arduino IDE's menus.
  2. Wait for the update to finish.
  3. In the "Filter your search" field, type: sd.
  4. Press Enter.
  5. Scroll down through the list of libraries until you see "SD Built-in by Arduino". Click on it.
  6. The latest version of the library is 1.2.4. If you have an older version installed, you will see an Update button. If so, click it.
  7. Wait for the installation to finish.
  8. Click the Close button.

Now you can try compiling the sketch again.

Ahh yes, this has allowed the sketch to be uploaded successfully but still no luck initializing the SD card, showing "ERROR: SD Card failed or not present! Halted." in the serial monitor. I know it is wired correctly and that the SD card works, is there anything else you could think of that would be causing this? I have also tried 2 SD cards and 3 SD card adapters.

https://www.arduino.cc/en/Reference/SDCardNotes

Thanks, I have tried formatting it many times but it only gives me the option of FAT32. While it does say this is compatible it recommends FAT16, could this be the problem?

Formatted under Windows or Linux?
From
https://lastminuteengineers.com/arduino-micro-sd-card-module-tutorial/

If you have a new SD card, chances are it’s already pre-formatted with a FAT file system. However you may have problems with how the factory formats the card, or if it’s an old card it needs to be reformatted. Either way, it’s always good idea to format the card before using, even if it’s new!

We strongly recommend you use the official SD card formatter utility – written by the SD association it solves many problems that come with bad formatting! Download the formatter and run it on your computer, just select the right drive and click FORMAT.

Unfortunately this method of formatting doesn't solve the problem. I have just tried another SD card reader and card too which shows the same message in the serial monitor.
This makes me think its likely something else, although it's wired exactly as the drawing indicated which I can't see any error with, and the coding is copied and u/Pert didn't have any issues

As you have the program running now I can only suggest that you have a good check of how your circuit is connected and see if you have any bad connections. Have you done anything different from the original project?

No it's all identical to the example, hardware included. This is why I am so confused