RTClib function rtc.now() resetting sketch

This sketch is an extract from a larger sketch, and has been created to isolate the problem area. The hardware schematic is shown in the attachment. For testing purposes I am using an Uno, the schematic is drawn for an Adafruit Metro Mini, which I'm told functions exactly like an Uno. IDE is 1.8.19.

No errors are reported when compiling or uploading.
The code works fine if I comment out the line: DateTime now = rtc.now();
With the line in, the sketch resets and starts again.
I have tried deleting and reinstalling the library, no change.

Typical output from the Serial Monitor is:
E:\MODEL BOATS\Arduino\Current-voltage recorder\TEST_VERS_Model_boat_perf_recorder\TEST_VERS_Model_boat_perf_recorder.ino
Last updated/compliled: Jul 7 2022
712 selected: 5A
Initializing SD card...
So far so good
E:\MODEL BOATS\Arduino\Current-voltage recorder\TEST_VERS_Model_boat_perf_recorder\TEST_VERS_Model_boat_perf_recorder.ino
Last updated/compliled: Jul 7 2022
712 selected: 5A
Initializing SD card...
So far so good
E:\MODEL BOATS\Arduino\Current-voltage recorder\TEST_VERS_Model_boat_perf_recorder\TEST_VERS_Model_boat_perf_recorder.ino
Last updated/compliled: Jul 7 2022
712 selected: 5A
Initializing SD card...
So far so good

/*
  Read current using ACS712 Hall Effect sensor
  Read input voltage using voltage divider
  Read distance and speed from GPS.
  Read prop speed using ?
  Read date time of test from RTC DS3231
  Write data to microSD card.

  References:
  https://arduinogetstarted.com/tutorials/arduino-rtc
  https://create.arduino.cc/projecthub/electropeak/sd-card-module-with-arduino-how-to-read-write-data-37f390
  2020 June
*/

#include <RTClib.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
RTC_DS3231 rtc;
File Datafile;
// char t[32];

#define ZeroV30 505.0
#define ZeroV05 516.0
#define Const05 .02639
#define Const30 .07398

float ZeroV;                                  //  For 30A ACS712 device, ZeroV = 505.0, for 20A = ?, for 5A =?
float CIcalc;
bool Testing = true, P712;

const int VoltsPin = A1;
const int I30Pin = A2;
const int I05Pin = A3;
const int Select712 = 2;             //if true/high,using 30A range, else 5A.
const int buzzer = 7;

void setup() {

  pinMode(buzzer, OUTPUT);
  pinMode(Select712, INPUT_PULLUP);

  Serial.begin(9600);
  Serial.println(__FILE__);
  Serial.print(F("Last updated/compliled: "));
  Serial.println(__DATE__);

  P712 = digitalRead(Select712);

  // if (Testing){
  Serial.print("712 selected: ");
  if (P712) {
    Serial.println("30A");
  }
  else {
    Serial.println("5A");
  }
  //  }

  if (P712) {                    // If high/true, reading 30A, otherwise 5A
    ZeroV = ZeroV30;
    CIcalc = Const30;
  }
  else {
    ZeroV = ZeroV05;
    CIcalc = Const05;
  }

  Serial.println("Initializing SD card...");
  delay(1000);

  //    while (!Serial) {
  //       ; // wait for serial port to connect. Needed for native USB port only
  //           }

  if (!SD.begin(10)) {
    if (Testing) {
      Serial.println(F("initialization failed!"));
    }
    tone(buzzer, 4000, 4000);                           //sound buzzer for 4 secs if card fails to open
    while (1);
  }
  if (SD.exists("MBtest.txt")) {
    SD.remove("MBtest.txt");
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  Datafile = SD.open("MBTest.txt", FILE_WRITE);

  Serial.println("So far so good");
  delay(1000);

  // if the file opened okay, write to it:
  //    Datafile.println("Date and time not established.");
  DateTime now = rtc.now();
  /*   Datafile.print("Date/Time: ");
      Datafile.print("Date:");
      Datafile.print(now.year());
      Datafile.print('/');
      Datafile.print(now.month());
      Datafile.print('/');
      Datafile.print(now.day());
      Datafile.print("    ");
      Datafile.print("Time:");
      Datafile.print(now.hour());
      Datafile.print(':');
      Datafile.print(now.minute());
      Datafile.print(':');
      Datafile.print(now.second());
      Datafile.println("Time in mins, V in volts, Amps, mAm in milliAmp minutes");
      Datafile.println(F("Time   Vin   Amps   Cumulative mAm"));
  */
  Serial.println("Even better");
  delay(1000);
}

void loop() {
  while (1) {
    Serial.println(" In the loop");
  }                           //infinite loop

}  //******************************** loop end

No rtc.begin() call so any call to the I2C interface will result in a null pointer dereference.

Many thanks for your help. That has solved the problem.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.