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 ).
#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 )
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.
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());
}
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.
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?
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.
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,
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.
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
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.