Facing problem SD card module with LCD display.

Hello, I am trying to take this code I want to show value in 16x2 lcd display which was stored in SD card ,

pinA0 connected to potentio meter.

#If i pressed the push button (butPin) Value of Analogread Pin A0 stored in to SD card.(this working fine :grinning: ).

#If i pressed the second push button (butlcd) Value which was stored previously will write on serialmonitor as well as showing lcd display than after all file will be removed. ===(Not working :disappointed_relieved: )

Problem is that, its writing in serial monitor as exact value which was stored,but lcd display shows wrong value (-1).

Here is my code:-

#include <Wire.h> 
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int chipSelect = 10;
const int butPin = 7;
bool butState;
const int butlcd = 2;
bool butState1;
int analogPin=A0;
void setup() 
{
  lcd.begin(16,2);
  lcd.backlight();
  pinMode(butPin,INPUT_PULLUP);
  pinMode(butlcd,INPUT_PULLUP);
  pinMode(analogPin,INPUT);
  Serial.begin(9600);
  while (!Serial)
  {
    ;
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed, or not present");
    while (1);
  }
  Serial.println("Card initialized.");
}

void loop() 
{
  butState = digitalRead(butPin);
  if (butState==1)
  {
   //Default state. Do Nothing. 
  }
  else
  {
    Serial.println("Data LOG");
    String dataString = "";
    
      int sensor = analogRead(analogPin);
      dataString += String(sensor);
      

   File dataFile = SD.open("datalog.txt", FILE_WRITE);
   if (dataFile)
   {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
   }
   else 
   {
    Serial.println("error opening datalog.txt");
   }}
   butState1 = digitalRead(butlcd);
  if (butState1==0)
  {
  readu();
  }}
 
 void readu()
  {
 
 File dataFile  = SD.open("datalog.txt");
  if (dataFile) {
    Serial.println("datalog.txt");

    // read from the file until there's nothing else in it:
    while (dataFile.available()) {
      Serial.write(dataFile.read());
     
    }
   lcd.setCursor(0,1);
    lcd.print(dataFile.read());
    delay(1000);
    lcd.clear();
     dataFile.close();}
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 delay(300);
 if (SD.exists("datalog.txt")) { // if "file.txt" exists, fill will be deleted
    Serial.println("File exists.");
    if (SD.remove("datalog.txt") == true) {
      Serial.println("Successfully removed file.");
    } else {
      Serial.println("Could not removed file.");
    }
  }
  }

Because I'm totally noob on this topic, any suggestions or recommendations are well appreciated.
Thanks a lot.

Because I'm totally noob on this topic

You REALLY need to learn how to use the functions that the Arduino supports. There is NO excuse for wrapping an int value in a String instance, and appending it to an empty String, when the println() method is perfectly capable of converting an int to text to write to the file.

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

Did you pay the slightest bit of attention to that comment?

   lcd.setCursor(0,1);
    lcd.print(dataFile.read());

Why are you trying to read from the same file, after you've read everything that was in the file?

PaulS:
You REALLY need to learn how to use the functions that the Arduino supports. There is NO excuse for wrapping an int value in a String instance, and appending it to an empty String, when the println() method is perfectly capable of converting an int to text to write to the file.

I am here asking because I would like to optimise my time and learning process.

Really thanks your reply.

when the println() method is perfectly capable of converting an int to text to write to the file.

when i used

while (dataFile.available()) {
      Serial.write(dataFile.read());
      
     
    }

serial monitor out put is here

Initializing SD card...Card initialized.
Data LOG
309
datalog.txt
309
File exists.
Successfully removed file.

when i used

 while (dataFile.available()) {
      Serial.println(dataFile.read());
      
     
    }

serial monitor o/p is here

Data LOG
313
datalog.txt
51
49
51
13
10
File exists.
Successfully removed file.

its very different from actual read.

Why are you trying to read from the same file, after you've read everything that was in the file?

how its possible ,please make necessary changes/advice me.

You would get the same output using .write() and .print() if you stored the value read into a char variable.

how its possible ,please make necessary changes/advice me.

Read once. Print twice.

 while (dataFile.available())
 {
    char c = dataFile.read();
    lcd.print(c);
    Serial.print(c);
 }

PaulS:
You would get the same output using .write() and .print() if you stored the value read into a char variable.
Read once. Print twice.

 while (dataFile.available())

{
   char c = dataFile.read();
   lcd.print(c);
   Serial.print(c);
}

sir i have changed as per your instruction here is the code

/*Connections
    SD card Module - Arduino UNO
              MOSI - Pin 11
              MISO - Pin 12
              CLK  - Pin 13
              CS   - Pin 10
   Button to Pin 7 and GND with internal Pull-up.
   Three 10KΩ Potentiometers to Analog Pins A0, A1 and A2.
*/
#include <Wire.h> 
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int chipSelect = 10;
const int butPin = 7;
bool butState;
const int butlcd = 2;
bool butState1;
int analogPin=A0;
char Str1[15];
void setup() 
{
  lcd.begin(16,2);
  lcd.backlight();
  pinMode(butPin,INPUT_PULLUP);
  pinMode(butlcd,INPUT_PULLUP);
  pinMode(analogPin,INPUT);
  Serial.begin(9600);
  while (!Serial)
  {
    ;
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed, or not present");
    while (1);
  }
  Serial.println("Card initialized.");
}

void loop() 
{
  butState = digitalRead(butPin);
  delay(1000);
  if (butState==1)
  {
   //Default state. Do Nothing. 
  }
  else
  {
    Serial.println("Data LOG");
    String dataString = "";
    
      int sensor = analogRead(analogPin);
      dataString += String(sensor);
      

   File dataFile = SD.open("datalog.txt", FILE_WRITE);
   if (dataFile)
   {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
   }
   else 
   {
    Serial.println("error opening datalog.txt");
   }}
   butState1 = digitalRead(butlcd);
 delay(500);
  if (butState1==0)
  {
  readu();
  }}
 
 void readu()
  {
 
 File dataFile  = SD.open("datalog.txt");
  if (dataFile) {
    Serial.println("datalog.txt");

    // read from the file until there's nothing else in it:
    while (dataFile.available()) {
      char c = dataFile.read();
      lcd.setCursor(0,1);
      lcd.print(c);
      delay(1000);
      lcd.clear();
      Serial.print(c);
      
      //lcd.clear();
      }
   dataFile.close();
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 delay(1000);
 if (SD.exists("datalog.txt")) { // if "file.txt" exists, fill will be deleted
    Serial.println("File exists.");
    if (SD.remove("datalog.txt") == true) {
      Serial.println("Successfully removed file.");
    } else {
      Serial.println("Could not removed file.");
    }
  }
  }

but serial monitor exact value,but lcd display shows single digits one by one in o segment in 1 raw,For example cahr c = 626, lcd display shows first 6 than clear and shows 2 than after 6 ,
but i want to show this in single line.
please expecting favourable reply.

but i want to show this in single line

Where, in my code, did I use delay()? Where, in my code, did I set the cursor position to print every character in the same place? Where, in my code, did I clear the LCD after each character?

So, why did you?

PaulS:
Where, in my code, did I use delay()? Where, in my code, did I set the cursor position to print every character in the same place? Where, in my code, did I clear the LCD after each character?

So, why did you?

sory sir,

After removing that ,lcd display showing the value in single line but after end digits of value showing some unspecified character,

sir,
now i removing last command from program that is

delay(1000);
 if (SD.exists("datalog.txt")) { // if "file.txt" exists, fill will be deleted
    Serial.println("File exists.");
    if (SD.remove("datalog.txt") == true) {
      Serial.println("Successfully removed file.");
    } else {
      Serial.println("Could not removed file.");
    }
  }

now values are written in same file,when i press the button lcd will display only the value last written in sd card.
how can i achieve this changes,please give the advice.

please give the advice.

You posted only a small part of your code, so I'll post only a small part of the answer.

You need to

Hope that's enough to get you going.

PaulS:
You posted only a small part of your code, so I'll post only a small part of the answer.

You need to

Hope that's enough to get you going.

here is the full code after removing last portion which was "deleting existing file"

/*Connections
    SD card Module - Arduino UNO
              MOSI - Pin 11
              MISO - Pin 12
              CLK  - Pin 13
              CS   - Pin 10
   Button to Pin 7 and GND with internal Pull-up.
   Three 10KΩ Potentiometers to Analog Pins A0, A1 and A2.
*/
#include <Wire.h> 
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int chipSelect = 10;
const int butPin = 7;
bool butState;
const int butlcd = 2;
bool butState1;
int analogPin=A0;
char Str1[15];
void setup() 
{
  lcd.begin(16,2);
  lcd.backlight();
  pinMode(butPin,INPUT_PULLUP);
  pinMode(butlcd,INPUT_PULLUP);
  pinMode(analogPin,INPUT);
  Serial.begin(9600);
  while (!Serial)
  {
    ;
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed, or not present");
    while (1);
  }
  Serial.println("Card initialized.");
}

void loop() 
{
  butState = digitalRead(butPin);
  delay(1000);
  if (butState==1)
  {
   //Default state. Do Nothing. 
  }
  else
  {
    Serial.println("Data LOG");
    String dataString = "";
    
      int sensor = analogRead(analogPin);
      dataString += String(sensor);
      

   File dataFile = SD.open("datalog.txt", FILE_WRITE);
   if (dataFile)
   {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
   }
   else 
   {
    Serial.println("error opening datalog.txt");
   }}
   butState1 = digitalRead(butlcd);
 delay(500);
  if (butState1==0)
  {
  readu();
  }}
 
 void readu()
  {
 
 File dataFile  = SD.open("datalog.txt");
  if (dataFile) {
    Serial.println("datalog.txt");

    // read from the file until there's nothing else in it:
    while (dataFile.available()) {
      char c = dataFile.read();
      
      lcd.print(c);
      
     
      Serial.print(c);
      
      
      }
   dataFile.close();
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 delay(1000);
 
  }



 //this portion deleted

Now values are written in same file when i press the button Dpin7.when i press the button DPin 2 lcd will display only the value last written in sd card.
how can i achieve this changes,

how can i achieve this changes,

You can start by getting rid of the stupid delay() in loop(). If you want to do something periodically, use the technique in the blink without delay example, NOT delay().

Then, you'll get rid of EVERY reference to String.

Then, I'll consider helping you.

PaulS:
You can start by getting rid of the stupid delay() in loop(). If you want to do something periodically, use the technique in the blink without delay example, NOT delay().

Then, you'll get rid of EVERY reference to String.

Then, I'll consider helping you.

sir
Relevant changes are done and all "stupid delays()" removed. code is here

/*Connections
    SD card Module - Arduino UNO
              MOSI - Pin 11
              MISO - Pin 12
              CLK  - Pin 13
              CS   - Pin 10
   Button to Pin 7 and GND with internal Pull-up.
   Three 10KΩ Potentiometers to Analog Pins A0, A1 and A2.
*/
#include <Wire.h> 
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int chipSelect = 10;
const int butPin = 7;
bool butState;
const int butlcd = 2;
bool butState1;
int analogPin=A0;
char Str1[15];
unsigned long previousMillis = 0;
unsigned long previousMillis1 = 0;
const long interval = 1000;
void setup() 
{
  lcd.begin(16,2);
  lcd.backlight();
  pinMode(butPin,INPUT_PULLUP);
  pinMode(butlcd,INPUT_PULLUP);
  pinMode(analogPin,INPUT);
  Serial.begin(9600);
  while (!Serial)
  {
    ;
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed, or not present");
    while (1);
  }
  Serial.println("Card initialized.");
}

void loop() 
{
  unsigned long currentMillis = millis();
   unsigned long currentMillis1 = millis();
  if (currentMillis - previousMillis >= interval) {
  previousMillis = currentMillis;
  butState = digitalRead(butPin);
  //delay(1000);
  if (butState==1)
  {
   //Default state. Do Nothing. 
  }
  else
  {
    Serial.println("Data LOG");
    String dataString = "";
    
      int sensor = analogRead(analogPin);
      dataString += String(sensor);
      

   File dataFile = SD.open("datalog.txt", FILE_WRITE);
   if (dataFile)
   {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
   }
   else 
   {
    Serial.println("error opening datalog.txt");
   }}
   butState1 = digitalRead(butlcd);
 //delay(500);
  if (butState1==0 && currentMillis1 - previousMillis1 >= interval)
  {
    previousMillis1 = currentMillis1;
  readu();
  }}

}
 void readu()
  {
 
 File dataFile  = SD.open("datalog.txt");
  if (dataFile) {
    Serial.println("datalog.txt");

    // read from the file until there's nothing else in it:
    while (dataFile.available()) {
      char c = dataFile.read();
      
      lcd.print(c);
      
     
      Serial.print(c);
      
      
      }
   dataFile.close();
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 /* delay(1000);
 if (SD.exists("datalog.txt")) { // if "file.txt" exists, fill will be deleted
    Serial.println("File exists.");
    if (SD.remove("datalog.txt") == true) {
      Serial.println("Successfully removed file.");
    } else {
      Serial.println("Could not removed file.");
    }
  }*/

 
  }



 //this portion deleted

Kindly consider my case.

Kindly consider my case.

No. You are still doing stupid stuff with Strings, uselessly.

PaulS:
No. You are still doing stupid stuff with Strings, uselessly.

PaulS:
No. You are still doing stupid stuff with Strings, uselessly.

sir please make necessary changes in my program to achieve my condition(Now values are written in same file when i press the button Dpin7.Wwhen i press the button DPin 2 lcd will display only the value last written in sd card.).ihave totally depending this platform for help.there is no other way beside me.