尝试在Arduino上读取遥控器并写入SD卡

时间:2019-06-08 05:52:51

标签: arduino sd-card remote-control

我正在尝试合并示例代码以从遥控器读取,然后将值写入SD卡。当我从遥控器获取值后尝试打开SD卡进行写入时,它无法打开。

如果我注释掉远程控制代码,我可以用文本写入SD卡,所以我知道SD卡没有问题。

#include <IRremote.h>
#include <SPI.h>
#include <SD.h>
int receiver_pin = 10;
IRrecv receiver(receiver_pin);
decode_results output;

void setup()
{
Serial.begin(9600);
while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(4)) {
     Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
  receiver.enableIRIn();
 }

void loop() {
if (receiver.decode(&output)) {
unsigned int value = output.value;

  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.println(value);
    dataFile.close();
    // print to the serial port too:
    Serial.println(value);
   }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
  receiver.resume();
}
}

每次我按遥控器上的按钮时,都会收到“打开datalog.txt出错”的信息。

我希望在按下远程键的值的情况下在SD卡上获取文件。

0 个答案:

没有答案