Read the last stored count on SD and continue count.

Hello All,

I’m a beginner with very minimum experience in programming. :slight_smile:

I’m putting a small project together and would like to start with simple ON/OFF pin which will open and close gate from a transistor that is also controlling a relay. I would like to record the number of times a full cycle on to the SD card but my problem is that when a power outage occurs the count will restart back to 1 after the last count stored.

My goal is simple but I just cant get my head around on how to look at last count from SD and continue the operation as well as the count. I’m also looking to having this run for at least 900k cycles with 30sec delay for each state (ON/OFF).

Hardware:

  1. Arduino Uno R3
  2. Adafruit assembled data logging shield.

Thank you all in advance!! :slight_smile:

Code_v1.2.ino (1.66 KB)

Original code attached for others:

#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>

int relayControl = 8;
int startStopSwitch = 7;
int chipSelect = 10;

int switchState = 0;
int delayPeriod = 500;

int cycleCount = 0;

File logfile;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);         // sets the LCD I2C address

void setup()
{
  pinMode(relayControl, OUTPUT);
  pinMode(startStopSwitch, INPUT);
  lcd.begin(20,4);
 
 // Open serial communications and wait for port to open:
  Serial.begin(9600);

  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(chipSelect, OUTPUT);
  
  // 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:
    while (1) ;    
  }
  Serial.println("card initialized.");
  
 // Open up the file we're going to log to!
  logfile = SD.open("datalog.txt", FILE_WRITE); 
  
  if (!logfile) {
    Serial.println("error opening datalog.txt");
    // Wait forever since we can't write data
    while (1) ;
  }
  delay(50);
  Serial.print("Ready to write");
 }

void loop(){
  
switchState = digitalRead(startStopSwitch);

if(switchState == HIGH) {
  digitalWrite(relayControl, HIGH);
  delay(delayPeriod);
  digitalWrite(relayControl, LOW);
  delay(delayPeriod);
  cycleCount++;
  logfile.print("Number of Cycles: ");
  logfile.println(cycleCount);
  logfile.flush();
 }
else {
  digitalWrite(relayControl, LOW);
  }
 }

In setup(), after you open the file, you should read the last value you wrote back into the sketch. This is then your starting point for the new counts.

I would not bother saving anything other than the count to the log file. There is no need for "Number of Cycles" label and it just complicates reading it back in at startup.

Can you please give me an example on how to read back the last saved value. :frowning:

What you are asking is in the documentation for the SD library Arduino - SD

That is exactly my problem. I dont understand how to read it then continue from last save number.

This complies but it is NOT tested. You should understand what it does so that you will be able to modify and debug.

#include <SD.h>

const char COUNT_FNAME = {"COUNT.DAT"};
const int8_t chipSelect = 10;
uint32_t cycleCount = 0;


void saveCount(uint32_t count)
{
  File f = SD.open(COUNT_FNAME, FILE_WRITE);

  if (f)
  {
    f.write((uint8_t *)&count, sizeof(count));
    f.close();
  }
}

uint32_t getCount(void)
{
  uint32_t n = 0;
  File f = SD.open(COUNT_FNAME, FILE_READ);

  if (f)
  {
    f.read((uint8_t *)&n, sizeof(n));
    f.close();
  }
  return(n);
}

void setup(void)
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);

  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(chipSelect, OUTPUT);
 
  // 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:
    while (1) ;   
  }
  Serial.println("card initialized.");

  cycleCount = getCount();
 }

void loop()
{
  cycleCount++;
  saveCount(cycleCount);
  Serial.println(cycleCount);
  delay(1000);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.