So I want to store a number on an sd card in a txt file and display it on an lcd, When the number is read the lcd just displays it as the number it should, but as soon as I set the number gotten from the sd card to an int it only adds the first number. Yes the code is messy. sorry in advance
code:
//set up display
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
//set up sd card
#include <SPI.h>
#include <SD.h>
//Setting up int64
#include <Int64String.h>
//set up button
int buttonState;
int isUsed;
const int buttonPin = 7;
int startup = 0;
int onetimer = 0;
int amountOfPresses = 0;
void setup() {
// Initiate the LCD:
lcd.init();
lcd.backlight();
Serial.begin(9600);
//set button to input
pinMode(buttonPin, INPUT);
}
void loop() {
if(startup == 0){
if (!SD.begin(10)){
lcd.print("No SD card found");
while(!SD.begin(10));
}
lcd.print("Reading file...");
delay(1000);
File dataFile = SD.open("data.txt");
if(dataFile){
lcd.clear();
while(dataFile.available()){
char number = dataFile.read();
lcd.print(number);
if(onetimer == 0){
String number2 = String(number);
amountOfPresses = number2.toInt();
Serial.println(amountOfPresses);
Serial.println(number2);
Serial.println(number);
onetimer = 1;
}
}
dataFile.close();
}
}
int pressed = digitalRead(buttonPin);
if(pressed == LOW && isUsed == 0){
isUsed++;
amountOfPresses++;
lcd.clear();
lcd.print(amountOfPresses);
if(String(amountOfPresses).endsWith("0")){
Serial.println(amountOfPresses);
SD.remove("data.txt");
File dataFile = SD.open("data.txt", FILE_WRITE);
if(dataFile){
dataFile.print(amountOfPresses);
dataFile.close();
}else{
Serial.println("Failed to get data.txt");
}
}
}else if(pressed == HIGH && isUsed == 1){
delay(10);
isUsed = 0;
}
}