Hello,
I have an IR receiver that receives IR signals. I have various remotes (a Vizio one and a Sony one) that each send different signals (like 12345 is the signal for the "1" button on Vizio, but 1939 is the "1" button on Sony's).
I'm trying to write a program that will store each of these signals in an array. I want to prompt (via Serial monitor) the user to press the "1" button, store that in the array, then prompt the user to press "2", store that, prompt to press "3", store that, etc. for numbers 1 - 9 (and 0) and the remote.
However, I can't seem to figure out how to wait for the code to actually WAIT for the IR to receive a signal, before continuing through the loop. What happens is I get
0.00
Please press button for 0
Please press button for 1
Please press button for 2
Please press button for 3
Please press button for 4
Please press button for 5
Please press button for 6
Please press button for 7
Please press button for 8
Please press button for 9
0.00
It should say "Please press button for 0", then wait until I press that button, store that in the array, then loop and ask for "1", etc.
Here's the code:
#include <IRremote.h>
// IR SENSOR SETUP ------------
const int IRpin = 8;
IRrecv irrecv(IRpin); // create instance of 'irrecv'
decode_results results; //create instance of 'decode_results'
// -------------------------
float btnCodes[9]; // This is the array for the remote's IR code when received by the IR.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
irrecv.enableIRIn(); // start the receiver
}
void loop() {
// put your main code here, to run repeatedly:
for (int i = 0; i <= 9; i++) {
String x = String(i);
Serial.print("Please press button for ");
Serial.println(x);
if (irrecv.decode(&results)) { // have we rec'd an IR signal?
Serial.println(results.value);
btnCodes[i] = Serial.read();
irrecv.resume(); // Receive the next value
delay(500);
}
}
Serial.println(btnCodes[1]);
}
At first, I had the code where the string would be btnCodes[i] = results.value
(in other words, avoid reading from the Serial monitor for the code, and get it straight from the IR). But, I thought I could have the Serial monitor set the value so the loop would stop. I think what's happening is now, the btnCodes[ i] is being set to literally "Please press button for 0".
Does this make sense? Thanks for any advice!