Thanks for your input,
First, I thought it would be easier to attach the code as a separate file, since it's not one specific point that I'm struggling with. I'll attach it with the code tags at the end.
Lucario448:
Since everything is text and varies in length, overwritting a row (or part of it) can lead to "overlaps" or row merging (due to losing the new-line characters in the process).
If I understand you correctly, once I stop writing on any particular given line, then the rest of that row is written as 'blank'? So in order to go back and start over in a new column, i'd really be editing the previous entries? I see why that could get me into trouble.
Lucario448:
File size it's just as simple as creating another file, and storage limit... well, then get a larger card (up to 32 GB recommended, anything bigger might work but I cannot guarantee).
I'm using a 32GB SD card to write the data to. A csv with three columns and 1,048,576 rows only takes up about 30MB - so that's not the problem. When you pull the csv off the SD card and open it, only the first 1,048,576 rows are displayed (and from what I can tell this is all I'll be able to get with the code I have now - hence wanting to go back and write to different columns).
I had hoped to keep everything in one file to make my life easier, but maybe it'll be better if I try to create a new file every time one fills up. It'll be more work for me post-recording, but if that works then it works. I just wanted to make sure there were no clever solutions floating around that I hadn't thought of.
Thanks again,
// include the library code:
#include <LiquidCrystal.h> // Needed for LCD Screen
#include <SPI.h> // Needed for SD card writing
#include <SD.h> // Needed for SD card writing
// For Display
//const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // For Stock LCD Display
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7; // For LCD Display Shield
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // For All LCD Display
// Variable Declaration
const int GSS = A3; // Grove Sound Sensor
int sensorValue = 0; // Declare Sensor Value for Groove sensor
int THV = 775; // The threshold value to trigger if statement
int i = 0; // Global Counter
unsigned long currentTime = 0; // Timer
unsigned long previousTime = 0; // Timer
unsigned long Sdiff = 0; // Define short timer difference variable
// Declare File for SD card
File RawData;
//--- VOID SETUP ---
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Set bod rate
// initialize SD card
SD.begin(4); // Define SD card starting pin
RawData = SD.open("RawData.csv", FILE_WRITE);
// if the file opened okay, write to it:
if (RawData) {
lcd.clear(); // Clear LCD Display
lcd.setCursor(0,0) ; //sets cursor to first line
lcd.print("SD Card"); //Print SD Card Message
lcd.setCursor(0,1) ; //sets cursor to second line
lcd.print("Detected"); //Print SD Card Message
// Initialize file headings
RawData.println("Movement Counter, version: 1.05-Beta");
RawData.close(); // close the file:
RawData = SD.open("RawData.csv", FILE_WRITE);
RawData.println("Global Time (ms since start), Time since last reading (ms), Sound threshold (unitless)");
RawData.close(); // close the file:
}
else {
// if the file didn't open, print an error:
lcd.clear(); // Clear LCD Display
lcd.setCursor(0,0) ; //sets cursor to first line
lcd.print("SD Card Error"); // Print SD Card Message
}
} // END VOID SETUP
//--- VOID LOOP ---
void loop() {
// Module to control microphone sensor reading
sensorValue = analogRead(GSS); //use A3 to read the electrical signal
if(sensorValue > THV) {
i++; // incriment counter - serves no real purpose other than to confirm on the screen that the program is updating
currentTime = millis(); // Log the current time
Sdiff = currentTime - previousTime; // Calculate time between readings
// Display
lcd.clear(); // Clear LCD Display
lcd.setCursor(0,0) ; //sets cursor to first line
lcd.print("S:"); // Print Microphone output label
lcd.setCursor(3,0) ; //sets cursor to first line, 4th column
lcd.print(sensorValue); // Print Microphone output
lcd.setCursor(9,0) ; //sets cursor to first line, 10th column
lcd.print("V:"); // Print THV label
lcd.setCursor(12,0); //sets cursor to first line, 4th column
lcd.print(THV); // Print difference in time values
lcd.setCursor(0,1) ; //sets cursor to second line
lcd.print("t:"); // Print miliseconds label
lcd.setCursor(3,1); //sets cursor to second line, 4th column
lcd.print(Sdiff); // Print difference in time values
lcd.setCursor(9,1) ; //sets cursor to second line, 10th column
lcd.print("C:"); // Print counter label
lcd.setCursor(12,1); //sets cursor to second line, 13th column
lcd.print(i); // Print counter label
// Write to SD Card
RawData = SD.open("RawData.csv", FILE_WRITE);
RawData.print(currentTime); // Write: Global time, time difference, sound value
RawData.print(",");
RawData.print(Sdiff); // Write: Global time, time difference, sound value
RawData.print(",");
RawData.println(sensorValue); // Write: Global time, time difference, sound value
RawData.close(); // close the file:
previousTime = currentTime; // Log the current time as the previous time
// Time Management
delay(80); // Force minimum time between readings to ensure distinct signals (in miliseconds)
} // END IF STATEMENT
// Reset Counters to prevent display overrun
if(i >= 9999) {i = 0; lcd.setCursor(12,0); lcd.print(" "); } // Reset counter after sufficient iterations
// Program termination if onboard clock reaches 49 days (4233600000 ms)
if(currentTime >= 4233600000) {
lcd.clear(); // Clear LCD Display
lcd.setCursor(0,0) ; //sets cursor to first line
lcd.print("Max Time"); // Print Microphone output
lcd.setCursor(0,1) ; //sets cursor to first line
lcd.print("Reached"); // Print Microphone output
exit(0); // Terminate Program
};
} // END SCRIPT