Groove, I managed to come to the same conclusion as you this morning but got stuck on the rawCodes[RAWBUF] part. I eventually realised that I could make this into a 2D array and made it into rawCodes[RAWBUF][10]. I have altered the rest of the code (eg codeType[buttonNumber]) and the sketch has compiled successfully. Now to test it and see if I can get it saving codes again!
[edit]I have uploaded the code, but now the Arduino does nothing at all! It does not record code, does not respond to button presses and will not show anything in the serial monitor. The updated code is shown below:
#include <IRremote.h>
int RECV_PIN = 11;
int BUTTON_PIN = 12;
int STATUS_PIN = 13;
IRrecv irrecv(RECV_PIN);
IRsend irsend;
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(BUTTON_PIN, INPUT);
pinMode(STATUS_PIN, OUTPUT);
}
// Storage for the recorded code
int codeType[10] = {-1}; // The type of code
unsigned long codeValue[10]; // The code value if not raw
unsigned int rawCodes[RAWBUF][10]; // The durations if raw
int codeLen[10]; // The length of the code
int toggle = 0; // The RC5/6 toggle state
// Stores the code for later playback
// Most of this code is just logging
void storeCode(decode_results *results, int buttonNumber) {
codeType[buttonNumber] = results->decode_type;
int count = results->rawlen;
if (codeType[buttonNumber] == UNKNOWN) {
Serial.println("Received unknown code, saving as raw");
codeLen[buttonNumber] = results->rawlen - 1;
// To store raw codes:
// Drop first value (gap)
// Convert from ticks to microseconds
// Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
for (int i = 1; i <= codeLen[buttonNumber]; i++) {
if (i % 2) {
// Mark
rawCodes[i - 1][buttonNumber] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
Serial.print(" m");
}
else {
// Space
rawCodes[i - 1][buttonNumber] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
Serial.print(" s");
}
Serial.print(rawCodes[i - 1][buttonNumber], DEC);
}
Serial.println("");
}
else {
if (codeType[buttonNumber] == NEC) {
Serial.print("Received NEC: ");
if (results->value == REPEAT) {
// Don't record a NEC repeat value as that's useless.
Serial.println("repeat; ignoring.");
return;
}
}
else if (codeType[buttonNumber] == SONY) {
Serial.print("Received SONY: ");
}
else if (codeType[buttonNumber] == RC5) {
Serial.print("Received RC5: ");
}
else if (codeType[buttonNumber] == RC6) {
Serial.print("Received RC6: ");
}
else {
Serial.print("Unexpected codeType ");
Serial.print(codeType[buttonNumber], DEC);
Serial.println("");
}
Serial.println(results->value, HEX);
codeValue[buttonNumber] = results->value;
codeLen[buttonNumber] = results->bits;
}
}
void sendCode(int repeat, int buttonNumber) {
if (codeType[buttonNumber] == NEC) {
if (repeat) {
irsend.sendNEC(REPEAT, codeLen[buttonNumber]);
Serial.println("Sent NEC repeat");
}
else {
irsend.sendNEC(codeValue[buttonNumber], codeLen[buttonNumber]);
Serial.print("Sent NEC ");
Serial.println(codeValue[buttonNumber], HEX);
}
}
else if (codeType[buttonNumber] == SONY) {
irsend.sendSony(codeValue[buttonNumber], codeLen[buttonNumber]);
Serial.print("Sent Sony ");
Serial.println(codeValue[buttonNumber], HEX);
}
else if (codeType[buttonNumber] == RC5 || codeType[buttonNumber] == RC6) {
if (!repeat) {
// Flip the toggle bit for a new button press
toggle = 1 - toggle;
}
// Put the toggle bit into the code to send
codeValue[buttonNumber] = codeValue[buttonNumber] & ~(1 << (codeLen[buttonNumber] - 1));
codeValue[buttonNumber] = codeValue[buttonNumber] | (toggle << (codeLen[buttonNumber] - 1));
if (codeType[buttonNumber] == RC5) {
Serial.print("Sent RC5 ");
Serial.println(codeValue[buttonNumber], HEX);
irsend.sendRC5(codeValue[buttonNumber], codeLen[buttonNumber]);
}
else {
irsend.sendRC6(codeValue[buttonNumber], codeLen[buttonNumber]);
Serial.print("Sent RC6 ");
Serial.println(codeValue[buttonNumber], HEX);
}
}
else if (codeType[buttonNumber] == UNKNOWN /* i.e. raw */) {
// Assume 38 KHz
irsend.sendRaw(rawCodes[buttonNumber], codeLen[buttonNumber], 38);
Serial.println("Sent raw");
}
}
int lastButtonState;
void loop() {
// If button pressed, send the code.
int buttonState = digitalRead(BUTTON_PIN);
if (lastButtonState == HIGH && buttonState == LOW) {
Serial.println("Released");
irrecv.enableIRIn(); // Re-enable receiver
}
if (buttonState) {
Serial.println("Pressed, sending");
digitalWrite(STATUS_PIN, HIGH);
sendCode(lastButtonState == buttonState, 1);
digitalWrite(STATUS_PIN, LOW);
delay(50); // Wait a bit between retransmissions
}
else if (irrecv.decode(&results)) {
digitalWrite(STATUS_PIN, HIGH);
storeCode(&results, 1);
irrecv.resume(); // resume receiver
digitalWrite(STATUS_PIN, LOW);
}
lastButtonState = buttonState;
}
[/edit]