Go Down

Topic: SD card text file to integer (Read 156 times) previous topic - next topic

morgunn3

Hi,

I have hit a brick wall, and hopefully someone knows the solution.

I have a SD card connected to an Arduino Nano.

I am attempting to read a number stored in a text file on that SD card and convert it to an integer.

The code I have included simply displays a text file on a TFT LCD and waits for the user to push a button, then displays the next text file. All text files on the SD card are named sequentially, ex. "1.txt", "2.txt", "3.txt", "4.txt" and so on. An integer is used to keep track of which text file should be loaded next. The integer is updated by 1 each time the user presses the button. The integer also stores its number in a text file called "VAR.txt" There is nothing in VAR.txt except for this number. This is where I run into trouble. I can write to VAR.txt and print the number stored in VAR.txt to the TFT LCD, but I cannot convert the number stored in VAR.txt to an integer. I want to do this so that whenever power is lost to the Arduino, it will remember where it was in the sequence of text files and pick up where it left off instead of starting from the beginning.

TL;DR

How to retrieve number from text file located on SD card and convert it to an integer.

I appreciate you taking the time to read this, and thank you so much for your help!  :D


Code: [Select]
/* A general overview of this code is as follows.
 * Text files stored on a sd card are printed
 * to a tft lcd display. These files are organized
 * as "1.txt", "2.txt", "3.txt", "4.txt", ect.
 * Whenever a button is pressed, the next sequential
 * text file is loaded. The number of the current
 * text file is stored on the sd card in "VAR.txt"
 * Each time the button is pressed, the number on
 * that file is increased by one. When the
 * Arduino is first powered on, it will retrieve
 * that number from "VAR.txt and convert it to
 * an integer for the purpose of picking up where
 * the last text file left off before powering down.
 * THIS IS WHERE I AM STUCK! I cannot find a means to
 * retrieve a number from a text file and convert it
 * to an integer! Please Help.
*/


#include <Adafruit_GFX.h>  //lcd screen
#include <TFT_ILI9163C.h>  //this is for the lcd screen
#define __CS 10
#define __DC 9  //lcd definition
// Color definitions
#define  BLACK   0x0000
#define WHITE   0xFFFF
TFT_ILI9163C tft = TFT_ILI9163C(__CS, 8, __DC); //lcd pin definition
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4; //pin that controls MicroSD Card
File file; //current file used for reading and writing

const int buttonPin1 = 5;     // the number of the pushbutton pin

int button1State = 0;         // variable for reading the pushbutton status

int index = 0; //variable for remembering the last text file




void setup() {
 
index = 1;

tft.begin();
       tft.setTextWrap(true);
       tft.setRotation(3); //use 1 if you want to flip it 180 degrees
       tft.fillScreen();
       tft.setCursor(0, 0);
       tft.setTextColor(WHITE); 
       tft.setTextSize(1.5);

Serial.begin(9600);
while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
tft.println("Initializing SD card...");
  if (!SD.begin(4)) {
    tft.println("initialization failed! Please powercycle");
    while (1);
  }
  tft.println("initialization done.");

 // open the file.
  file = SD.open("VAR.txt");
if (file) {
    while (file.available()) {   
 tft.write(file.read());        // this lets me know what number is stored in the  VAR.txt file


//THIS IS WHERE I AM STUCK!!!

//How do I retrieve a number from a text file on an SD card and convert it to an Integer. I can
//display the number from the text file with tft.write(file.read()); but I cannot convert it to
//an integer. Please help...
}
 file.close();
 tft.println("System Ready");
 
} else {
   // if the file didn't open, print an error:
    tft.println("CYKA BLYAT!");
}

 
pinMode(buttonPin1, INPUT);



}

void loop() {


 button1State = digitalRead(buttonPin1);
  delay(2);


  if (button1State == HIGH) {
    // yes output:
    String thisString = String(index);               //this is the code that lets you cycle through text files
    String stringOne =  String(thisString + ".txt");
    file = SD.open(stringOne);
    if (file) {
       tft.fillScreen();
       tft.setCursor(0, 0);
       tft.setTextColor(WHITE); 
       tft.setTextSize(1);
       tft.println("txt reads:");
      // read from the file until there's nothing else in it:
      while (file.available()) {
      tft.write(file.read()); 
      }
      // close the file:
      file.close();

    index++; // this adds one to the index variable

   //this code deletes the var.txt file and create a new empty one
   //without this, it just adds the index number to the next line in the text file
             SD.remove("VAR.txt");

     file = SD.open("VAR.txt", FILE_WRITE);      // this updates the text file record of the variable
              if (file) {
                file.println(thisString);
                 file.close();

//delete after debugging. This lets me know what number the text file is.
tft.println(index);

                 
         } else {
               // if the file didn't open, print an error:
               tft.fillScreen();
               tft.setCursor(0, 0);
               tft.println("Could not update index");
                }
     } else {
    // if the file didn't open, print an error:
    tft.println("сука блять");
    tft.println(index);
    }
    delay(200); 
}
}

Whandall

You could use parseInt instead of reading the content as bytes and displaying it only.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

PaulS

Code: [Select]
if (file) {
    while (file.available()) {   
 tft.write(file.read());        // this lets me know what number is stored in the  VAR.tx

That does not store the character anywhere, so it's going to be hard to use the data you read from the file.
The art of getting good answers lies in asking good questions.

Go Up