如何创建函数:使用文件名作为函数参数从 SD 卡文件中获取单个值

How to create a function: Get a single value from SD card file using the file name as function parameter

我很感激我能得到的任何指导或帮助。我正在编写一个程序,其中包含存储在 SD 卡上的 PID 值,因此我可以在触摸屏上更改它们,而无需将其连接到我的计算机。我想要一个可以调用参数的函数,允许我增加或减少数字并更改文件名。下面的函数是我必须改变的 "pnum.txt" 是增加还是减少;但是我无法弄清楚如何使 File 作为参数工作。

我试过将“"pnum.txt"”(带引号)作为字符和字符串,即使它按应有的方式打印出来,但在插入函数时不起作用。我也试过将整个 SD.open("pnum.txt", FILE_WRITE)myFile = SD.open("pnum.txt", FILE_WRITE) 作为 File 传递,但这有些奇怪 - 它会在我打开文件时创建文件,但不会写入文件。我发现自己只是一遍又一遍地尝试同样的事情,所以我显然缺乏我在任何地方都找不到的理解。再次感谢您对此的任何帮助!

float incDecValue(float value) {

        //This is important, because the libraries are sharing pins
        pinMode(XM, OUTPUT);
        pinMode(YP, OUTPUT);

        value;
        SD.remove("pnum.txt");
        myFile = SD.open("pnum.txt", FILE_WRITE);
        myFile.print(value);
        myFile.close();
        counter = 0;
        myFile = SD.open("pnum.txt");
        if (myFile) {
          while (myFile.available()) {
            testChar[counter] = myFile.read();
            counter++;
          }
          myFile.close();
         }
         float convertedValue = atof(testChar);
         return convertedValue;
    }

我会这样称呼它。

pValue = incValue(pValue+=.5);

由于不完全知道你真正想要什么,我做了以下假设:

  • 您想将单个浮点值保存到名为 pnum.txt
  • 的文件中
  • 您想用那个值做点什么
  • 您希望将处理后的值写回文件pnum.txt(覆盖内容)
  • 两个不同的函数参数化,每个函数都以 fileName 作为输入和 write 的值

所以这里有一个完整的序列(如发布的那样工作),您可以轻松地将其实现到您的代码中。没有使用字符串 class 并且只有一行文件 read/write:

/* SD card pid read/write

  The circuit:
     SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4   */

#include <SPI.h>
#include <SD.h>
const uint8_t chipSelect = 4;
char dataChar[16] = {'[=10=]'};

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect.
  }
  Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed or no card present");
    return;
  }
  Serial.println("Card initialized");
}

void loop() {
  float pValue = readFileValue ("pnum.txt") + 0.5;
  writeFileValue ("pnum.txt", pValue);
}

float readFileValue (const char* fileName) {
  /* Open the file. note that only one file can be open at a time,
    so you have to close this one before opening another.*/
  File dataFile = SD.open(fileName, FILE_READ);
  // if the file is available, write to it:
  if (dataFile) {
    char c;
    uint8_t i = 0;
    while (dataFile.available()) {
      c = dataFile.read();   // Read char by char
      if (c != '\n') {      // As long no line terminator
        dataChar[i] = c;
        i++;
      }
      else {
        dataChar[i] = '[=10=]';  // Terminate char array properly
        dataFile.close();
        break;
      }
    }
    Serial.print("Success writing content: ");
    Serial.println(dataChar);
  }
  else {   // If the file isn't open, pop up an error:
    Serial.print("Error opening requested file: ");
    Serial.println(fileName);
  }
  float fileVal = atof(dataChar);;
  return fileVal;
}

bool writeFileValue (const char* fileName, float fileVal) {
  SD.remove(fileName); // Delete the existing file if existing 
  File dataFile = SD.open(fileName, FILE_WRITE);
  // If the file opened okay, write to it
  if (dataFile) {
    // dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
    dtostrf(fileVal, 5, 2, dataChar);  // Not really needed for this simple issue, but ..
    Serial.print("Writing to file: ");
    Serial.println(fileName);
    dataFile.println(dataChar);
    // Close the file
    dataFile.close();
    Serial.println("Success saving done");
    return true;
  } else {
    // if the file didn't open, print an error:
    Serial.println("Error opening file: ");
    Serial.println(fileName);
    return false;
  }
}