Hi,
I've got multiple problems with writing to an SD card for my dynamo meter project.
Problem number 1:
For some reason it will not write the RTC data to the SD card. I can either get all the other header lines or the RTC data but not both. It shows up correctly on the Serial but will not write to the file.
Problem number 2:
The SD card will not create files correctly if i uncomment the code required to read the BMP180 barometric pressure sensor.
Any help would be appreciated. I've tried several different writing methods for problem 1 and nothing seems to work. The second problem has me completely stumped. I hope im doing something stupid because this is really annoying the hell out of me for some really simple problems.
//////////////////////
// Library Setup
//////////////////////
#include <DHT22.h>
#include "HX711.h"
#include <SPI.h>
#include <Wire.h>
#include <SD.h>
#include "RTClib.h"
RTC_DS1307 rtc;
#include <SFE_BMP180.h>
SFE_BMP180 pressure;
//////////////////////
// Varaiable Declerations
//////////////////////
unsigned int count; //Used for rpm loop
boolean runn = false; //Used to start and stop datalogging
boolean Cont = true;
int datapoints = 10; //Defines the amount of points for RPM
unsigned int datafilenumber = 0; //Used for inital file setup
String datastring, datafile; //Used to write data
long period,timeold,timenew; //Used for RPM measurment
boolean statenew,stateold; //Used for RPM measurment
float rpm;
int load;
File myFile;
// For RTC
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//////////////////////
// Pin Declerations
//////////////////////
// SD Card
const int chipSelect = 10;
// RPM
const int slottedswitchpin = 2;
// Start logic
const int led_pin = 7;
const int switchpin = 6;
const int speakerpin = 5;
// Temperature and humidty
const int DHT22_PIN = 3;
DHT22 myDHT22(DHT22_PIN);
// Load Cell
HX711 scale(A0,A1);
//////////////////////
// Setup Loop
//////////////////////
void setup() {
// put your setup code here, to run once:
pinMode(switchpin, INPUT);
pinMode(led_pin, OUTPUT);
pinMode(speakerpin, OUTPUT);
speaker();
Serial.begin(9600);
Serial.println("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:
return;
}
Serial.println("card initialized.");
//Create new rpm file
datafilenumber = 0;
do
{
// Increment through datafilenumbers
datafilenumber ++;
// make data log file name
datafile = "LOG" + String(datafilenumber) + ".csv";
// Check to see if the file exists:
if (SD.exists(datafile))
{
}
else
{
Serial.println("Creating datafile...");
myFile = SD.open(datafile, FILE_WRITE);
myFile.close();
Cont = false;
delay(500);
Serial.println(datafile);
}
}
while (Cont);
// Pressure sensor setup
if (pressure.begin())
{
Serial.println("BMP180 init success");
}
else
{
Serial.println("BMP180 init fail (disconnected?)\n\n");
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
Serial.println("Setup complete");
}
void loop()
{
if (runn == true)
{
// Resetting values
count = 0;
period = 0;
do
{
statenew = digitalRead(slottedswitchpin);
timenew = micros();
if (statenew == 1 & stateold == 0)
{
period += (timenew - timeold);
count ++;
timeold = timenew;
}
stateold = statenew;
}while (count < datapoints);
rpm = (1.0 / ((period/1000000.0) / count)) * 60.0;
// Grab loadcell data
datastring = String(millis()*1000) + " , " + String(rpm) + "," + String(scale.read_average(5));
write_data();
digitalWrite(led_pin,LOW);
delay(100);
digitalWrite(led_pin,HIGH);
if (digitalRead(switchpin) == HIGH)
{
speaker();
Serial.println("End of datalogging");
//Stop Datalogging and write last lines and header
header();
datastring = "End of Data";
write_data();
runn = false;
digitalWrite(led_pin,LOW);
pinMode(switchpin, OUTPUT);
delay(5000);
pinMode(switchpin, INPUT);
}
}
else
{
if (digitalRead(switchpin) == HIGH)
{
speaker();
Serial.println("Start of datalogging");
digitalWrite(led_pin,HIGH); //Turn on LED to indicate start of datalogging
// Reading Temperature, Humidity and Barometric Pressure for Dyno Correction Factor.
delay(5000);
//Writing Datafile header to SD card
header();
//Blink of LED to indicate correct setup
digitalWrite(led_pin,LOW);
delay(1000);
digitalWrite(led_pin,HIGH);
runn = true;
}
}
}
void write_data()
{
File dataFile = SD.open(datafile, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(datastring);
dataFile.close();
}
Serial.println(datastring);
delay(500);
}
//double getPressure()
//{
// char status2;
// double T,P,p0,a;
//
// // You must first get a temperature measurement to perform a pressure reading.
//
// // Start a temperature measurement:
// // If request is successful, the number of ms to wait is returned.
// // If request is unsuccessful, 0 is returned.
//
// status2 = pressure.startTemperature();
// if (status2 != 0)
// {
// // Wait for the measurement to complete:
//
// delay(status2);
//
// // Retrieve the completed temperature measurement:
// // Note that the measurement is stored in the variable T.
// // Use '&T' to provide the address of T to the function.
// // Function returns 1 if successful, 0 if failure.
//
// status2 = pressure.getTemperature(T);
// if (status2 != 0)
// {
// // Start a pressure measurement:
// // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
// // If request is successful, the number of ms to wait is returned.
// // If request is unsuccessful, 0 is returned.
//
// status2 = pressure.startPressure(3);
// if (status2 != 0)
// {
// // Wait for the measurement to complete:
// delay(status2);
//
// // Retrieve the completed pressure measurement:
// // Note that the measurement is stored in the variable P.
// // Use '&P' to provide the address of P.
// // Note also that the function requires the previous temperature measurement (T).
// // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
// // Function returns 1 if successful, 0 if failure.
//
// status2 = pressure.getPressure(P,T);
// if (status2 != 0)
// {
// return(P);
// }
// else Serial.println("error retrieving pressure measurement\n");
// }
// else Serial.println("error starting pressure measurement\n");
// }
// else Serial.println("error retrieving temperature measurement\n");
// }
// else Serial.println("error starting temperature measurement\n");
//}
void Date()
{
DateTime now = rtc.now();
char message[120];
int Year = now.year();
int Month = now.month();
int Day = now.day();
int Hour = now.hour();
int Minute = now.minute();
int Second = now.second();
sprintf(message, "%d/%d/%d %02d:%02d:%02d", Year,Month,Day,Hour,Minute,Second);
File dataFile = SD.open(datafile, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(message);
dataFile.close();
}
Serial.println(message);
}
void header()
{
datastring = "--------------------";
write_data();
datastring = "Version 1.0" ;
write_data();
Date();
myDHT22.readData();
datastring = "Temperature: " + String(myDHT22.getTemperatureC()) + "C";
write_data();
datastring = "Humidity: " + String(myDHT22.getHumidity()) + "%";
write_data();
// datastring = "Barometric Pressure:" + String(getPressure());
// write_data();
datastring = "--------------------";
write_data();
}
void speaker()
{
tone(speakerpin, 5000, 600);
}