Hi I'm new to the world of arduinos and coding and I'm having issues with data logging on my mega, I've followed this tutoial https://www.arduino.cc/en/Tutorial/ReadWrite and all works as it should.
I've then found a tutorial on youtube with recording data from a LDR Arduino Tutorial 5.2 - Data Logging - YouTube
I'm having issues in this part of my code where its going into the else statement
myFile = SD.open("SensorVal.csv", FILE_WRITE);
if (myFile)
{
Serial.print("val");
myFile.print(val);
myFile.print(",");
Serial.print(",");
myFile.println(millis());
Serial.println(millis());
myFile.close();
}
else
{
Serial.println("Error");
}
Heres the full code and any help will be brilliant. Thank you in advance
#include <SPI.h>
#include <SD.h>
File myFile;
int pinCS = 53;
int LDR = A0;
int val = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode (pinCS, OUTPUT);
pinMode (LDR, INPUT);
Serial.println("Initializing SD card");
if (SD.begin())
{
Serial.println("Initialization Complete");
}
else
{
Serial.println("Initialization Failed");
}
}
void loop() {
// put your main code here, to run repeatedly:
val = analogRead(LDR);
myFile = SD.open("SensorVal.csv", FILE_WRITE);
if (myFile)
{
Serial.print("val");
myFile.print(val);
myFile.print(",");
Serial.print(",");
myFile.println(millis());
Serial.println(millis());
myFile.close();
}
else
{
Serial.println("Error");
}
delay (1000);
}