Problems with IR send code

Hello, I'm having problems sending the same code or pulse from the IRremote library. it doesn't send the same code that I've programmed it to. it's like it's not listening to the include library part. should included libraries be orange because the irremote library.h is black.

any advice would be greatly appreciated

thanks!

The colour doesn't matter. If it compiled successfully, there must be another reason why it doesn't work. I also have some code that doesn't work. Would you like to try to guess why?

I want to send the same IR code everytime I press a button. I don'tknow why it sends random codes. I've kept to the sony protocol and copied the power IRcode on my TV remote, but I don't know why it's random!

Nobody else knows either, and never will, unless you post your code, using code tags please!

i'm downloading the latest IDE which is taking forever....when I've updated my IDE i'll let you know

Thanks oh and what transistor are you refrerring to?

SAMHAWK:
i'm downloading the latest IDE which is taking forever....when I've updated my IDE i'll let you know

Thanks oh and what transistor are you refrerring to?

It's a line from the Steely Dan song, Bad Sneakers.

here is the code

lasertag.ino (8.79 KB)

Your code

#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include "IRremote.h"
#include <LiquidCrystal.h>


// Defining bitmasks for custom letters on the segment display
#define BM_A     0b01110111
#define BM_D     0b00111111
#define BM_E     0b01111001
#define BM_G     0b00111101
#define BM_O     0b00111111
#define BM_P     0b01110011
#define BM_S     0b01101101
#define BM_U     0b00111110
#define BM_DASH  0b01000000
#define BM_CLEAR 0b00000000


Adafruit_7segment matrix = Adafruit_7segment();

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 = A0;    // Out of Ammo LED
const int speakerPin   = 5;   // Piezo Speaker
const int hitLedPin    = 13;   // Hit Indicator

// Game play variables
int myCode             = 1;    // Unused at the moment
int maxHealth          = 3;    // Number of times you can get shot before dying
int myHealth           = maxHealth;
int maxAmmo            = 30;   // Amount of ammo returned on a reload
int ammoCount          = 30;   // Starting ammo
int deadTime           = 3000; // Number of ms you stay dead
int deathCount         = 0;

// Initial states for state stores
int triggerState       = LOW;
int hitState           = HIGH;
int reloadState        = LOW;
int lastTriggerState   = LOW;
int lastHitState       = HIGH;
int lastReloadState    = LOW;

// Setup the IR sender and receiver routines.
IRsend irsend;
IRrecv irrecv(irSensorPin);
decode_results results;      // Stores the IR receiver results

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// Setup routine, runs once at beginning.
void setup() {
  // initialize the button/sensor pins as input:
  pinMode(triggerPin, INPUT);
  pinMode(irSensorPin, INPUT);
  pinMode(reloadPin, INPUT);
  // initialize the LED/Speaker as output:
  pinMode(fireLedPin, OUTPUT);
  pinMode(myAmmoLedPin, OUTPUT);
  pinMode(hitLedPin, OUTPUT);
  pinMode(speakerPin, OUTPUT);
  lcd.begin(16, 2);
  // initialize serial communication for debug
  Serial.begin(9600);

  // Adafruit I2C 7-Segment Display backpack initialization
  matrix.begin(0x70);  // pass in the address
  matrix.setBrightness(4);

  // Write out the initial display
  matrix.writeDigitRaw(1, BM_G);
  matrix.writeDigitRaw(3, BM_O);
  matrix.writeDisplay();

  // Start the receiver listening
  irrecv.enableIRIn();

  // Startup tone generator
  for (int i = 6; i > 0; i--) {
    digitalWrite(hitLedPin, HIGH);
    playTone(900 * i, 50);
    digitalWrite(hitLedPin, LOW);
    delay(50);
  }

  Serial.println("Ready.");

  lcd.print("READY");
}

// Main loop
void loop() {

  // Check if there is a message waiting that can be decoded
  if (irrecv.decode(&results)) {
    Serial.println(results.value, DEC);

    // Check if it is a valid hit routine
    // This should be the firing IR code of who you are NOT
    // Player1 = 3978641416;
    // Player2 = 275977667;
    // TODO: Need to change this to accept a range so more than two 'teams' can be in play
    //       Probalby shouldn't be doing this gross decimal parsing too on the binary values.

    if (results.value == 3978641416) {
      Serial.println("HIT HIT HIT");
      digitalWrite(hitLedPin, HIGH);
      lcd.clear();
      lcd.print("HIT BY FALLEN ANGEL!");
      lcd.setCursor(1, 1);
      lcd.print (myHealth);
      // TODO: Less lame hit sound
      playTone(2500, 200);

      // Reduce their health and then check if they should be dead or not
      myHealth--;
      if (myHealth <= 0) {
        Serial.println("DEAD!");
        lcd.clear();
        lcd.print("DEAD!");
        deathCount++;
        matrix.blinkRate(1);
        matrix.writeDigitRaw(0, BM_D);
        matrix.writeDigitRaw(1, BM_E);
        matrix.writeDigitRaw(3, BM_A);
        matrix.writeDigitRaw(4, BM_D);
        matrix.writeDisplay();
        playTone(5000, 1000);
        delay(deadTime);
        matrix.blinkRate(0);
        matrix.print(deathCount, DEC);
        matrix.writeDigitRaw(0, BM_S);
        matrix.writeDigitRaw(1, BM_DASH);
        matrix.writeDisplay();
        delay(2000);
        matrix.writeDigitRaw(0, BM_CLEAR);
        matrix.writeDigitRaw(1, BM_G);
        matrix.writeDigitRaw(3, BM_O);
        matrix.writeDigitRaw(4, BM_CLEAR);
        matrix.writeDisplay();
        myHealth = maxHealth;
        ammoCount = maxAmmo;
        Serial.println("Ready again!");
        lcd.clear ();
        lcd.print("Ready again!");

      } else {
        Serial.print("Health: ");
        Serial.println(myHealth);



      }
      digitalWrite(hitLedPin, LOW);


    } else {
      Serial.println("IR signal received but not recognized");
      digitalWrite(hitLedPin, LOW);

    }

    irrecv.resume(); // Receive the next value
  }

  // read the pushbutton input pins
  triggerState = digitalRead(triggerPin);
  reloadState = digitalRead(reloadPin);

  // Check if the state has changed for the reload pin and then perform a reload if it's been pushed
  if (reloadState != lastReloadState) {
    if (reloadState == HIGH) {
      Serial.println("Reloading!");
      lcd.clear();
      lcd.print("Reloading!");
      delay (1000);
      lcd.clear();
      lcd.print("AMMO=30! FIRE!");
      delay (1000);
      lcd.clear();
      ammoCount = maxAmmo;
      matrix.blinkRate(0);
      matrix.print(ammoCount, DEC);
      matrix.writeDisplay();
      playTone(1000, 100);
      playTone(500, 100);

    }
  }

  // compare the triggerState to its previous state
  if (triggerState != lastTriggerState) {
    if (ammoCount > 0) {
      if (triggerState == HIGH) {
        ammoCount--;
        Serial.println("Trigger on");
        Serial.print("Ammo:  ");
        Serial.println(ammoCount);
        // clear the screen
        lcd.clear();
        lcd.setCursor(5, 1);
        lcd.print (ammoCount);
        lcd.print (" = AMMO");
        lcd.setCursor(2, 0);
        lcd.print ("BLACK HAWK");


        // TODO: Less lame firing sound
        playTone(600, 50);
        playTone(500, 50);
        playTone(400, 50);
        playTone(300, 50);

        // Fire the IR LED (3x to try and make sure it is received and to keep to Sony protocol)
        // This should be the firing IR code for who you are
        // Player1 = 0x00B
        // Player2 = 0xa90

        irsend.sendSony(0x00B, fireLedPin);
        irsend.sendSony(0x00B, fireLedPin);
        irsend.sendSony(0x00B, fireLedPin);

        irrecv.enableIRIn();

      } else {
        Serial.println("Trigger off");
      }

      matrix.print(ammoCount, DEC);
      matrix.writeDisplay();

    } else {
      // Out of ammo so flash the display accordingly and play a error noise if they try and fire
      matrix.writeDigitNum(0, 0);
      matrix.writeDigitNum(1, 0);
      matrix.writeDigitNum(3, 0);
      matrix.writeDigitNum(4, 0);
      matrix.blinkRate(2);
      matrix.writeDisplay();
      //digitalWrite(fireLedPin, LOW);
      digitalWrite(myAmmoLedPin, HIGH);
      Serial.println("Out of myAmmo!");
      lcd.print("Out of Ammo!");
      delay(1000);
      lcd.clear();
      playTone(5000, 100);
      //delay(40);

    }

    lastTriggerState = triggerState;
  }
}

// Play a sound on the piezo
void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}

The definition

void  sendSony(unsigned long data,  int nbits) ;

the usage

        irsend.sendSony(0x00B, fireLedPin);

Your blocking implementation of an interactive device would be useless for me, as I expect buttons to be disabled by logic, not because a sound is playing.
Do yourself a favor and redesign the whole thing.

thanks also is pin 3 the correct pin to use? my chip is the mega 328 p au 153. on my mega its pin 9 but what about the nano?

That depends on the timer you are using

// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
   //#define IR_USE_TIMER1    //tx = pin 9
   #define IR_USE_TIMER2     //tx = pin 3

ok I'll do some prototyping with the timer. so in my loop I should implement the signal of the fire LED pin as...

void sendSony(unsigned long data, int nbits) {
irsend.sendSony(0x00B, fireLedPin);
irsend.sendSony(0x00B, fireLedPin);
irsend.sendSony(0x00B, fireLedPin);
}

would this be acceptable?

You do not use the tx-pin for sending in code, its implicit.

You have to pass the command and the number of bits used in that command you want to send.