Hi all! I am building a coin counting system utilizing a 16x2 LCD shield and SeedStudio SD Card shield on the Mega 2560. The SD is used for both data logging and retention. I am writing values onto the SD card into a .txt file. Is it possible to use these files to begin the count at the last written number? For clarification, if the count is at 100 and the power goes out, I need the count to start at 100 again after power is lost.
Included is a shortened version of the code.
Thanks for any help!
#include <SPI.h>
#include <SD.h>
int dataString = "";
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#include "Timer.h"
Timer t;
const int input_12 = 41;
int last_in_12_state = 0;
const int chipSelect = 4;
int count = 0;
int coin = 0;
int hourly_count = 0;
int sensorPin = A0;
int Update = 0;
pinMode (input_12, INPUT);
pinMode (chipSelect, OUTPUT); pinMode (53, OUTPUT);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {}
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");
return;}
Serial.println("card initialized.");
// set up inputs as inputs:
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.clear();
t.every(10*60*1000*6,UpdateResults); //update every 60 min
// t.every(10000,UpdateResults); //update every 10 Sec
lcd.setCursor(0,0);
lcd.print("ACI");
lcd.setCursor(0,1);
lcd.print("NOTE TIME");
delay(5000);
lcd.clear();
}
void loop() {
dataString = int(count);
dataString = int(hourly_count);
in_state_12 = digitalRead(input_12);
if (in_state_12 != last_in_12_state){
if (in_state_12 == LOW){
count ++;
}}
last_in_12_state = in_state_12;
coin = (count/4);
t.update();
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print("Hour:");dataFile.print(hourly_count);dataFile.print(";");
dataFile.print("Count:");dataFile.println(count);
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}