Hello,
I am pretty new to C++ and Arduino. I am attempting to record force from an fsr sensor and timestamps for each point of data. I want to be able to record a large amount of this data, say a million points and save it to an sd card so that I can analyze it later.
The way I have the code setup right now I am running into SRAM capacity limitations. Any recommendations would be appreciated on how I can fix this, if there are other ways of handling this data I am open to changing the way I have to sketch setup. It is important that I keep the write speed high, between 10ms and 1ms ideally. The system needs to standalone, aka it will not be connected to a PC while recording this data.
Hardware:
Arduino Uno r3
int fsrPin = 0; // the FSR and 10K pulldown are connected to a0
int fsrReading; // the analog reading from the FSR resistor divider
int fsrVoltage; // the analog reading converted to voltage
unsigned long fsrResistance; // The voltage converted to resistance, can be very big so make "long"
unsigned long fsrConductance;
long fsrForce; // Finally, the resistance converted to force
const int MAX_READINGS = 10000; // Maximum number of readings
int TAKE_READINGS = 10; // First time readings are stored
long readings[2][MAX_READINGS]; // Stores timestamps and forces
int j = 0; // Index for the next reading
unsigned long timestamp; // Get current time in milliseconds
const int BUTTON_PIN = 8; // Button pin
bool recording = true; // State to track if we are recording
bool lastButtonState = HIGH; // Assume button starts in not-pressed state (using internal pull-up)
bool pressed = false;
#include <SD.h>
void setup(void) {
Serial.begin(115200); // We'll send debugging information via the Serial monitor
// setup pin modes
pinMode (BUTTON_PIN, INPUT_PULLUP);
}
void writeToSD(int j, long readings[2][MAX_READINGS]) {
File dataFile = SD.open("results_" + String(j) + ".txt", FILE_WRITE);
if (dataFile) {
int i = j - TAKE_READINGS;
if (i >= j){
// dataFile.print("Timestamp:");
dataFile.print(readings[0][i]);
dataFile.println(", ");
// dataFile.print("FSR Force:");
dataFile.println(readings[1][i]);
dataFile.close();
i++;
}
Serial.println("Data written to SD card.");
} else {
Serial.println("Error opening datalog.txt");
}
}
void loop(void) {
// bool currentButtonState = digitalRead(BUTTON_PIN);
bool currentState = digitalRead(BUTTON_PIN);
// Check for button press (transition from HIGH to LOW)
// if (currentState == pressed) {
// // if (lastButtonState == HIGH && currentButtonState == LOW) {
// recording = !recording; // Toggle recording state
// delay(50); // Debounce delay
// }
// lastButtonState = currentButtonState; // Update the last button state
// recording = true;
if (recording) {
if (j == TAKE_READINGS) {
// Save to SD card
Serial.println("writing data block " + String(TAKE_READINGS));
// writeToSD(TAKE_READINGS, readings);
TAKE_READINGS = TAKE_READINGS + 10;
if (j >= MAX_READINGS) {
// Reached the maximum number of readings, potentially stop recording or handle as needed
Serial.println("Maximum readings reached.");
recording = false; // Optionally stop recording
return;
}
}
fsrReading = analogRead(fsrPin);
fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
timestamp = millis();
if (fsrVoltage != 0) {
fsrResistance = 5000 - fsrVoltage; // Calculate resistance
fsrResistance *= 10000;
fsrResistance /= fsrVoltage;
fsrConductance = 1000000;
fsrConductance /= fsrResistance;
// Approximate the force
if (fsrConductance <= 1000) {
fsrForce = fsrConductance / 80;
} else {
fsrForce = fsrConductance - 1000;
fsrForce /= 30;
}
}
else {
fsrForce = 0;
}
// Store the timestamp and force in the readings array
readings[0][j] = timestamp;
readings[1][j] = fsrForce;
// Print current reading for debugging
Serial.print("Timestamp: ");
Serial.println(timestamp);
Serial.print("FSR Force: ");
Serial.println(fsrForce);
j++; // Increment index for the next reading
delay(10); // Sample delay
}
}
Thanks for any help!