Hello,
I have written this code to read a text file from an SD card, the file contains the definitions for an IR remote, the file is in the format = #, as in the extract below:
C0E8=PWR # Power
C091=PLY # Play
C04D=REC # Record
C061=STP # Stop
C001=PSE # Pause
C07D=FFW # Fast Forward
What want to do is read the IR code into an char* array and my attempt to do this does not work:
#include <SD.h>
File myFile;
char* irCommand[33];
boolean setIRbuttons() {
char irCmd[5];
int i = 0;
int ic = 0;
int ne = 0;
myFile = SD.open("IR_SONY.TXT");
if (myFile) {
while (myFile.available()) {
int c = myFile.read();
if (i < 4) { // read first four bytes of line
irCmd[ic++] = c;
irCmd[ic] = '\0';
}
i++;
if (c == '\n' || c== '\r') {
i = 0; // end of line reached, reset some line counters
ic = 0;
Serial.print(ne);
Serial.print(". irCmd: ");
Serial.println(irCmd); // print the char
irCommand[ne] = irCmd; // add the line to the array
Serial.print(ne);
Serial.print(". irCommand: ");
Serial.println(irCommand[ne]); // print the array
ne++;
}
}
myFile.close();
}
else {
Serial.println("could not open ");
return false;
}
return true;
}
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("failed!");
return;
}
Serial.println("done.");
setIRbuttons();
Serial.println("irCommands in array");
for (int i=0; i < 33; i++) {
Serial.print(i);
Serial.print(". ");
Serial.println(irCommand[i]);
}
}
void loop()
{
// nothing happens after setup
}
it is supposed to copy each IR command into irCommand. But I end up with each element or irCommand containing only the last command processed, as in this extract of the output:
Initializing SD card...done.
0. irCmd: C0E8
0. irCommand: C0E8
1. irCmd: C091
1. irCommand: C091
2. irCmd: C04D
2. irCommand: C04D
3. irCmd: C061
3. irCommand: C061
4. irCmd: C001
4. irCommand: C001
5. irCmd: C07D
5. irCommand: C07D
ir commands in array
0. C07D
1. C07D
2. C07D
3. C07D
4. C07D
5. C07D
can anyone explain why irCommand is not being filled in? I wrote a little test programme to try this, which does work:
char* copyCat[6];
boolean satCat() {
char* catSat[] = {"The", "Cat", "Sat", "on", "the", "Mat"};
for (int i=0; i < 6; i++) {
Serial.print(i);
Serial.print(". ");
Serial.println(catSat[i]);
copyCat[i] = catSat[i];
}
return true;
}
void setup() {
Serial.begin(9600);
satCat();
Serial.println("copied cat");
for (int i=0; i < 6; i++) {
Serial.print(i);
Serial.print(". ");
Serial.println(copyCat[i]);
}
}
void loop()
{
// nothing happens after setup
}
which has left me confused.
can you help?
Thanks
Karl