Just a wild guess here: I see you are creating a new File instance each time logData() runs. I would be interested to see what would happen if you created one global File instance and re-used it, in case there's something "a bit leaky" going on?
I'd put in a third vote here for SdFat too.
Libraries typically come with example sketches showing how the library is used in coding. They are often helpful in making sure everything is hooked up correctly. I don't know about V2 of the IDE, but in V1.8 they can be found at Files/Examples/SD [or whatever library].
In that case you don't need to bother with the examples.
Ok I updated to use the SDFat library instead of SD. I've noticed something I dont understand occuring. When I have my laptop hooked up and I'm running serial monitor, it is printing in monitor that its logging data and I have data recorded to the SD card. When I disconnect my laptop and and run the program I'm not getting a saved CSV file.
#include <Arduino.h>
#include <SdFat.h>
SdFat SD; // Initialize the SdFat library
const int relayPin = 8; // Relay for Control Solenoid
const int ExtPin = A3; // Extension Switch Signal
const int RetPin = A1; // Retraction Switch Signal (home)
const int StSpPin = 7; // Start Stop Button
const int chipSelect = 10; // SD Module CS line
bool isRunning = false; // Flag system running
bool stopRequested = false; // Flag stop request
unsigned long startTime; // Start time
unsigned long extensionTime; // Cylinder extension time
unsigned long retractionTime; // Cylinder retraction time
enum CylinderState { IDLE, EXTENDING, RETRACTING, RETURN_TO_HOME };
CylinderState state = IDLE; // Initial state set to idle
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(ExtPin, INPUT);
pinMode(RetPin, INPUT);
pinMode(StSpPin, INPUT_PULLUP);
Serial.begin(9600);
if (!SD.begin(chipSelect)) {
Serial.println("SD Card Missing or Error");
isRunning = false;
while (1); // Stop if SD card missing or issue
}
Serial.println("System Ready");
}
void loop() {
if (digitalRead(StSpPin) == LOW) {
delay(500); // Debounce delay
if (isRunning) {
stopRequested = true; // Set stop flag if system is running
Serial.println("Stop Requested");
} else {
isRunning = true; // Start system if not running
Serial.println("Starting System");
state = digitalRead(RetPin) == HIGH ? IDLE : RETURN_TO_HOME; // Check if cylinder is at home position
delay(2000); // Delay before starting cycle
}
while(digitalRead(StSpPin) == LOW); // Wait for button release
}
if (isRunning) {
switch (state) {
case RETURN_TO_HOME:
Serial.println("Returning to Home");
if (digitalRead(RetPin) == HIGH) {
state = IDLE; // Switch to idle if cylinder is at home position
Serial.println("Reached Home: IDLE State");
}
break;
case IDLE:
if (stopRequested) {
isRunning = false; // Stop
stopRequested = false;
Serial.println("System Stopped");
} else {
state = EXTENDING; // Switch to extending state
digitalWrite(relayPin, HIGH); // Activate solenoid
Serial.println("Extending Cylinder");
startTime = millis(); // Record start time of extension
}
break;
case EXTENDING:
if (digitalRead(ExtPin) == HIGH) {
extensionTime = millis() - startTime; // Calculate extension time
Serial.print("Extension Time: ");
Serial.println(extensionTime);
delay(2000); // Pause between cycles
state = RETRACTING; // Switch to retracting state
digitalWrite(relayPin, LOW); // Deactivate solenoid
Serial.println("Retracting Cylinder");
startTime = millis(); // Record start time of retraction
}
break;
case RETRACTING:
if (digitalRead(RetPin) == HIGH) {
retractionTime = millis() - startTime; // Calculate retraction time
Serial.print("Retraction Time: ");
Serial.println(retractionTime);
logData(extensionTime, retractionTime); // Log data
delay(2000); // Pause between cycles
state = stopRequested ? RETURN_TO_HOME : IDLE;
if (stopRequested) {
Serial.println("Returning to Home after Stop Request");
}
}
break;
}
} else {
digitalWrite(relayPin, LOW); // Relay off when not running
if (state != IDLE) {
Serial.println("System Idle - Cylinder Stopped");
}
}
}
void logData(unsigned long extTime, unsigned long retrTime) {
SdFile dataFile; // Use SdFile type for file operations
if (dataFile.open("datalog.csv", O_CREAT | O_WRITE | O_AT_END)) {
dataFile.print(millis());
dataFile.print(",");
dataFile.print(extTime);
dataFile.print(",");
dataFile.println(retrTime);
dataFile.close();
Serial.println("Data Logged to SD Card");
} else {
Serial.println("Error opening datalog.csv");
}
}
What does your power circuit look like?
I can't read that.
what are the modules, do you have resistors, what are the pin numbers... ? what do you mean by pullup or pull down?
Don't make it look like the real thing, just rectangles with labels is much easer to read
Do you get error messages in the serial monitor, such as
"Error opening datalog.csv"?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
