Hi guys, I'm trying to make a data logger. I've managed to partially make a datalogger now but my problem is that I can't start and end the recording based on a character read from the serial monitor. Any character is ok but I can't figure out how to assign it to a specific character. When the character "A" is entered, it will start recording and when the character "B" is entered, it will end the recording, but after the recording is finished, I also want the loop to return to the beginning so that the recording can be started again.
I'm trying to create a project, but after coming to a certain place, I was left without a solution, I'm still trying to read and solve the problem, if anyone can help me, I would be very happy.
I'm sorry I added the code sample as below because I'm a new member.
Every opinion given is respected.
/*
Simple data logger.
*/
#include <SPI.h>
#include "SdFat.h"
#include "RTClib.h"
#include "HX711.h"//XFW-HX711 amplifier 80Hz
#define calibration_factor -7090.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
int LOADCELL_DOUT_PIN = 7;
int LOADCELL_SCK_PIN = 6;
int LOADCELL_DOUT_PIN_1 = 5;
int LOADCELL_SCK_PIN_1 = 4;
int LOADCELL_DOUT_PIN_2 = 3;
int LOADCELL_SCK_PIN_2 = 2;
HX711 scale;
HX711 scale1;
HX711 scale2;
RTC_DS1307 RTC;//using RTClib
DateTime now;
const uint8_t chipSelect = 10;// SD chip select pin. Be sure to disable any other SPI devices such as Enet.
// Interval between data records in milliseconds.
// The interval must be greater than the maximum SD write latency plus the
// time to acquire and write data to the SD to avoid overrun errors.
// Run the bench example to check the quality of your SD card.
const uint32_t SAMPLE_INTERVAL_MS = 1;//time
// Log file base name. Must be six characters or less
#define FILE_BASE_NAME "LOGGG"
//------------------------------------------------------------------------------
// File system object.=
SdFat sd;
SdFile file;// Log file.
uint32_t logTime;
//==============================================================================
// User functions. Edit writeHeader() and logData() for your requirements.=
int Date_count = 1;
int Time_count = 1;
int Load_Cell1 = 1;
int Load_Cell2 = 1;
int Load_Cell3 = 1;
//------------------------------------------------------------------------------
void load_cell () {
Serial.print("Reading: ");
Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
// Serial.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor
Serial.print("\t");
Serial.print(scale1.get_units(), 1);
Serial.print("\t");
Serial.print(scale2.get_units(), 1);
Serial.print(" kg");
Serial.println();
}
void writeHeader() {
file.print(F("Time(us)"));
for (int c = 0; c < Load_Cell1; c++) {
file.print(F(",lCell"));
//file.print(c, DEC);
} for (int d = 0; d < Load_Cell2; d++) {
file.print(F(",2Cell"));
//file.print(d, DEC);
} for (int e = 0; e < Load_Cell3; e++) {
file.print(F(",3Cell"));
//file.print(e, DEC);
}
for (int a = 0; a < Date_count; a++) {
now = RTC.now();
file.print(F(",date:"));
file.print(now.day(), DEC);
file.print('/');
file.print(now.month(), DEC);
file.print('/');
file.print(now.year(), DEC);
} for (int b = 0; b < Time_count; b++) {
file.print(F(",Time(second)"));
}
file.println();
}
//------------------------------------------------------------------------------
// Log a data record.
void logData() {
// Read all channels to avoid SD write latency between readings.
file.print(logTime);// Write ADC data to CSV record.
for (int c = 0; c < Load_Cell1; c++) {
file.write(',');
file.print(scale.get_units());
} for (int d = 0; d < Load_Cell2; d++) {
file.write(',');
file.print(scale1.get_units());
} for (int e = 0; e < Load_Cell3; e++) {
file.write(',');
file.print(scale2.get_units());//file.print(scale2.get_units(),DEC);
}
for (int a = 0; a < Date_count; a++) {
file.write(',');
//file.print("test");
} for (int b = 0; b < Time_count; b++) {
file.write(',');
file.print(now.hour(), DEC);
file.print(":");
file.print(now.minute(), DEC);
file.print(":");
file.print(now.second(), DEC);
file.print(":");
file.print(micros(), DEC);
}
file.println();
}
//==============================================================================
// Error messages stored in flash.
#define error(msg) sd.errorHalt(F(msg))
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);// Wait for USB Serial
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale1.begin(LOADCELL_DOUT_PIN_1, LOADCELL_SCK_PIN_1);
scale2.begin(LOADCELL_DOUT_PIN_2, LOADCELL_SCK_PIN_2);
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
scale1.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale1.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
scale2.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale2.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
RTC.begin();
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[13] = FILE_BASE_NAME "00.csv";
while (!Serial) {
SysCall::yield();
}
delay(100);
Serial.println(F("enter any character to start"));
while (!Serial.available()) {
load_cell ();
SysCall::yield();
}
// Initialize at the highest speed supported by the board that is. not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
sd.initErrorHalt();
}
// Find an unused file name.
if (BASE_NAME_SIZE > 7) {
error("FILE_BASE_NAME too long");
}
while (sd.exists(fileName)) {
if (fileName[BASE_NAME_SIZE + 1] != '9') {
fileName[BASE_NAME_SIZE + 1]++;
} else if (fileName[BASE_NAME_SIZE] != '9') {
fileName[BASE_NAME_SIZE + 1] = '0';
fileName[BASE_NAME_SIZE]++;
} else {
error("Could not create file name");
}
}
if (!file.open(fileName, O_WRONLY | O_CREAT | O_EXCL)) {
error("File.open");
}
do {
delay(100);
} while (Serial.available() && Serial.read() >= 0);
Serial.print(F("Recording: "));
Serial.println(fileName);
Serial.println(F("enter any character to stop"));
// Write data header.//
writeHeader();
// Start on a multiple of the sample interval.
//logTime = millis()/(1UL*SAMPLE_INTERVAL_MS) + 1;
//logTime *= 1UL*SAMPLE_INTERVAL_MS;
}
//------------------------------------------------------------------------------
void loop() {
logTime += SAMPLE_INTERVAL_MS;
logData();
// Force data to SD and update the directory entry to avoid data loss.
if (!file.sync() || file.getWriteError()) {
error("error write");
}
if (Serial.available()) {
file.close();
Serial.println(F("done"));
SysCall::halt();
}
}