Writing an index to a variable from SD Card

Hey all. Got a counter issue. I am reading from an SD card to use values after a power reset, from the SD file. I can read the file, but the value will not stay in my integer “count.”

How can I change the code to put the value into “count?”

Explaination of picture (attachment): Count before is initializing the variable. Count during is while reading the value. Count after is after trying to write it to the variable.
Thanks!

#include <SPI.h>
#include <SD.h>
int dataString = "";

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#include "Timer.h"
Timer t;

File dataCount;
// define variables
const int input_1 = 30;    const int input_2 = 31;


int in_state_1 = 0;   int last_in_1_state = 0;

const byte ResetPin = 21;
int Reset_State = 0;  int last_Reset_State = 0;

const int chipSelect = 4;

int count = 0;
int coin = 0;
int hourly_count = 0;
int sensorPin = A0;
int Update = 0;


void setup() {
  delay(1000);  
  pinMode (input_1, INPUT);  pinMode (input_2, INPUT);

  pinMode (ResetPin, 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:

   Serial.print("Count before;");
   Serial.println (count);  
 // if (SD.exists("datalog.txt")) {
    dataCount = SD.open("count.txt");
  if (dataCount) {
    Serial.print("count during:");

    // read from the file until there's nothing else in it:***************What I've tried;
    while (dataCount.available()) {
    Serial.write(dataCount.read());
    //int count = Serial.read(dataCount.read());  
    //count = (dataCount.read());  
  }}
    //dataCount.read() == count;
    //int count = (dataCount.read());
    // close the file:
    dataCount.close();

   Serial.print("Count after;");
   Serial.println (count);

Capture.JPG

Image from Original Post so we don’t have to download it. See this Image Guide

310ac811a290b65451d32235643aa056b36e7e8d.jpg

…R

For the future please don't post text as pictures. Just copy and paste it and put output in code tags.

You have not posted a complete program so I can't see where the problem might be.

Post the full program in your next Reply, rather than updating your Original Post.

...R

As far as I can see you never update the count variable after initialising it.

You do, however, have this in the code

    //int count = (dataCount.read());

Here is the full code.

// include the library codes:
#include <SPI.h>
#include <SD.h>
int dataString = "";

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#include "Timer.h"
Timer t;

File dataCount;
// define variables
const int input_1 = 30;    const int input_2 = 31;
const int input_3 = 32;    const int input_4 = 33;
const int input_5 = 34;    const int input_6 = 35;
const int input_7 = 36;    const int input_8 = 37;
const int input_9 = 38;    const int input_10 = 39;
const int input_11 = 40;   const int input_12 = 41;

int in_state_1 = 0;   int last_in_1_state = 0;
int in_state_2 = 0;   int last_in_2_state = 0;
int in_state_3 = 0;   int last_in_3_state = 0;
int in_state_4 = 0;   int last_in_4_state = 0;
int in_state_5 = 0;   int last_in_5_state = 0;
int in_state_6 = 0;   int last_in_6_state = 0;
int in_state_7 = 0;   int last_in_7_state = 0;
int in_state_8 = 0;   int last_in_8_state = 0;
int in_state_9 = 0;   int last_in_9_state = 0;
int in_state_10 = 0;  int last_in_10_state = 0;
int in_state_11 = 0;  int last_in_11_state = 0;
int in_state_12 = 0;  int last_in_12_state = 0;

const byte ResetPin = 21;
int Reset_State = 0;  int last_Reset_State = 0;

const int chipSelect = 4;

int count = 0;
int coin = 0;
int hourly_count = 0;
int sensorPin = A0;
int Update = 0;


void setup() {
  delay(1000);  
  pinMode (input_1, INPUT);  pinMode (input_2, INPUT);
  pinMode (input_3, INPUT);  pinMode (input_4, INPUT);
  pinMode (input_5, INPUT);  pinMode (input_6, INPUT);
  pinMode (input_7, INPUT);  pinMode (input_8, INPUT);
  pinMode (input_9, INPUT);  pinMode (input_10, INPUT);
  pinMode (input_11, INPUT); pinMode (input_12, INPUT);
  pinMode (ResetPin, 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:

   Serial.print("Count before;");
   Serial.println (count);  
 // if (SD.exists("datalog.txt")) {
    dataCount = SD.open("count.txt");
  if (dataCount) {
    Serial.print("count during:");

    // read from the file until there's nothing else in it:
    while (dataCount.available()) {
    Serial.write(dataCount.read());
    //int count = Serial.read(dataCount.read());  
    //count = (dataCount.read());  
  }}
    //dataCount.read() == count;
    //int count = (dataCount.read());
    // close the file:
    dataCount.close();

   Serial.print("Count after;");
   Serial.println (count);


 // 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();
  lcd.setCursor(0, 0);
  lcd.print("$ = ");lcd.print (coin);
  lcd.setCursor(0, 1);
  lcd.print("# = ");lcd.print (count);   
  lcd.setCursor(10,0);   
  lcd.print("Hr");
//  lcd.print(hourly_count);
  Serial.println (count);
}

void loop() {
 dataString =   int(count);  
 dataString =   int(hourly_count);
 
 
 /**************************PULSE MONITORING*******************************/
 // check for voltage change 
 in_state_1 = digitalRead(input_1);   in_state_2 = digitalRead(input_2);
 in_state_3 = digitalRead(input_3);   in_state_4 = digitalRead(input_4);
 in_state_5 = digitalRead(input_5);   in_state_6 = digitalRead(input_6);
 in_state_7 = digitalRead(input_7);   in_state_8 = digitalRead(input_8);
 in_state_9 = digitalRead(input_9);   in_state_10 = digitalRead(input_10);
 in_state_11 = digitalRead(input_11); in_state_12 = digitalRead(input_12);
 Reset_State = digitalRead(ResetPin);
 
  if (in_state_1 != last_in_1_state){
    if (in_state_1 == LOW){    
      count ++;
  }}
 last_in_1_state = in_state_1;

  if (in_state_2 != last_in_2_state){
   if (in_state_2 == LOW){    
     count ++;
  }}
 last_in_2_state = in_state_2;

  if (in_state_3 != last_in_3_state){
    if (in_state_3 == LOW){    
      count ++;
  }}
 last_in_3_state = in_state_3;

  if (in_state_4 != last_in_4_state){
   if (in_state_4 == LOW){    
     count ++;
  }}
 last_in_4_state = in_state_4;


  if (in_state_5 != last_in_5_state){
    if (in_state_5 == LOW){    
      count ++;
  }}
 last_in_5_state = in_state_5;

  if (in_state_6 != last_in_6_state){
   if (in_state_6 == LOW){    
     count ++;
  }}
 last_in_6_state = in_state_6;


   if (in_state_7 != last_in_7_state){
    if (in_state_7 == LOW){    
      count ++;
  }}
 last_in_7_state = in_state_7;

  if (in_state_8 != last_in_8_state){
   if (in_state_8 == LOW){    
     count ++;
  }}
 last_in_8_state = in_state_8;

   if (in_state_9 != last_in_9_state){
    if (in_state_9 == LOW){    
      count ++;
  }}
 last_in_9_state = in_state_9;

  if (in_state_10 != last_in_10_state){
   if (in_state_10 == LOW){    
     count ++;
  }}
 last_in_10_state = in_state_10;


   if (in_state_11 != last_in_11_state){
    if (in_state_11 == LOW){    
      count ++;
  }}
 last_in_11_state = in_state_11;

  if (in_state_12 != last_in_12_state){
   if (in_state_12 == LOW){    
     count ++;
  }}
 last_in_12_state = in_state_12;

coin = (count/4);
 
 //if (Reset_State != last_Reset_State){
  if (Reset_State == LOW){
    count = 0; coin = 0; hourly_count = 0;
    SD.remove("count.txt");
    Serial.println("Count Reset count =;");
    Serial.print(count);
  }
// }
 t.update();



 

/**************************Update*******************************/
   Update = analogRead(sensorPin); //Select button used to update lcd information
   if (Update < 850) {
    delay(1000);
    UpdateResults();
   }

}



void UpdateResults(){  
   lcd.clear();   
   hourly_count ++;                 
   lcd.setCursor(0, 0);
   lcd.print("$ = ");lcd.print (coin);
   lcd.setCursor(0, 1);
   lcd.print("# = ");lcd.print (count);   
   lcd.setCursor(10,0);   
   lcd.print("Hr ");lcd.print (hourly_count);
   Serial.println ("count update=");
   Serial.println (count);
   

   
   
  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("Dollars:");dataFile.println(coin); 
    dataFile.close();
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }

  
  SD.remove("count.txt");
  
 
  
  File dataCount = SD.open("count.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataCount) {
  //  dataFile.print("Hour:");dataFile.print(hourly_count);dataFile.print(";");
  //  dataFile.print("Dollars:");
    dataCount.println(count); 
    dataCount.close();
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }

}

/*
void Reset()  {
  count = 0;// hourly_count = 0; coin = 0;
}
*/

UKHeliBob:
As far as I can see you never update the count variable after initialising it.

You do, however, have this in the code

    //int count = (dataCount.read());

The idea is to read a value that was put into the file last the last time the program had run. If the power was lost and the count initialized at 0, I’d lose my count prior to losing power.

So I have a reset that resets the count to 0 and writes that to a file. If the count is not zero and the power is lost I was the last value to show up which is a single file read in the start up. Hence the “int count = (dataCount.read());”

Thanks!

I can't make any sense of your program but even a cursory look shows that it has inconsistencies in it. For instance

  dataString =   int(count);
  dataString =   int(hourly_count);

The cursory look also reveals that the program is crying out for arrays to be used. The clue is the variables with numeric suffixes.

Please can you describe in words what the program is supposed to do and when the count is meant to be written to the file ?

Absolutely! I am counting pulses from 12 coin counters to write two separate files on an SD for back up and confirmation of count. datastring count is simply the pulses, but the hourly count is incremented every hour and written into the second file. That all works fine.

So after install the program checks for a value in "dataCount.read()" If there is no file, the count is 0. This also occurs after a reset switch is operated.

10:00 am: Initialize, count = 0. Coins begin to fall.
12:00 pm: count = 4095
12:01 pm: coin collected, program reset
12:02 pm: count = 0

However if a count has been accumulated and there is a loss of power, the program needs to initiate the count at the last value before power loss.

12:00 pm: count is = 1023
12:01 pm: Power loss
12:02 pm count = 1023

As you can see in the jpg. random values seem to come up such as -1, 59 etc.... All while the SD is being read. But if I check the SD the value in dataCount is equal to the count I intentionally made during testing.

The first thing that I would do is to use arrays to read and hold the input states.

This will not make your program work faster or better but it will substantially reduce its size and with suitable choice of variable names make it easier to read. Iterate through the sensors using a for loop or just use the loop() function and increment an index to the arrays each time through loop().

Write the current count to the file either after each sensor read or at the end of the loop that reads all of the sensors. Forget about the hourly count for now.