myFile = SD.open("test.txt");
if (myFile) {
while (myFile.available() && i<len) {
x = myFile.parseInt();
Serial.println(x);
}
myFile.close();https://forum.arduino.cc/index.php?topic=326573.0
The issue is not specific to the stm core, but rather the way the sdfat library works.
#define ARDUINO_FILE_USES_STREAM 1#include <SPI.h>
#include "SdFat.h"
SdFat SD;
const uint8_t csPin = SS;
File file;
..
file = SD.open("stream.txt", O_RDWR|O_CREAT|O_TRUNC);
if (!file) {
Serial.println(F("open error"));
return;
}
;
..
// Rewind the file and read the number with parseInt().
file.seek(0);
int i = file.parseInt();
Serial.println(i);
file.close();#include <SPI.h>
#include "SdFat.h"
SdFat sd1(1);
// SdFatEX sd1(1);
const uint8_t SD1_CS = PA4; // chip select for sd1
//SdFat sd2(2);
//// SdFatEX sd2(2);
//const uint8_t SD2_CS = PB12; // chip select for sd2
#define errorExit(msg) errorHalt(F(msg))
#define initError(msg) initErrorHalt(F(msg))
unsigned long duration;
int my_array[1000];
int num;
int c;
void setup() {
Serial.begin(115200);
delay(10000);
// initialize the first card
if (!sd1.begin(SD1_CS, SD_SCK_MHZ(18))) {
sd1.initError("sd1:");
}
// initialize the second card
// if (!sd2.begin(SD2_CS, SD_SCK_MHZ(18))) {
// sd2.initError("sd2:");
// }
Serial.println(F("------sd1 root-------"));
sd1.ls();
// set the current working directory for open() to sd2
//sd1.chvol();
File file1;
if (!file1.open("data.txt", O_RDWR | O_CREAT | O_TRUNC)) {
sd1.errorExit("file1");
}
for (int x=0;x<1000;x++){
parseUInt();
Serial.println(num);
}
file1.close();
Serial.println(F("Done"));
}
void loop(){}
int parseUInt () {
int num=-1;
while (true) {
int c = file1.read();
if (c>47 && c<58) {
if (num<0) num=0;
num=num*10+c-48;
} else if (num>-1 || c==-1) return num;
}
}
Do not try to mess with complex sketches, instead try to go step-by-step.
Try the original example (my above code snippet is from) from the SdFat until it works perfectly. And after then you may play with the code.
https://github.com/greiman/SdFat/blob/m … rseInt.ino
Also mind the C language requires some rules are followed. The file1 must be declared global, otherwise it will not be visible inside a function.
See my snippet above.
#define ARDUINO_FILE_USES_STREAM 1
