As suggested to me in a prior Topic I had posted ( previous Topic) , I am re-working my program to write data pairs (time, force) to an Array first, then attempting to loop through that array and write all the data-pairs to an SD card. Here is the background of my setup:
- Generic {SD-card + RTC} shield, using the DS1307Z chip
- Arduino UNO R4 Minima
- Arduino 2.8.3 IDE (running on a Linux platform)
Note that the {SD-card + RTC} Shield I am now using is NOT the same as used in the prior Topic that I noted above; that hardware is actually in use and I did not have a duplicate of the Adafruit shield. As a result, there are multiple changes to the code.
I have proved that the hardware works, by using Arduino Examples for the DS1307Z and the SD-card.
The re-write is partially successful. I added many comments to figure out where the execution stopped. The sticking point, now, is where I try to loop-through the Array, and write data-pairs to the SD card. After several hours of focus on this, I still cannot see what the problem is. There are no compilation-time errors. A fresh set of eyes and some insight born of experience would be greatly appreciated. Here is my code; I have added a comment ("<<<<< output terminates abruptly here!") where the execution appears to hang up / terminate.
#include <SD.h>
#include "HX711.h"
//#include <Wire.h"
#include <I2C_RTC.h>
static DS1307 RTC;
#define SD_FAT_TYPE 3
#define PIN_SPI_CS 4
#define FILE_NAME "test.txt"
uint8_t dataPin = 2;
uint8_t clockPin = 3; //3
const int chipSelect = 10;
File myFile;
HX711 scale;
float f;
float Unixtime;
int i = 0; //iteration counter
int j = 0; //iteration counter
int k = 0; //iteration counter
int n = 20; // number of 1-second interval iterations
float matrix[20][1]; // for up to 21 data pairs of [unixtime], [force]
void setup() {
scale.begin(dataPin, clockPin); // setup the HX711 module
scale.set_scale(27500); // scale output from the 1 kg beam so that output is in Gram units
scale.tare(); // tare the scale output
Serial.begin(115200); // Open serial communications, and wait for a port to open:
while (!Serial) // wait for Serial Monitor to connect. Needed for native USB port boards only:
;
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("1. is a card inserted?");
Serial.println("2. is your wiring correct?");
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!");
while (true)
;
}
Serial.println("initialization done.");
if (RTC.isConnected() == false) {
Serial.println("RTC Not Connected!");
while (true)
;
}
Serial.println("*** RTC 1307 ***");
Serial.print("Is Clock Running : ");
if (RTC.isRunning())
Serial.println("Yes!");
myFile = SD.open("test.txt", FILE_WRITE); // open the SD file. Note that only one file can be open at a time, so you have to close this file before opening another.
if (myFile) { // if the file opened okay, write to it:
Serial.print("Writing to test.txt ...");
myFile.println("testing 999.");
myFile.close(); // close the file:
Serial.println("SD File now closed.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt ");
}
Serial.println("Reading & display file contents");
myFile = SD.open("test.txt"); // re-open the file for reading contents
if (myFile) {
Serial.println("test.txt :");
while (myFile.available()) { // read data from the file until the end:
Serial.write(myFile.read());
}
myFile.close(); // close the file:
} else { // if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
while (i < n) { //time-and-weight measuring steps at 1 second intervals
Serial.print(RTC.getDateTimeString());
Serial.println("");
Unixtime = RTC.getEpoch();
// f = scale.get_units(5);
matrix[i][0] = Unixtime;
matrix[i][1] = scale.get_units(5);
Serial.println(matrix[i][0]);
Serial.println(matrix[i][1]);
delay(1000);
i = i + 1;
}
myFile = SD.open("test.txt", FILE_WRITE); // open SD file to write the data stored in 'matrix'
if (myFile) {
Serial.print("Now Writing 'matrix' to test.txt ...");
myFile.println(",,"); //blanks as a data-block delimiter
while (j < n) {
Serial.println("first step"); /// <<<<< output terminates abruptly here!>>>>>>>>>>>>>
myFile.print(matrix[j][0]);
myFile.print(matrix[j][1]);
Serial.println("second step");
j = j + 1;
}
} else {
Serial.println("error opening test.txt "); // if the file didn't open, print an error
myFile.close();
// Serial.print(RTC.getDateTimeString());
// Serial.print(", Unix Time: ");
// Serial.println(RTC.getEpoch());
// f = scale.get_units(5);
// Serial.println(f);
// Serial.print("Done iterations and file closed");
}
}