0

I have a text file with an IR raw code that I need to read from the SD in order to send it and I don't know how to do it. I tried to use the read file function from the specific SD library but it didn't work.

irsend.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), khz);

I want to save the code from the SD in the irSignal so I can sent it. Any ideas?

The code I've tried

#include <SPI.h>
#include <SD.h>

#include <IRremote.h>

IRsend irsend;

File myFile;

int k=0;
unsigned int irSignal[227];

void setup()
{
  Serial.begin(9600);


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


  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  myFile = SD.open("sc_17.txt");
  if (myFile) {
    while (myFile.available()) {
      int val = myFile.parseInt();
      Serial.println(val);
      irSignal[k]={val};
      k++;
    }
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening dc_17.txt");
  }
}

void loop()
{
  int khz = 38;
  irsend.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), khz); //Note the approach used to automatically calculate the size of the array.
  sendSignal();
  delay(2000); //In this example, the signal will be repeated every 5 seconds, approximately.
}

The problem is when I try to implement it in a separate function, because I need to call it for more files, it doesn't work.

  • What is the code you used for reading from the SD card (the read file function), and what exactly means ' it didn't work' ? – Michel Keijzers Aug 27 '19 at 10:42
  • I will add the code to the question and modify it – Teofil Sandu Aug 27 '19 at 10:51
  • shouldn't it be: irSignal[k]=val; ? – Michel Keijzers Aug 27 '19 at 11:00
  • show the not working code – Juraj Aug 27 '19 at 11:04
  • I added to the question. I put the code from the setup and the one from the loop in a separate function and it worked. Thanks ! – Teofil Sandu Aug 29 '19 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.