Happy holidays All,
For the past few weeks I have been struggling with send and receiving I.R. codes from the same Arduino.
I am trying to develop a self contained vision system that can send a discreet signal out while also being able to detect if said signal bounces off an object. Initially my hope was that I could have the main loop cycle through sending out the signals while periodically scanning for signals via interrupts, but this did not work. I wrote the following code however it cannot seem to detect and I cannot figure out what I am missing.
I have tried to test my program by pointing an appropriately coded universal remote at the Uno just to see if it can detect signals from an independent source and while the signal is sometimes read, most of the time it does not detect the signal.
I suspect that either the code is sent before the I.R. receiver can react or that the I.R. receiver scan is so quick that it misses the signal or perhaps a combination of the two.
I also tried adding a timed loop to keep the I.R. receiver scanning for several milliseconds, but this did not work either.
Is there something fundamental that I am missing here?
// Send & Receive I.R. signals from Arduino Fio Rev. 2, JBL code 000 (NEC 32 bit protocol)
// Hex. Decimal
// 0 = 38863BC0 = 948321216
// 1 = 38863BE0 = 948321248
// 2 = 38863BD0 = 948321232
// 3 = 38863BF0 = 948321264
// 4 = 38863BC8 = 948321224
// 5 = 38863BE8 = 948321256
#include <IRremote.h> // use the library
const byte irReceiverPin = 9; // Pin the receiver is connected to.
IRrecv irrecv(irReceiverPin); // create instance of irrecv which points to Digital Pin 2.
IRsend irsend;
decode_results results;
long clock = 0;
byte LEDGround[] = {5, 8, 9, 10, 11, 12}; // Array which cycles (gound path) of Infrared LEDs. (Can the I.R Detectors be tied to I.R LED ground nodes?)
byte ReceiverPin[] = {7, 15, 16, 17, 18, 19}; // Array which cycles I.R. REceivers "On" & "Off"
long irKeyCodes[] = {
0x38863BC0, // 0 Key
0x38863BE0, // 1 Key
0x38863BD0, // 2 Key
0x38863BF0, // 3 Key
0x38863BC8, // 4 Key
0x38863BE8, // 5 Key
0x38863BD8, // 6 Key
0x38863BF8, // 7 Key
0x38863BC4, // 8 Key
0x38863BE4, // 9 Key
};
void setup()
{
//pinMode(irReceiverPin, INPUT); // Not required???
Serial.begin(9600); // Established serial communication at a rate of 9600 Baud
irrecv.enableIRIn(); // start the IR receiver
//attachInterrupt(0, analyze, FALLING); // Interrupt triggered by I.R. Receiver (Pin 2)
}
void loop()
{ // Start of Main Loop
for (byte i = 0; i < 6; i++) // Start of loop which cycles througn I.R. LED array.
{
irsend.sendNEC(irKeyCodes[i], 32); // Sends 0 - 5 I.R. Codes, 32 value equals number of Bits. Is NES code 32 Bit?
irrecv.enableIRIn(); // start the IR receiver, needs to be restarted, terminated when I.R. is sent above.
} // End of Main Loop
clock = (millis() + 200);
{
while (clock > millis())
{
if (irrecv.decode(&results)) // Have we received an IR signal?
{
switch(results.value)
{
case 0x38863BC0:
Serial.println("0");
break; // 0
case 0x38863BE0:
Serial.println("1");
break; // 1
case 0x38863BD0:
Serial.println("2");
break; // 2
case 0x38863BF0:
Serial.println("3");
break; // 3
case 0x38863BC8:
Serial.println("4");
break; // 4
case 0x38863BE8:
Serial.println("5");
break; // 5
case 0x38863BD8:
Serial.println("6");
break; // 6
case 0x38863BF8:
Serial.println("7");
break; // 7
case 0x38863BC4:
Serial.println("8");
break; // 8
case 0x38863BE4:
Serial.println("9");
break; // 9
}
}
}
irrecv.resume(); // reveive the next value
}
}
Thanks,
Z