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!