0

I've created a sensor that measures the color of the external cerebrospinal fluid drain using TCS3200. I'm able to get readings, but I'm unable to save them on the SD card, yet I'm very much able to save using the ReadWrite example. This led me to believe the issue isn't hardware-related. I'm possibly missing a very obvious thing(Just like I've overlooked the division by zero and lost roughly 6 hours figuring it out).

I'm using an Arduino Micro clone. TCS3200 and a microSD module.

Here's my code:

    #include <SPI.h>
#include <SD.h>

// Define pins connected to the TCS3200 sensor
#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define sensorOut 6

// Calibration values for clear CSF readings (TO-DO: Calibrate these)
int baselineClearPeriodCount = 1; // Initialize to 1 to prevent division by zero (oops.)
int baselineRedPeriodCount = 1;   
int baselineGreenPeriodCount = 1; 
int baselineBluePeriodCount = 1; 

// Target count to be achieved within the dynamic time adjustment
const int targetCount = 360; 

// Timeout setting for the pulseIn() function (microseconds)
const int pulseTimeout = 2000; 

// Specifies the chip select pin for the SD card module
const int chipSelect = 10;  

// Variables for interacting with the SD card
File sensorDataFile;
String dataString = ""; 

// Function Prototypes
void setupSensorPins();
void readColor(int &colorCount, int S2State, int S3State);

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  // Give time for the serial monitor to open
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Serial communication initialized.");

  // Set up the sensor pins
  setupSensorPins();
  Serial.println("Sensor pins setup completed.");

  // Initialize SD card
  Serial.print("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("SD card initialization failed!");
    while (1);
  }
  Serial.println("Initialization done.");

  
}

void loop() {
  // Debug statement to indicate start of loop
  Serial.println("Starting loop...");

  // Variables to store counts for each color and clear reading
  int redPeriodCount = 0;
  int greenPeriodCount = 0;
  int bluePeriodCount = 0;
  int clearPeriodCount = 0;

  // Read Red, Green, Blue, and Clear colors
  readColor(redPeriodCount, LOW, LOW);
  readColor(greenPeriodCount, HIGH, HIGH);
  readColor(bluePeriodCount, LOW, HIGH);
  readColor(clearPeriodCount, HIGH, LOW);

  // Calculate and log data
  int turbidityReductionPercent = (baselineClearPeriodCount - clearPeriodCount) * 100 / baselineClearPeriodCount;
  int redChangePercent = (redPeriodCount - baselineRedPeriodCount) * 100 / baselineRedPeriodCount;
  int greenChangePercent = (greenPeriodCount - baselineGreenPeriodCount) * 100 / baselineGreenPeriodCount;
  int blueChangePercent = (bluePeriodCount - baselineBluePeriodCount) * 100 / baselineBluePeriodCount;

  // Build the data string
  dataString  = String(redPeriodCount) + "," + String(greenPeriodCount) + "," + 
                String(bluePeriodCount) + "," + String(clearPeriodCount) + "," + 
                String(turbidityReductionPercent) + "," + 
                String(redChangePercent) + "," + String(greenChangePercent) + "," +
                String(blueChangePercent) + "%\n"; 

  // Debug statement to print the data string
  Serial.println("Data String: " + dataString);

  // Log data to SD card
  sensorDataFile = SD.open("sensordata.txt", FILE_WRITE);
  
  if (sensorDataFile) {
    sensorDataFile.println(dataString);
    sensorDataFile.close();
    Serial.println("Text file write complete");
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("Error opening text file.");
  }

  // Short delay before the next loop iteration
  delay(1000);
}

void setupSensorPins() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);
}

void readColor(int &colorCount, int S2State, int S3State) {
  // Setting frequency scaling to 20%
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);

  digitalWrite(S2, S2State);
  digitalWrite(S3, S3State);
  unsigned long startTime = millis();
  colorCount = 0;
  int dynamicMeasurementTime = 50;
  unsigned long currentTime = millis();

  while (currentTime - startTime < dynamicMeasurementTime) {
    int pulseWidth = pulseIn(sensorOut, LOW, pulseTimeout);
    if (pulseWidth > 0) {
      colorCount++;
      if (colorCount >= targetCount) {
        dynamicMeasurementTime = currentTime - startTime;
        break;
      }
    }
    currentTime = millis();
  }

  // Debug statement to print color count
  //Serial.print("Color count (S2: ");
  //Serial.print(S2State);
  //Serial.print(", S3: ");
  //Serial.println(S3State);
  //Serial.print("): ");
  //Serial.println(colorCount);
}

The monitor reads:

Starting loop...
Data String: 0,0,0,1,0,-100,-100,-100%
Error opening text file.

I've also tried manually adding an empty txt file which also didn't work.

4
  • 1
    Try shortening the name of the file to 8 or less characters left of the dot.
    – Delta_G
    Commented Jul 6 at 22:49
  • That fixed it... I suppose that's what "short 8.3 names" mean. I feel like it should be more explicitly said on the Arduino documentation. Thank you! Commented Jul 7 at 7:13
  • can it be more explicit than it is? arduino.cc/reference/en/libraries/sd/#usage
    – Juraj
    Commented Jul 9 at 15:44
  • @Juraj If you know what "short 8.3 names" mean, of course. But considering that many people using Arduino are either hobbyists or people from other departments, it might be overlooked. I was even surprised to learn that it is limited as such, with a device so capable... Commented Jul 9 at 18:10

1 Answer 1

0

Because "short 8.3 names" means less than 8 characters on file names and you cannot make file names longer than 8 characters in order to fit that naming convention. Thanks @Delta_G

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.