I'm running into problems writing to a file on my SD card.
I'm using the datalogger shield and SdFat16Lib, card is properly formatted (SD Tool), and tested with the SdFat16 demo sketches.
Now, I'm pretty sure It's my code that's doing bogus, but I'm at my wit's end. Could you give me a hint? - the actual SD writing happens in the last functional block::
Code: Select all
#include <SdFat.h>
#include <Wire.h>
#include "RTClib.h"
/* Rotary encoder pin setup */
#define ENC_A 14
#define ENC_B 15
#define ENC_PORT PINC
#define RELAY 8
/* Initialize variables */
RTC_DS1307 RTC;
SdFat sd;
SdFile myFile;
const int chipSelect = 10;
int waterings = 0;
long watertime = 0;
void setup()
{
/* Setup encoder pins as inputs */
pinMode(ENC_A, INPUT);
digitalWrite(ENC_A, HIGH);
pinMode(ENC_B, INPUT);
digitalWrite(ENC_B, HIGH);
/* Setup water relay to off */
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, LOW);
/* Set CardSelect pin as Output, reqd. by SD.h */
pinMode(10, OUTPUT);
/* Initialize Serial, Real Time Clock, I2C Communications */
Serial.begin (115200);
Wire.begin();
RTC.begin();
/* Set RTC to current CPU time */
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
/*Dump init*/
Serial.println("Polyp v.01");
}
void loop()
{
DateTime now = RTC.now();
static long counter = 0; //this variable will be changed by encoder input
int8_t tmpdata;
tmpdata = read_encoder();
if( tmpdata ) {
Serial.print("Counter value: ");
Serial.println(counter, DEC);
counter += tmpdata;
if ( abs(counter) >= 55 )
{
/* Setup watering time */
watertime = analogRead(2);
watertime = map(watertime, 0, 1023, 1000, 12000);
/* Run pump for duration of watering time */
digitalWrite(RELAY, HIGH);
/* Debug */
Serial.println("Start Water");
Serial.print("Wait ");
Serial.println(watertime);
/* Debug */
delay(watertime);
Serial.println("Stop Water"); // Debug
digitalWrite(RELAY, LOW);
counter = 0; // Zero out counted knob turns
waterings++; // Adds to the watering counter
Serial.print("Today watered: ");
Serial.print(waterings);
Serial.println("time(s).");
Serial.println("Logging to SD >>");
logaction(2);
}
}
}
/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
{
static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
static uint8_t old_AB = 0;
/**/
old_AB <<= 2; //remember previous state
old_AB |= ( ENC_PORT & 0x03 ); //add current state
return ( enc_states[( old_AB & 0x0f )]);
}
/* Log time and type of interaction: 1 = light; 2 = water */
void logaction(int type)
{
Serial.println("Function called");
if (type == 1)
{
Serial.println("Type 1");
insertdate();
myFile.print("0");
myFile.print(",");
myFile.print("1");
myFile.println();
myFile.close();
}
if (type == 2)
{
Serial.println("Type 2");
insertdate();
myFile.print("1");
myFile.print(",");
myFile.print("0");
myFile.println();
myFile.close();
}
}
/* Write date to CSV row */
void insertdate()
{
Serial.println("Inserting date.");
DateTime now = RTC.now();
!myFile.open("thelog.csv", O_RDWR | O_CREAT | O_AT_END);
myFile.print(now.day(), DEC);
myFile.print('.');
myFile.print(now.month(), DEC);
myFile.print('.');
myFile.print(now.year(), DEC);
myFile.print('.');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
myFile.print(',');
// myFile.close();
}
-Whiz