Laser Tag semi automatic IR ability


again can master me concept of adding semi auto firing program.


#include <wire.h>
#include "irremote.h"
#include <liquidcrystal_i2c.h>

liquidcrystal_i2c lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, positive);


const int irsensorpin  = 2;    // ir sensor
const int fireledpin   = 3;    // ir led
const int triggerpin   = 4;    // trigger switch
const int reloadpin    = 6;    // reloading switch
const int myammoledpin = 9;    // out of ammo led
const int speakerpin   = 12;   // piezo speaker
const int hitledpin    = 13;   // hit indicator

// game play variables
int mycode             = 1;    // unused @ moment
int maxhealth          = 3;    // number of times can shot before dying
int myhealth           = maxhealth;
int maxammo            = 30;   // amount of ammo returned on reload
int ammocount          = 30;   // starting ammo
int deadtime           = 3000; // number of ms stay dead
int deathcount         = 0;
int score = 000;
int = 1;

// initial states state stores
int triggerstate       = low;       
int hitstate           = high;
int reloadstate        = low;
int lasttriggerstate   = low;   
int lasthitstate       = high;
int lastreloadstate    = low;

// setup ir sender , receiver routines.
irsend irsend;
irrecv irrecv(irsensorpin);
decode_results results;      // stores ir receiver results

// setup routine, runs once @ beginning.
void setup() {
  // initialize button/sensor pins input:
  pinmode(triggerpin, input);
  pinmode(irsensorpin, input);
  pinmode(reloadpin, input);
  // initialize led/speaker output:
  pinmode(fireledpin, output);
  pinmode(myammoledpin, output);
  pinmode(hitledpin, output);
  pinmode(speakerpin, output);
  // initialize serial communication debug
  serial.begin(9600);
   lcd.begin(16,2);
 
 
  // start receiver listening
  irrecv.enableirin();
 
  // startup tone generator
   for (int = 1;i < 254;i++) { // loop plays start noise
     playtone((3000-9*i), 2);
    digitalwrite(hitledpin, high);
   
  }

  serial.println("ready.");
  lcd.setcursor(4,0);
    lcd.print("ready!!");
    lcd.setcursor(1,1);
    lcd.print("destroyer!");
}

// main loop
void loop() { 
   int khz = 38; // 38khz carrier frequency nec protocol
  unsigned int irsignal[67] = {8950,4500, 550,600, 500,600, 500,600, 550,550, 550,600, 550,550, 550,600, 500,600, 550,1700, 500,1700, 600,1700, 500,1700, 550,1650, 600,1700, 500,550, 600,1700, 500,1750, 500,600, 550,550, 550,550, 550,600, 550,550, 550,600, 500,600, 550,550, 550,1700, 500,1750, 500,1700, 600,1650, 550,1700, 500,1700, 600,1700, 500}; //analysir batch export (irremote) - raw
  // check if there message waiting can decoded
  if (irrecv.decode(&results)) {
    serial.println(results.value, dec);

    // check if valid hit routine
    // should firing ir code of not
    // player1 = 3978641416;
    // player2 = 275977667;
    // todo: need change accept range more 2 'teams' can in play
    //       probalby shouldn't doing gross decimal parsing on binary values.
   
    if (results.value == 16724175) {
      serial.println("hit hit hit");
      digitalwrite(hitledpin, high);
      delay (100);
      digitalwrite(hitledpin, low);
     // todo: less lame hit sound
      playtone(2500, 200);
      lcd.clear();
      lcd.print("hit by....");
      lcd.setcursor(3,1);
      lcd.print("black hawk!");
       lcd.setcursor(1, 1);
        lcd.print (myhealth);
      // reduce health , check if should dead or not
      myhealth--;
      if (myhealth <= 0) {
        serial.println("dead!");
         lcd.clear();
        lcd.print("game on pig!");
        deathcount++;
        score = + score;
        lcd.setcursor(0, 1);
  // score sum total
  lcd.print(score);
        playtone(5000, 1000);     
        delay(deadtime);
        myhealth = maxhealth;
        ammocount = maxammo;       
        serial.println("ready again!");
        lcd.clear ();
        lcd.print("fire!!go go go");
        digitalwrite(hitledpin, high);
        (int = 1;i < 254;i++)
        playtone((3000-9*i), 2);
     
      } else {
        serial.print("health: ");
        serial.println(myhealth);
      }
      digitalwrite(hitledpin, low);

     
    } else {
      serial.println("ir signal received not recognized");
      digitalwrite(hitledpin, low);

    }

    irrecv.resume(); // receive next value
  } 

  // read pushbutton input pins
  triggerstate = digitalread(triggerpin);
  reloadstate = digitalread(reloadpin);

  // check if state has changed reload pin , perform reload if it's been pushed
  if (reloadstate != lastreloadstate) {
    if (reloadstate == high) {
       serial.println("reloading!");
       lcd.clear();
       lcd.print("reloading!");
       lcd.clear();
       lcd.print("ammo=30! fire!");
       ammocount = maxammo;
       playtone(1000, 100);
       playtone(500, 100);

    }
  }   
 
  // compare triggerstate previous state
  if (triggerstate != lasttriggerstate) {
    if (ammocount > 0) {   
      if (triggerstate == high) {
        ammocount--;
        serial.println("trigger on");
        serial.print("ammo:  ");
        serial.println(ammocount);
         lcd.clear();
     lcd.setcursor(5,1);
        lcd.print (ammocount);
        lcd.print (" = ammo");
        lcd.setcursor(2,0);
        lcd.print ("destroyer");
        digitalwrite(myammoledpin, high);

        // todo: less lame firing sound
        playtone(600, 10);
        playtone(500, 10);
        playtone(400, 10);
        playtone(300, 10);
     
        // fire ir led (3x try , make sure received , keep sony protocol)
        // should firing ir code are
        // player1 = 0x00b
        // player2 = 0xa90
       irsend.sendraw(irsignal, sizeof(irsignal) / sizeof(irsignal[67]), khz);
       
        irrecv.enableirin();
       
      } else {
        serial.println("trigger off");
        digitalwrite(myammoledpin, low);
      }

    } else {
      // out of ammo flash display accordingly , play error noise if try , fire 
      //digitalwrite(fireledpin, low);
      digitalwrite(myammoledpin, high);
      serial.println("out of myammo!");
      playtone(5000, 100);
      //delay(40);

    }
     
    lasttriggerstate = triggerstate;
   }
}

// play sound on piezo
void playtone(int atone, int duration) { // sub routine playing tones standard arduino melody example
for (long = 0; < duration * 1000l; += atone * 2) {
    digitalwrite(speakerpin, high);
    delaymicroseconds(atone);
    digitalwrite(speakerpin, low);
    delaymicroseconds(atone);
  }
}

look in code @ line

code: [select]
// compare triggerstate previous state
  if (triggerstate != lasttriggerstate) {
...
} else


that's makes wait trigger release. need define want yo if come during next loop thee triggerstate being firing


Arduino Forum > Using Arduino > Programming Questions > Laser Tag semi automatic IR ability


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'