Sending IR codes - problem

Hello, I am trying to make a 'grenade' for laser tag. It will allow me to record the code that my gun sends at the start of the program (by recording the IR code when I shoot my gun into it), and later it will send out that code in many directions using IR LEDs a couple of seconds after I press a button. I have been using my TV and its remote control for testing.

Since I don't know the protocol that the laser guns will use, I have decided to work with the raw IR codes rather than trying to decode them. I have taken most of the code from these two youtube videos:

The code he uses in these videos can be found here (chapter 13): https://goo.gl/lhCidhttps://goo.gl/lhCid

I have also used the code from the Arduino debounce tutorial.

I have combined the code from the two videos and changed it a bit, in an attempt to record the raw code and then send it out again.
When I use the exact code from the first video to capture the IR data for a button on my TV remote, and then use it in the code from the second video, it works - my TV responds to the signal. However, when I try and do the same using my all-in-one program, there is no response. I am not sure what the problem is, but I think it might be to do with how I am using arrays to store the IR data.

Here is my code:
// If one keypress results in multiple codes being output, then
// change in IRremoteInt.h:
// #define _GAP 50000

#include <IRremote.h>

int RECV_PIN = 11;
const int buttonPin = 2;
const int ledPin = 13;

int ledState = LOW;
int buttonState;
int lastButtonState = LOW;

//bool buttonPressed = false;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers

IRrecv irrecv(RECV_PIN);
decode_results results;

IRsend irsend;
const int sizeOfFirstArray = 500;
int nonZeroEntityCount = 1;
unsigned int myRawCodeArray[sizeOfFirstArray];

int c = 1;

void dump(decode_results *results) {
int count = results->rawlen;
Serial.println(c);
c++;
Serial.println("For IR Scope: ");
for (int i = 1; i < count; i++) {

if ((i % 2) == 1) {
Serial.print("+");
Serial.print(results->rawbuf_*USECPERTICK, DEC);_

  • }*
  • else {*
    Serial.print(-(int)results->rawbuf_USECPERTICK, DEC);_
    _
    }_
    _
    Serial.print(" ");_
    _
    }_
    _
    Serial.println("");_
    _
    Serial.println("For Arduino sketch: ");_
    _
    Serial.print("unsigned int raw[");_
    _
    Serial.print(count, DEC);_
    _
    Serial.print("] = {");_
    _
    for (int i = 1; i < count; i++) {*_

* if ((i % 2) == 1) {*
Serial.print(results->rawbuf_USECPERTICK, DEC);
myRawCodeArray = results->rawbufUSECPERTICK;
}

* else {
Serial.print((int)results->rawbufUSECPERTICK, DEC);
myRawCodeArray = (int)results->rawbufUSECPERTICK;
}

Serial.print(",");
}
Serial.print("};");
Serial.println("");
Serial.print("irsend.sendRaw(raw,");
Serial.print(count, DEC);
Serial.print(",38);");
Serial.println("");
Serial.println("");
for (int i = 1; i <= sizeOfFirstArray; i++){
if (myRawCodeArray != 0){
nonZeroEntityCount++;
}
}
}
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);*_

* for (int i = 0; i < 500; i++){*
_ myRawCodeArray = 0;
* }*_

* Serial.begin(9600);*
* irrecv.enableIRIn(); // Start the receiver*
* digitalWrite(13, HIGH);*
* while (irrecv.decode(&results) == false){} //pause here until IR data is received*
* digitalWrite(13, LOW);*

* if (irrecv.decode(&results)) {*
* dump(&results);*
* //irrecv.resume(); // Receive the next value*
* }*
_ /
unsigned int myCutDownArray[nonZeroEntityCount];
for (int i = 1; i <= nonZeroEntityCount; i++){_

myCutDownArray _= myRawCodeArray;
}*

*/_

* Serial.println("End of setup loop");*
}
void loop() {
* // read the state of the switch into a local variable:*
* int reading = digitalRead(buttonPin);*
* // If the switch changed, due to noise or pressing:*
* if (reading != lastButtonState) {*
* // reset the debouncing timer*
* lastDebounceTime = millis();*
* }*
* if ((millis() - lastDebounceTime) > debounceDelay) {*
* // whatever the reading is at, it's been there for longer than the debounce*
* // delay, so take it as the actual current state:*
* // if the button state has changed:*
* if (reading != buttonState) {*
* buttonState = reading;*
* // only turn on the LED if the new button state is HIGH*
* if (buttonState == HIGH) {*
* //ledState = HIGH;*
* for (int i = 1; i <= 20; i++){*
* digitalWrite(ledPin, HIGH);*
* delay(60);*
* digitalWrite(ledPin, LOW);*
* delay(60);*
* ledState = LOW; *
* }*
* for (int i = 0; i < 3; i++){*
* irsend.sendRaw(myRawCodeArray,nonZeroEntityCount,38); //SEND THE IR CODE*
* }*

* Serial.println("Code sent!");*
* Serial.println("Printing sent Array:");*
* for (int i = 0; i <nonZeroEntityCount; i++){*
_ Serial.println(myRawCodeArray*);
}
Serial.println("nonZeroEntityCount = ");
Serial.print(nonZeroEntityCount);*_

* }*
* else {*
* ledState = LOW;*
* }*
* }*
* }*
* // set the LED:*
* digitalWrite(ledPin, ledState);*
* // save the reading. Next time through the loop, it'll be the lastButtonState:*
* lastButtonState = reading;*

* delay(10);*
}
I know the code is a bit sloppy. This is a project that I need to get done very quickly and I don't have much spare time right now.
Any help will be appreciated. Thank you!

Sorry, the code hasn't quite come out right in the original post. Here is the file:

IR-Record_and_Send.ino (3.99 KB)

If you read the how to use the forum-please read stickies you will see how to properly post code. The sticky is in in bold type at the top of every topic. Apparently that makes it nearly invisible, somehow.

CODE:

// If one keypress results in multiple codes being output, then
// change in IRremoteInt.h:
// #define _GAP 50000

#include <IRremote.h>

int RECV_PIN = 11;
const int buttonPin = 2;
const int ledPin = 13;

int ledState = LOW;
int buttonState;
int lastButtonState = LOW;

//bool buttonPressed = false;
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

IRrecv irrecv(RECV_PIN);
decode_results results;

IRsend irsend;
const int sizeOfFirstArray = 500;
int nonZeroEntityCount = 1;
unsigned int myRawCodeArray[sizeOfFirstArray];

int c = 1;

void dump(decode_results *results) {
  int count = results->rawlen;
  Serial.println(c);
  c++;
  Serial.println("For IR Scope: ");
  for (int i = 1; i < count; i++) {
   
  if ((i % 2) == 1) {
    Serial.print("+");
    Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
  }
  else {
    Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
  }
  Serial.print(" ");
  }
  Serial.println("");
  Serial.println("For Arduino sketch: ");
  Serial.print("unsigned int raw[");
  Serial.print(count, DEC);
  Serial.print("] = {");
  for (int i = 1; i < count; i++) {
   
  if ((i % 2) == 1) {
    Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
    myRawCodeArray[i] = results->rawbuf[i]*USECPERTICK;
  }
  else {
    Serial.print((int)results->rawbuf[i]*USECPERTICK, DEC);
    myRawCodeArray[i] = (int)results->rawbuf[i]*USECPERTICK;
  }
  Serial.print(",");
  }
  Serial.print("};");
  Serial.println("");
  Serial.print("irsend.sendRaw(raw,");
  Serial.print(count, DEC);
  Serial.print(",38);");
  Serial.println("");
  Serial.println("");

  for (int i = 1; i <= sizeOfFirstArray; i++){
    if (myRawCodeArray[i] != 0){
      nonZeroEntityCount++;
    }
  }
}

void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);
  
  for (int i = 0; i < 500; i++){
  myRawCodeArray[i] = 0;
  }
  
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  digitalWrite(13, HIGH);
  while (irrecv.decode(&results) == false){}  //pause here until IR data is received
  digitalWrite(13, LOW);
  
  if (irrecv.decode(&results)) {
  dump(&results);
  //irrecv.resume(); // Receive the next value
  }

  /*
  unsigned int myCutDownArray[nonZeroEntityCount];
  for (int i = 1; i <= nonZeroEntityCount; i++){
    myCutDownArray[i] = myRawCodeArray[i];
  }
  */
  
  Serial.println("End of setup loop");
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only turn on the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        //ledState = HIGH;
        for (int i = 1; i <= 20; i++){
          digitalWrite(ledPin, HIGH);
          delay(60);
          digitalWrite(ledPin, LOW);
          delay(60);
          ledState = LOW;       
        }

        for (int i = 0; i < 3; i++){
          irsend.sendRaw(myRawCodeArray,nonZeroEntityCount,38);  //SEND THE IR CODE
        }
        
        Serial.println("Code sent!");

        Serial.println("Printing sent Array:");
        for (int i = 0; i <nonZeroEntityCount; i++){
          Serial.println(myRawCodeArray[i]);
        }

        Serial.println("nonZeroEntityCount = ");
        Serial.print(nonZeroEntityCount);
        
      }
      else {
        ledState = LOW;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
    
  delay(10);
}

and the code that does work please...

"I have been using my TV and its remote control for testing."

What type of TV ? Sony? Samsung ? LG ?

There are a few different libraries around. I have just created a project using this one:

As there are a few IR libraries with similar names - can you please confirm the library you are using ?
Also in your combined sketch - can you confirm that the signal you are receiving is correct?

Just trying to work out - where in your long sketch to focus my attention to ?