Greetings! my first post here after a week of trying to find out what is wrong with my code (I have very little coding background only using the opensource matlab, Octave) and this is only my 5th implementation of the arduino. I have scanned over it multiple times and do not see why the code does not work. I am using an 'Elegoo Arduino Mega2560' with a 'HiLetGo SD Shield 3.0' as well as an HX711 board. Overall goal is to reproduce some code I had that used much like the SD example "DataLogger" That i had modified, but instead taking the data from the HX711 and writing it to the card. I know my loop as written does not do this as I am stuck in the Setup() of my code.
In the setup, well I want to set up the SD card and make sure its in there correctly and do a one time write of a value to the beginning of a new .txt file. I can probe the SD card and see that it is there, but I can not create a file in it as I had in previous attempts.
In the setup I jump to another function "zeropoint" which finds the default value from the HX711 and return said value in the variable 'restingval' to be written on the first line of the SD card. It then goes through the examples' code of testing the SD card which it returns the line "card initialized." I then create a string 'FID' which will be the name of the file it will need to write to (I think I understand the 8.3 format...). FID is created and sent to another function to run through the files in the SD card and count until the file name does not exist in the card to then write to it (I had this working in another script I had written). FID is then returned as "DATA0.txt" which is then used to create a file in the SD card... but low and behold as is my code spits out "error opening file" to the serial monitor - which to me tells me that the file was not created. Is there something I am overlooking?
I can upload any of the examples and the scripts all execute fine and write/read to the SD card.
Attached is my script .ino and the HX711 headers along with my version of Datalogger.ino which also works.
I have tried changing the line < File dataFile = SD.open(FID, FILE_WRITE) > to < File dataFile = SD.open("STRING01.txt", FILE_WRITE) > to no avail. Making me rule out my FID being a wrong format.
#include "HX711.h"
#include <SPI.h>
#include <SD.h>
HX711 cell(3, 2); //format is "HX711 cell(DT, SCK)" where DT is the HX711 data output and SCK is the serial clock input.
int delaytime = 20; //this is the delay between readings
int zeroSamples = 10;//this is the number of iterations to average the zero point (time is roughly this val times delaytime
const int SDLED = 13;// pin for the writing LED (13 for onboard led)
File dataFile;
long val = 0; //value for math in finding average
const int chipSelect = 4; // data pin for sd card
float count = 0; //value for math in finding average
long restingval = 5; //calculated value of loadcell zero point
unsigned long t; //use for keeping track of time
int filecounter = 0; //used for filenaming
String filename = "DATA"; //makes the filename variable a global variable to be used in functions
String FID = ""; // makes the FID
void setup() {
Serial.begin(9600); //sets the baud for the serial port
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(SDLED, OUTPUT);
restingval = zeropoint(zeroSamples); //uses zeropoint function to declare the resting value
////////////////////////////////////////////////////////////
//HERE IS ALL THE SD CARD STUFF.........................//
Serial.print("Initializing SD card..."); //provides status to serial
//
// see if the card is present and can be initialized: //
if (!SD.begin(chipSelect)) { //tests to see if SD card is present in correct format (FAT32)
Serial.println("Card failed, or not present"); //provides status to serial
// don't do anything more: //
digitalWrite(SDLED, HIGH); //makes LED bright
return;} //exits set up if cant find SD on chipSelect
Serial.println("card initialized."); //provides status to serial
digitalWrite(SDLED, HIGH); //makes LED bright
delay(100); //
FID = filename + String(filecounter) + ".txt"; //sets FID to initial filename option 'filename'0
FID = filenamedeterminer(filecounter, FID); //determines number after 'filename' ***ie: DATA_3
Serial.println(FID);
File dataFile = SD.open(FID, FILE_WRITE); //assigns handle to file on SD card
if (dataFile) { //tests to see if file is open to write
dataFile.println(String(restingval)); //write to SD file:
dataFile.close(); //closes the file to save ram
Serial.print("This is the restingval: ");Serial.println(restingval);} // print to the serial port too:
else {
// if the file didn't open, print an error:
Serial.println("error opening file");}
////////////////////////////////////////////////////////////
}
//////////////////////////////////////////////////////////////////
void loop() {
// String dataString = ""; //defines variabel to used to assemble data as string to log.
digitalWrite(SDLED, HIGH); //makes LED bright
long data = cell.read(); //assigns the HX711 data to var 'data'
//long data = 0.5 * val + 0.5 * cell.read(); // take recent average use this or next line
t = millis();// assigns the time in ms to 't' right after
digitalWrite(SDLED, LOW);
delay(delaytime);
Serial.print(restingval);
Serial.print (" ");
// Serial.print( count) ;
Serial.print (" ");
Serial.println( val );
delay(delaytime);
}
/////////////////////////////////////////////////////////////////
long zeropoint(int zeroSamples) { //THIS FUNCTION FINDS THE AVERAGE VALUE OF THE LOAD CELL WITH SET UP
// Serial.println("banana");
float X = 0;
while (X < zeroSamples) {
X++;
val = ((X-1)/X) * val + (1/X) * cell.read(); // take long term average
// Serial.print ("X is ");
// Serial.print (X);
// Serial.print ( " ");
// Serial.println(val);
// delay(100);
}
return val;}
//////////////////////////////////////////////////////////////////////////////////////////////////////
String filenamedeterminer(int filecounter, String FID){// THIS FUNCTION IS USED ONCE TO FIND THE NEXT NAME TO WRITE TO FOR THE SESSION
// if (false){
// Serial.println("In filenamedeterminer if");
// filecounter++;
// filename = filename + String(filecounter) + ".txt";}
// Serial.println("In filenamedeterminer else");
//Serial.print("testing FID: "); Serial.println(FID);
while (SD.exists(FID)) {
Serial.print(FID); Serial.print(" exists");
Serial.print(filecounter);
Serial.print(".TXT' exists: testing '");Serial.print(filename);
filecounter++;
Serial.print(filecounter);
Serial.println(".TXT'");
Serial.println();
FID = filename + String(filecounter) + ".txt";
delay(1);
}
Serial.print("WRITING TO ");
// Serial.print(FID);
filecounter = filecounter;
Serial.println(FID);
return FID;
}
HX711.cpp (1.76 KB)
HX711.h (2.22 KB)
LoadCell.ino (5.48 KB)
Datalogger.ino (3.55 KB)