Laser game code (IR sensor)

Im having problems getting my infra red sensor code to work
can anyone help

circuit:

here is my code:

#include <EEPROM.h>
#include <IRremote.h>



int addr = 0;
int fire = 0;
int reset = 0;
int ammo = 30;
int hp = 0;
int pews = 0;
int reload = 0;







void setup() {
 pinMode(4, INPUT); //fire button
 pinMode(8, OUTPUT); //green LED
 pinMode(7, OUTPUT); //red LED
 pinMode(2, OUTPUT); //transmitter
 pinMode(3, INPUT); //receiver
 pinMode(5, INPUT); //reload button
 pinMode(6, INPUT); //reset button
 Serial.begin(9600);

 
}
if ()

void loop() {
 int val = analogRead(0) / 4;
 EEPROM.write(addr, val);
 addr = addr + 1;

 if (addr == EEPROM.length()) {//dit is voor EEPROM (dit is nodig)

   addr = 0;

 }
/************************************************************/
fire = digitalRead(4);
reload = digitalRead(5);


 

 if (reload == HIGH) {
   if (ammo <= 30)
   ammo = 30;
   digitalWrite(7,HIGH);
   Serial.println("reloaded");
   delay(100);
 }




if (fire == HIGH) {
 if (ammo > 0){
   pews++;
   ammo--;


   digitalWrite(7,HIGH);
   Serial.print("pew, ");
   Serial.print(pews);
   Serial.print(" ammo, ");
   Serial.print(ammo);
   Serial.println();
   delay(50);
 }
 else{
   digitalWrite(7, HIGH);
   Serial.println("reload");
 }
}
else {
 digitalWrite(7, LOW);
}





}//loop end

Less or equal to 30.
This does nothing ammo will always be any value from 30 to 0.

What does the code not do?

Just notice this , does the code compile?

it doesnt send the signal of the IR sender to the receiver

i had to delete that line (was there by accident) then yes

and then in else

Pin 7 also HIGH , what is connected to pin 7?

What Pin is the IR Transmitter connected to?

Edit: Pin2 is never used in your code.

void setup() {
 pinMode(4, INPUT); //fire button
 pinMode(8, OUTPUT); //green LED
 pinMode(7, OUTPUT); //red LED
 pinMode(2, OUTPUT); //transmitter
 pinMode(3, INPUT); //receiver
 pinMode(5, INPUT); //reload button
 pinMode(6, INPUT); //reset button
 Serial.begin(9600);

here you can see what does what

Yes I saw that after posting my previous post , that's why I edit it.
You only switch Pin7 several times in your code but never Pin2.

yes i realised that the code was incorrect this is the updated code

#include <EEPROM.h>
#include <IRremote.h>



int addr = 0;
int fire = 0;
int reset = 0;
int ammo = 30;
int hp = 0;
int pews = 0;
int reload = 0;

IRsend irsend;
IRrecv irrecv(3);

decode_results results;

bool shouldTurnLedOn = false;

const unsigned long expectedHex = 0xFEA857;

void setup() {
  pinMode(4, INPUT); //fire button
  pinMode(8, OUTPUT); //green LED
  pinMode(7, OUTPUT); //red LED
  pinMode(2, OUTPUT); //transmitter
  pinMode(3, INPUT); //receiver
  pinMode(5, INPUT); //reload button
  pinMode(6, INPUT); //reset button
  Serial.begin(9600);

  irrecv.enableIRIn();
}


void loop() {
  int val = analogRead(0) / 4;
  EEPROM.write(addr, val);
  addr = addr + 1;

  if (addr == EEPROM.length()) {//dit is voor EEPROM (dit is nodig)

    addr = 0;

  }
/************************************************************/
fire = digitalRead(4);
reload = digitalRead(5);

if (shouldTurnLedOn)
{

digitalWrite(8, HIGH);

}
else
{
  digitalWrite(8,LOW);
}
if (irrecv.decode(&results))
{

  Serial.println(results.value, HEX);

  if (results.value == expectedHex)
  {
    shouldTurnLedOn = !shouldTurnLedOn;
  }
  irrecv.resume();
}

  if (reload == HIGH) {
    if (ammo <= 30)
    ammo = 30;
    digitalWrite(7,HIGH);
    Serial.println("reloaded");
    delay(100);
  }




 if (fire == HIGH) {
  if (ammo > 0){
    pews++;
    ammo--;

    irsend.sendNEC(0xFEA857, 32);

    digitalWrite(7,HIGH);
    Serial.print("pew, ");
    Serial.print(pews);
    Serial.print(" ammo, ");
    Serial.print(ammo);
    Serial.println();
    delay(50);
  }
  else{
    digitalWrite(7, HIGH);
    Serial.println("reload");
  }
}
else {
  digitalWrite(7, LOW);
}
 




}//loop end

Yes, you can. But with better code, it would not be necessary!

const byte fire_button = 4;
const byte green_LED = 8;
const byte red_LED = 7;
...

void setup() {
  pinMode(fire_button, INPUT);
  pinMode(green_LED, OUTPUT);
  pinMode(red_LED, OUTPUT);
  ...
  fire = digitalRead(fire_button);
  reload = digitalRead(reload_button);
  

Get it?

yes but i like the other option better

why? By defining your pins at the top of your program, you can re-wire your board and change 1 value in 1 location and it will work. Your method would require changing those numbers everywhere throughout the code. This is often referred to as magic numbers and is considered poor coding.

Oh, what other option?

Less typing also im here for help with my project not for criticism

Criticism? I'm pointing out the value of using well named constants v. magic numbers. It is a well established practice. You are certainly free to write your code however you wish.

Posting your code to a public forum invites a lot of feedback. Whether or not you consider it helpful or criticism.
<ploink>

What's with the EEPROM stuff? I don't think it is doing what you think it is doing.

Can you please explain

  • what devices you are using (part numbers)
  • the purpose of those devices
  • why you decided to use EEPROM?
  • how a user is supposed to interact with the device
  • what, if any, other devices you considered before deciding on the IR idea

HI @pepvb !

The Wokwi online simulator produces this

**************************************************************************************************
Thank you for using the IRremote library!
It seems, that you are using an old version 2.0 code / example.
This version is no longer supported!

Upgrade instructions can be found here:
 https://github.com/Arduino-IRremote/Arduino-IRremote?tab=readme-ov-file#converting-your-2x-program-to-the-4x-version

Please use one of the new code examples from the library,
 available at "File > Examples > Examples from Custom Libraries / IRremote".
Start with the SimpleReceiver or SimpleSender example.
The examples are documented here:
 https://github.com/Arduino-IRremote/Arduino-IRremote?tab=readme-ov-file#examples-for-this-library

Or just downgrade your library to version 2.6.0.

Thanks
**************************************************************************************************

If you compile your code do you see any messages or did you downgrade to version 2.6.0 of the IRremote lib (or use another old 2.0 lib)?

Ok, so this is a pretty good start and thanks for posting your full code correctly. Let's make a few changes to start developing habits that will make this a little easier for you.

Before you consider the revisions, note that the IR stuff has been left out for now. One step at a time. I think you want to use an IR remote as a blaster to take the place of the fire button? Is that right? Also, i didn't see the reset button but let's leave that for now, too.

Final note: I didn't read any other code suggestions here before posting. My suggestions are standalone, probably not the best among the suggestions but without further ado:

// #include <EEPROM.h> get rid of this for now. One stage at a time in game building
// let's do something fun since you seem to be using Uno R4
#include "Arduino_LED_Matrix.h"  // Include the LED_Matrix library, more here: https://docs.arduino.cc/tutorials/uno-r4-wifi/led-matrix/#sequencedone
#include <IRremote.hpp>

ArduinoLEDMatrix matrix;  // Create an instance of the ArduinoLEDMatrix class

// const int for things like pin assignments that will never change
const int irTx = 2;  // naming conventions help with longer code, good habit to get into
const int irRx = 3;
const int fireButton = 4;
const int reloadButton = 5;
const int resetButton = 6;
const int rLed = 7;
const int gLed = 8;
// the declarations below are ok.
int addr = 0;
int fire = 0;
int reset = 0;
int ammo = 30;
int hp = 0;
int pews = 0;
int reload = 0;

// lots of white space lines don't help; I deleted some
void setup() {
  Serial.begin(115200);                    // 9600? Let's go faster, it's 2025
  Serial.println("IR blaster game v1.0");  // so you always know what's loaded on the board
  matrix.begin();                          // Initialize the LED matrix
  pinMode(irTx, OUTPUT);                   //transmitter
  pinMode(irRx, INPUT);                    //receiver
  pinMode(fireButton, INPUT_PULLUP);       //no need for external resistors with pushbuttons
  pinMode(reloadButton, INPUT_PULLUP);
  pinMode(resetButton, INPUT_PULLUP);
  pinMode(gLed, OUTPUT);  //green LED
  pinMode(rLed, OUTPUT);  //red LED
  // a for loop is a great way to repeat something, say 5 times or however much you like
  for (int i = 0; i < 5; i++) {
    digitalWrite(gLed, HIGH);  // let's toggle these to know they work
    digitalWrite(rLed, HIGH);
    delay(250);
    digitalWrite(gLed, LOW);
    digitalWrite(rLed, LOW);
    delay(250);
  }  // end of for loop that toggles LEDs

  digitalWrite(gLed, LOW);  // let's init these to off
  digitalWrite(rLed, LOW);
}
// if () << what was the idea here? It's between setup and loop, so...? Delete it

void loop() {
  // you need some sort of debouncing of buttons and you'll see why in Serial
  fire = digitalRead(fireButton);
  reload = digitalRead(reloadButton);
  // remember INPUT_PULLUP below? The catch is your logic must be reversed so keep that in mind
  if (reload == LOW) {  // was HIGH, changed to LOW because INPUT_PULLUP
    // digitalWrite(7, HIGH); -- which was this one again?
    digitalWrite(rLed, HIGH);  // Oh yeah, red - isn't that easier to follow? Let's get the LED lit right away
    matrix.clear();
    matrix.loadFrame(LEDMATRIX_EMOJI_HAPPY);
    ammo = 30;
    if (ammo >= 30)  // this will keep ammo always at 30, I think you mean > so magazine can't be more
      ammo = 30;
    Serial.println("reloaded");
    delay(100);
    digitalWrite(rLed, LOW);  // and turn it back off
  }

  if (fire == LOW) {
    digitalWrite(rLed, HIGH);
    matrix.loadFrame(LEDMATRIX_CHIP);
    if (ammo > 0) {
      pews++;
      ammo--;
    } else if (ammo <= 0) {
      matrix.loadFrame(LEDMATRIX_DANGER);
      ammo = 0;
    }
    Serial.print("pew, ");
    Serial.print(pews);
    Serial.print(" ammo, ");
    Serial.println(ammo);
    delay(50);
    digitalWrite(rLed, LOW);
    matrix.clear();
  }
}  //loop end

yes
and thx