Interfacing piezoelectric sensor to sd card module

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

const int chipSelect = 4;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

Serial.print("Initializing SD card...");

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
}

void loop() {
// make a string for assembling the data to log:
String dataString = "";

// read three sensors and append to the string:
int value = analogRead(A1);
Serial.println(value);
for (int a = 1; a <=5; a++) {
if (value > a*20) {
digitalWrite(a+1, HIGH);
} else {
digitalWrite(a+1, LOW);
}
}
if (value >= 0) {
dataString += "";
}

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.

File dataFile = SD.open("datalog.txt", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}

This is my code and it is getting compiled and uploaded successfully but the sd card when inserted into the laptop shows a blank text file. How to resolve this?

Welcome to the worldbest Arduino forum ever.

Read this first:

What did you expect to see in the file? Look at your code, the only thing that you stored in the file is a empty string:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.