// Include necessary libraries for SD card and HX711
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include "Adafruit_HX711.h"
// Define SD card pins
#define SD_MOSI 23
#define SD_MISO 19
#define SD_SCLK 18
#define SD_CS 5
// Define other hardware pins
#define LED_PIN 2
#define SWITCH_PIN 32
// Define HX711 pins
#define HX711_DATA_PIN 26
#define HX711_CLOCK_PIN 27
Adafruit_HX711 hx711(HX711_DATA_PIN, HX711_CLOCK_PIN); // HX711 object
int filenumber = 0; // To track file names for each session
File myFile; // File object to manage SD card files
bool isWriting = false; // Flag to check if data is being written
void setup() {
// Serial communication initialization
Serial.begin(115200);
// Set LED and switch pins
pinMode(LED_PIN, OUTPUT);
pinMode(SWITCH_PIN, INPUT);
// Wait for serial connection
while (!Serial) {
delay(10);
}
Serial.println("Setup start");
// Initialize SPI for SD card
SPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
// Mount SD card
if (!SD.begin(SD_CS)) {
Serial.println("SD Card MOUNT FAIL");
} else {
Serial.println("SD Card MOUNT SUCCESS");
digitalWrite(LED_PIN, HIGH); // Turn on LED when SD card is mounted
Serial.println("");
delay(100);
uint32_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.println("SDCard Size: " + String(cardSize) + "MB");
}
// Initialize HX711 for strain gauge
hx711.begin();
// Tare the scale (zero out the readings)
Serial.println("Tareing...");
for (uint8_t t = 0; t < 3; t++) {
hx711.tareA(hx711.readChannelRaw(CHAN_A_GAIN_128));
hx711.tareB(hx711.readChannelRaw(CHAN_B_GAIN_32));
hx711.tareB(hx711.readChannelRaw(CHAN_B_GAIN_32));
hx711.tareB(hx711.readChannelRaw(CHAN_B_GAIN_32));
}
Serial.println("Setup complete");
pinMode(SWITCH_PIN, INPUT);
}
void loop() {
// Check the state of the switch
int switchState = digitalRead(SWITCH_PIN);
// Start writing data to SD card if switch is pressed
if (switchState == HIGH) {
if (!isWriting) {
// Generate a new file name for each session
String fileName = "data" + String(filenumber) + ".csv";
//char fileName[15];
//sprintf(fileName, "data%d.csv", filenumber);
Serial.println("Attempting to create a new file...");
delay(100);
myFile = SD.open(fileName, FILE_WRITE);
if (myFile) {
Serial.println("Started writing to " + fileName);
isWriting = true;
} else {
Serial.println("Error opening " + fileName);
}
}
// Poll strain gauge data and write to the SD card
if (isWriting) {
// Read strain data from HX711 (Channel A with Gain 128)
int32_t strainValueA = hx711.readChannelBlocking(CHAN_A_GAIN_128);
int32_t strainValueB = hx711.readChannelBlocking(CHAN_B_GAIN_32);
// Log strain data to the SD card
myFile.print("Strain A (Gain 128): ");
myFile.print(strainValueA);
myFile.print(", Strain B (Gain 32): ");
myFile.println(strainValueB);
// Print data to serial monitor for debugging
Serial.print("Strain A (Gain 128): ");
Serial.print(strainValueA);
Serial.print(", Strain B (Gain 32): ");
Serial.println(strainValueB);
delay(50); // Delay to control logging rate
}
} else {
// Stop writing if the switch is released
if (isWriting) {
myFile.close(); // Close the file
Serial.println("File saved and closed.");
filenumber++; // Increment file number for next session
isWriting = false; // Reset writing flag
}
}
delay(100); // Short delay for stability in the loop
}