Считывание с SD обработка и перезапись переменных

Gr5
Offline
Зарегистрирован: 26.03.2015

Привет помогите с работой с SD картой, нужно считать переменную из файла, изменить переменную и перезаписать изменённую, таких переменных будет 2. У меня ничего не получается.
 

#include <SD.h>
#include <SimpleTimer.h>
File myFile;
int timewalkcurrent=0;
long int globalruntime;
SimpleTimer timer2;
void setup(){
 timer2.setInterval(1000,  timerrunglobal );

{
  Serial.begin(9600);
  Serial.println(SD_CHIP_SELECT_PIN); //53
  Serial.println(SPI_MOSI_PIN);       //51
  Serial.println(SPI_MISO_PIN);       //50
  Serial.println(SPI_SCK_PIN);        //52
   while (!Serial) {
    ; 
  }

void timerrunglobal(){
timerrunglobal++;
}
void refreshsd(){
  
   pinMode(53, OUTPUT);  
    myFile = SD.open("time.txt");
   if (myFile) {
    while (myFile.available()) {
       globalruntime=myFile.read();
         }
       myFile.close();
  } 
  long int prof=globalruntime;
  globalruntime=prof+timerrunglobal;
  timerrunglobal==zero;
   SD.remove("time.txt");
    pinMode(53, OUTPUT);  
     myFile = SD.open("time.txt", FILE_WRITE); 
     if (myFile) {
     
        myFile.write(globalruntime);
         myFile.close();   

}}
void loop(void)
{

      unsigned long currentMillis = millis();
      if (currentMillis - previousMillis >= interval) {
     previousMillis = currentMillis;
         refreshsd();
     } 
        
     
     const long interval = 0; }
  
   Serial.println("");
   Serial.println(globalruntime);
   Serial.println("");
}

 

Gr5
Offline
Зарегистрирован: 26.03.2015

Вот упростил до максимума код, переделал, выводит значение меньше 254 только, как обойти ето?

#include <SD.h>
#include <stdlib.h>    
#include <math.h>
#include <WString.h>

File myFile;
int arr[2];
int j=0;
int k;
int v=121;
char l[25];
void setup()

{
  Serial.begin(9600);
  Serial.println(SD_CHIP_SELECT_PIN); //53
  Serial.println(SPI_MOSI_PIN);       //51
  Serial.println(SPI_MISO_PIN);       //50
  Serial.println(SPI_SCK_PIN);        //52
   while (!Serial) {
    ; 
  }
Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


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

  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
SD.remove("test.txt");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    k=999;
    myFile.print("999");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
   while (myFile.available()) {
      long int a = myFile.read();
      arr[j] = a;
      j++;
    }
      myFile.close();
    }
    
   else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  delay(500);
 Serial.println(arr[0]);
 Serial.println(arr[1]);
 Serial.println(arr[2]);
 Serial.println(arr[3]);
}

 

Gr5
Offline
Зарегистрирован: 26.03.2015

Вот упростил до максимума код, переделал, выводит значение меньше 254  коректно только, как обойти ето?

#include <SD.h>
#include <stdlib.h>    
#include <math.h>
#include <WString.h>

File myFile;
int arr[2];
int j=0;
int k,pi;
//int v=121;
//char l[25];
void setup()

{
  Serial.begin(9600);
  Serial.println(SD_CHIP_SELECT_PIN); //53
  Serial.println(SPI_MOSI_PIN);       //51
  Serial.println(SPI_MISO_PIN);       //50
  Serial.println(SPI_SCK_PIN);        //52
   while (!Serial) {
    ; 
  }
Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


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

  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
SD.remove("test.txt");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    k=322;
    myFile.write(k);
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
   while (myFile.available()) {
      long int a = myFile.read();
      arr[j] = a;
      j++;
       myFile.close();
    }
     
    }
    
   else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  delay(1000);
  //pi=arr[0]-48;
// Serial.println(pi);
  Serial.println("");
  Serial.println("");
  Serial.println(arr[0]);
 Serial.println(arr[1]);
Serial.println(arr[2]);
 Serial.println(arr[3]);
Serial.println("");
Serial.println("");

}

 

Gr5
Offline
Зарегистрирован: 26.03.2015

Люди которіе работали со строками дайте подсказку)
попробівал таким образом считывать, уже 5тизначные числа коректно только считывает.
 

#include <SD.h>
#include <stdlib.h>    
#include <math.h>
#include <WString.h>

File myFile;
int arr[7];
int j=0;
int k,pi;
//int v=121;
//char l[25];
void setup()

{
  Serial.begin(9600);
  Serial.println(SD_CHIP_SELECT_PIN); //53
  Serial.println(SPI_MOSI_PIN);       //51
  Serial.println(SPI_MISO_PIN);       //50
  Serial.println(SPI_SCK_PIN);        //52
   while (!Serial) {
    ; 
  }
Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


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

  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
SD.remove("test.txt");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    k=1;
    myFile.println("12345");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
 while (myFile.available()) {
      char val[50];
      float v;
      int i=0;
      while(val[i-1]!='\r'){
          Serial.println("work1");
             val[i]=myFile.read();
          Serial.println("work2");
        i++;
       
      }      
v=atoi(val);
arr[j] = v;
j++;
       myFile.close();
    }
     
    }
    
   else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  delay(1000);
  //pi=arr[0]-48;
// Serial.println(pi);
  Serial.println("");
  Serial.println("");
  Serial.println(arr[0]);
 Serial.println(arr[1]);
Serial.println(arr[2]);
 Serial.println(arr[3]);
Serial.println("");
Serial.println("");

}