Hey,
I've been working on a circuit that will measure the voltage of a 25V ish battery over time and then log it on a sd card, the only problem is that it only displays the voltage at 55.61V and doesn't react to anything. I have tested the circuit with another sketch and it gives the correct value, I have used almost exactly the same code spliced into this one but it just doesnt work. I'm still fairly new to this and this is my first project so as much information on what I've done wrong will be much appreciated
thanks
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#define LOG_INTERVAL 2000 // mills between entries (reduce to take more/faster data)
// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()
#define ECHO_TO_SERIAL 1 // echo data to serial port
RTC_DS1307 RTC; // define the Real Time Clock object
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
#define NUM_SAMPLES 10
int sum = 0; // sum of samples taken
unsigned char sample_count = 0; // current sample number
float voltage = 0.0; // calculated voltage
// the logging file
File logfile;
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
while (1);
}
void setup(void)
{
Serial.begin(9600);
Serial.println();
pinMode(A2, INPUT);
// initialize the SD card
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
error("Card failed, or not present");
}
Serial.println("card initialized.");
// create a new file
char filename[] = "LOGGER00.TXT";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i / 10 + '0';
filename[7] = i % 10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("datetime,voltage");
#if ECHO_TO_SERIAL
Serial.println("datetime,voltage");
#endif //ECHO_TO_SERIAL
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);
}
void loop(void)
{
DateTime now;
// delay for the amount of time we want between readings
delay(5000);
while (sample_count < NUM_SAMPLES) {
sum += analogRead(A2);
sample_count++;
delay(10);
}
voltage = ((float)sum / (float)NUM_SAMPLES * 5) / 1024.0;
// fetch the time
now = RTC.now();
// log time
// logfile.print(now.unixtime()); // seconds since 1/1/1970
// logfile.print(", ");
logfile.print('"');
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(" ");
logfile.print(voltage * 11.132, 2);
logfile.print(" V");
logfile.println('"');
#if ECHO_TO_SERIAL
// Serial.print(now.unixtime()); // seconds since 1/1/1970
// Serial.print(", ");
Serial.print('"');
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(" ");
Serial.print(voltage * 11.132, 2);
Serial.print(" V");
Serial.println('"');
#endif //ECHO_TO_SERIAL
sample_count = 0;
sum = 0;
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
// if ((millis() - syncTime) < SYNC_INTERVAL) return;
// syncTime = millis();
// blink LED to show we are syncing data to the card & updating FAT!
logfile.flush();
}