0

I'm new to the Arduino and I asked some questions around here and they were really helpful so I'm hoping I will get help once more

#define maxLen 800
volatile unsigned int irBuffer[maxLen]; //stores timings - volatile because changed by ISR
volatile unsigned int x = 0; //Pointer thru irBuffer - volatile because changed by ISR
void setup() {
 Serial.begin(9600); //change BAUD rate as required
 attachInterrupt(0, rxIR_Interrupt_Handler, CHANGE);//set up ISR for receiving IR signal
}
void loop() {
 Serial.println(F("Press the button on the remote now - once only"));
 delay(5000); // pause 5 secs
 if (x) { //if a signal is captured
 Serial.println();
 Serial.print(F("Raw: (")); //dump raw header format - for library
 Serial.print((x - 1));
 Serial.print(F(") "));
 detachInterrupt(0);//stop interrupts & capture until finshed here
 for (int i = 1; i < x; i++) { //now dump the times
 if (!(i & 0x1)) Serial.print(F(" "));
 Serial.print(irBuffer[i] - irBuffer[i - 1]);
 Serial.print(F(", "));
 }
 x = 0;
 Serial.println();
 Serial.println();
 attachInterrupt(0, rxIR_Interrupt_Handler, CHANGE);//re-enable ISR for receiving IR signal
 }
}
void rxIR_Interrupt_Handler() {
 if (x > maxLen) return; //ignore if irBuffer is already full
 irBuffer[x++] = micros(); //just continually record the time-stamp of signal transitions
}

I would like to modify the code above to write to an SD file instead of printing it on the serial port. I've tried to use myFile.write or myFile.print but it won't work. Maybe I am just not that aware of how the SD functions work. Any tips? This is a function to capture IR signals. I require them for a Arduino remote. The modified code:

//you may increase this value on Arduinos with greater than 2k SRAM
#define maxLen 800
#include <SPI.h>
#include <SD.h>
File myFile;
volatile unsigned int irBuffer[maxLen]; //stores timings - volatile because changed by ISR
volatile unsigned int x = 0; //Pointer thru irBuffer - volatile because changed by ISR
void setup() {
 Serial.begin(9600); //change BAUD rate as required
 attachInterrupt(0, rxIR_Interrupt_Handler, CHANGE);//set up ISR for receiving IR signal
}
void loop() {
 // put your main code here, to run repeatedly:
 Serial.print("Initializing SD card...");
 if (!SD.begin(53)) {
 while (1);
 }
 Serial.println("initialization done.");
 // 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);
 Serial.println(F("Press the button on the remote now - once only"));
 delay(5000); // pause 5 secs
 if (x) { //if a signal is captured
 Serial.println();
 myFile.print(F("Raw: (")); //dump raw header format - for library
 myFile.print((x - 1));
 myFile.print(F(") "));
 detachInterrupt(0);//stop interrupts & capture until finshed here
 for (int i = 1; i < x; i++) { //now dump the times
 if (!(i & 0x1)) myFile.write(F(" "));
 myFile.print(irBuffer[i] - irBuffer[i - 1]);
 myFile.print(F(", "));
 }
 x = 0;
 Serial.println();
 Serial.println();
 attachInterrupt(0, rxIR_Interrupt_Handler, CHANGE);//re-enable ISR for receiving IR signal
 }
}
void rxIR_Interrupt_Handler() {
 if (x > maxLen) return; //ignore if irBuffer is already full
 irBuffer[x++] = micros(); //just continually record the time-stamp of signal transitions
}
asked Aug 29, 2019 at 6:29
9
  • How is it not working? What is actually happening? And why don't you include the actual code in question (the SD card code), so that we can search for errors? Commented Aug 29, 2019 at 6:51
  • The code is in the question. The code is working, it prints to the serial monitor. I just want to modifiy it to 'print' to a file in an SD card. Commented Aug 29, 2019 at 7:04
  • 1
    You wrote, that you already tried to use the SD library to do so. But I cannot see this in your code. We need to see this, so that we can find possible errors in there. We will not simply write the code for you, as we are not a free code writing service. Explain what exactly the problem was, when trying to use the SD library. Otherwise your question is too broad. Commented Aug 29, 2019 at 7:16
  • file.print() should work exactly the same as Serial.print(). But you have to open the file first. Did you read the documentation of the SD library? The examples? Anything you didn't understand in these docs? Commented Aug 29, 2019 at 7:19
  • I just used file.print file instead that's why I dind't upload the code. I just need help to see where the problem is, not for someone to write my code. I will add it to the question right away. Commented Aug 29, 2019 at 7:53

1 Answer 1

2

You are never closing the file. The SD library (owing to how SD cards work) operates around a 512 byte buffer. Only when that buffer is full, or the library is manually instructed, will that buffer ever get written to the SD card.

The two ways of forcing the buffer to be written to the card are:

  • Close the file with myFile.close() or
  • Flush the file with myFile.flush()

Unless you do one of those after storing your data, since you are writing less than 512 bytes, the data will be lost when you power off (or reset) or remove the SD card.

answered Aug 29, 2019 at 10:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.