Can someone tell me what I am doing wrong with .parseInt()
I have a file with three values. But I get a return of 0 for each.
foo.txt holds
1234
44
125
File myText = SD.open("/foo.txt");
int value1 = myText.parseInt();
int value2 = myText.parseInt();
int value3 = myText.parseInt();
if (myText){
if (myText.available()){
value1 = myText.parseInt();
value2 = myText.parseInt();
value3 = myText.parseInt();
myText.close();
}
}
Serial.print(value1);
Serial.print(value2);
Serial.print(value3);
Try printing the results after you read the values the first time.
if (myText) {
if (myText.available()) {
value1 = myText.parseInt();
Serial.println(value1);
value2 = myText.parseInt();
Serial.println(value2);
value3 = myText.parseInt();
Serial.println(value3);
myText.close();
}
}
Same result.
0
0
0
No, try printing them before this:
if (myText){
It looks like you read them all initially and then try to read them again if there's anything left in the file.
I did this and it printed the correct numbers.
myText = SD.open("/foo.txt");
while (myText.available())
{
Serial.write(myText.read());
}
file2.close();
@rudy-work Start by posting ALL code See http://snippets-r-us.com/
And as @wildbill said in reply #1 , print it after the first time you read the values. In your response in reply #2 you print it the second time. Just find the first time
wildbill:
No, try printing them before this:
if (myText){
It looks like you read them all initially and then try to read them again if there's anything left in the file.
OK I will change that
I had copied that code for someone's example here. I didn't even look at that part.
I removed the extra if (myText){
now it is this, with the same result
int value1 = myText.parseInt();
int value2 = myText.parseInt();
int value3 = myText.parseInt();
if (myText.available()) {
value1 = myText.parseInt();
Serial.println(value1);
value2 = myText.parseInt();
Serial.println(value2);
value3 = myText.parseInt();
Serial.println(value3);
myText.close();
You have six parseint calls in your code, but only three numbers in your file. You only print anything for the fourth, fifth and six calls to parseint. It should be no surprise then that they all return zero.
I will stripped down the code to this. It is a complete sketch.
#include "FS.h"
#include "SD.h"
#include "SPI.h"
File myFile;
//-------------------------------------------
#define VSPI FSPI
static const int spiClk = 1000000; // 1 MHz
//uninitalised pointers to SPI objects
SPIClass * vspi = NULL;
//===========================================
void setup() {
Serial.begin(115200);
SPIClass SDSPI(HSPI);
#define SD_miso 17
#define SD_mosi 16
#define SD_sck 15
#define SD_ss 14
SDSPI.begin(SD_sck, SD_miso, SD_mosi, -1);
pinMode(SD_ss, OUTPUT); //HSPI SS set slave select pins as output
if (!SD.begin(SD_ss, SDSPI)) {
Serial.println("Card Mount Failed");
return;
}
//------------------------------------
File myText = SD.open("/foo.txt");
if (myText) {
Serial.println("file good");
}
else {
Serial.println("file bad");
while (1);
}
int value1 = myText.parseInt();
int value2 = myText.parseInt();
int value3 = myText.parseInt();
if (myText.available()) {
value1 = myText.parseInt();
Serial.println(value1);
value2 = myText.parseInt();
Serial.println(value2);
value3 = myText.parseInt();
Serial.println(value3);
myText.close();
}
Serial.println(value1);
Serial.println(value2);
Serial.println(value3);
//===========================================
Serial.println("this works");
myText = SD.open("/foo.txt");
while (myText.available())
{
Serial.write(myText.read());
}
myFile.close();
}
wildbill:
You have six parseint calls in your code, but only three numbers in your file. You only print anything for the fourth, fifth and six calls to parseint. It should be no surprise then that they all return zero.
Yeah I didn't understand that part. Like I said, I just copied that code from a post on this forum. I will get rid of that.
OK that works now.
A big thanks.
Wouldn't it than be a good idea to look up what code does instead of just copying it and crying for help if it does not work?
Although karma++ for coming up with a MCVE. Not only does that make it easier for us, it for sure makes it easier for you because you don't need to worry about other parts possibly causing the problem.
system
Closed
May 7, 2021, 2:21pm
12
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.