Hey, I'm making a laser tag system as my first project, I've been using the IRemote library, I've been having some troubles. First I'll tell you what it is supposed to do, when you press the BUTTON it turns on the LED for 250 ms and sends an IR signal, there is 1 second between shots and each shot takes 1 from the bullets variable. The bullets variable starts at 6 and when it reaches zero you have to wait 5 seconds for it to reload. If the IR sensor detects the signal that the gun sends it can't do anything for 10 seconds.
The problem I'm having is that you shouldn't be able to fire whilst you're reloading but you can and if you do the reloading light never turns off. I am also unsure as to whether the IR is actually working as IR is extremely complex, for me at least. Here is my code, I got the library from this site.
#include <IRremote.h>
#include <IRremoteInt.h>
IRsend irsend;
const int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long irCool = 100; //this sets the pause time for IR
unsigned long irTimer ;
const int LED = 13; //corresponds to LED, lights up when gun is fired (will be replaced with ir transmitter)
const int BUTTON = 7; //corresponds to BUTTON, used to fire gun
const int BULL = 2; //corresponds to LED, shows bullets
const int B2 = 1; //corresponds to LED, shows bullets
//pin three is an IR transmitter
const int B3 = 4; //corresponds to LED, shows bullets
const int B4 = 5; //corresponds to LED, shows bullets
const int B5 = 6; //corresponds to LED, shows bullets
const int B6 = 9; //corresponds to LED, shows bullets
const int OUT = 8; //corresponds to LED, lights up when reloading
const int HIT = 10; //corresponds to BUTTON, makes you go out (will be replaced with ir reciever)
unsigned long millisecond = 0;
bool outOfGame = false;
int bullets = 6; //changes amount of ammo
unsigned long outCool = 10000; //this changes how long you go out for
unsigned long outTimer;
bool reloading = false;
unsigned long reloadTimer;
unsigned long reloadCool = 5000; //this changes how long it takes to reload
unsigned long flashCool = 250; //this changes how long the laser stays on for
unsigned long flashTimer;
bool shooting = false;
unsigned long shootCool = 1000; //this changes fire rate
unsigned long shootTimer;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT); //set pinmodes
pinMode(BULL, OUTPUT);
pinMode(B2, OUTPUT);
pinMode(B3, OUTPUT);
pinMode(B4, OUTPUT);
pinMode(B5, OUTPUT);
pinMode(B6, OUTPUT);
pinMode(OUT, OUTPUT);
pinMode(HIT, INPUT);
pinMode(RECV_PIN, INPUT);
}
void loop()
{
// ()=================|| SETTING UP PER-TICK VARIABLES ||=================() \\
millisecond = millis();
bool pressingButton = (digitalRead(BUTTON) == HIGH) ? true : false;
// ()=================|| BEING HIT LOGIC ||=================() \\
//if you get hit by another gun (this uses IR) you have to wait 10 seconds before continuing
Being Hit = IR Recieve
bool beingHit = recieveIR();
if(outOfGame == true && millisecond >= outTimer)
{
outOfGame = false;
}
if(beingHit == true && !outOfGame)
{
outTimer = millisecond + outCool;
outOfGame = true;
}
// ()=================|| RELOADING LOGIC ||=================() \\
//you have 6 bullets in your gun, after you shoot your weapon 6 times you have to wait 5 seconds so it can 'reload'
if (bullets == 0) // Check if we have Bullets
{
if (reloading == false) // Are we Reloading?
{
reloadTimer = millisecond + reloadCool; //sets the time to 3 seconds in the future
reloading = true; //add 1 to reloading
digitalWrite(OUT, HIGH);
}
if (bullets == 0 && millisecond >= reloadTimer && reloading) // Has 3 Seconds Passed?
{
bullets = 6; //add 6 to bullets
reloading = false; //take 1 from reloading
digitalWrite(OUT, LOW);
}
}
// ()=================|| SHOOTING LED LOGIC ||=================() \\
//this turns of the LED after shooting and is also ends the logic
if(millisecond >= flashTimer && shooting == true)
{
digitalWrite(LED, LOW);
shooting = false;
}
// ()=================|| SHOOTING LOGIC ||=================() \\
//when the press the button the LED turns on for 250 milliseconds, sends an IR signal, you have to wait 1000 ms between shots and each shot takes 1 from the bullets variable
if (pressingButton == true && millisecond >= shootTimer && !outOfGame) // Are we Pressing the Button and are we Able to Shoot
{
if (bullets > 0 && !shooting)
{
shootTimer = millisecond + shootCool;
bullets -= 1;
digitalWrite(LED, HIGH);
sendIR();
flashTimer = millisecond + flashCool;
shooting = true;
}
//
}
}
// ()=================|| INFRARED SEND ||=================() \\
//this is the code for infrared sending, I don't completely understand this and my friend helped me with it
void sendIR()
{
if (Serial.read() != -1 && millisecond >= irTimer)
{
irTimer = millisecond + irCool;
for (int i = 0; i < 3; i++)
{
irsend.sendSony(0xa90, 12); // Sony TV power code
}
}
}
// ()=================|| INFRARED RECIEVE ||=================() \\
// this is the infrared recieve, again I don't understand it very well and my friend
bool recieveIR()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
return true;
}
return false;
}
[CODE]