Laser Tag S07 – Sounds

Love Jesus, Love Code

Duration 40 mins

In the hardware tutorial “Laser Tag H07” you would have added sounds to the JQ6500 chip. In this tutorial, you will learn how to program the sounds into your gun.

In the next tutorial, we will be adding the shoot button and adding lights and sounds to that. You may find that you need to move from tutorial to tutorial as you seek to add more and more sound effects.

For this tutorial, we will get the gun to play the power up sound, which is file 8 on the sound chip.

1. Download and install the library files for JQ6500

By now you should have worked out that you can program most components by following the same steps:

  1. Find the model number
  2. Search for library files and install on Arduino software (or google for a tutorial)
  3. Search for tutorials on how to program, which may be found on the same page the library files are on. Sometimes there will be a reference for the library to help you program more features than just a tutorial will offer.

In these tutorials, I have often done the hard work for you by finding the model numbers and searching for the library files, and searching for how to program the chips, and working out what pins on the NodeMcu board are compatible. These are all steps you would need to do if you were to create your own project.

You can download the library files from https://github.com/sleemanj/JQ6500_Serial. The same page also contains instructions on how to install the library files into Arduino. I’m not going to repeat those instructions here, you should be able to find what you need on that website. It will work on Arduino 2.x and you may need to click on code –> download to download the library. The other link on the website was not working last time I checked.

2. Program the sound

First we need to declare what pins we want to use. At the start of the program we previously placed a comment “pin declarations”. We need to put the following code in that section:

int soundRX_Pin = 15; //D8 (only this pin is needed for sound)
int soundTX_Pin = 3; // RX - can put in 20 does not exist (this pin is not needed)

We also need to call the sound module library files and make sure the library can find the sound module. This code is placed in the program just before the setup loop:

//Code for JQ6500Q sound module
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <JQ6500_Serial.h>
SoftwareSerial mySerial(soundTX_Pin,soundRX_Pin);
JQ6500_Serial mp3(mySerial);

SoftwareSerial starts a serial stream called mySerial. The stream called mySerial will be used for the mp3 communication.

In the setup() loop we need to initialise the mp3 player. We will create a subroutine called setupSound();

Place setupSound(); in the setup() loop, just under the setupSound(); subroutine.

In the Arduino software, create a new tab called Sounds.ino

Paste the following code into the Sounds.ino tab

//setup MP3 player
void setupSounds(){
  mySerial.begin(9600);
  mp3.reset();
  debugInfo += "MP3 sounds are now operational <br>";
  Serial.println("MP3 sounds are now operational");
  mp3.setVolume(volume); //sets the volume of the speaker
  delay(500); // a pause is needed between setting the volume and playing
  mp3.playFileByIndexNumber(22); // ready to engage startup sound
}

The code above makes sure the mp3 player is operational.

Once this program is run, you will be able to use the sound module. You should also hear the sci-fi reload sound (sound 22) being played. The code is now ready to compile and upload onto your gun.

Note: Once the sound library is modified (as outlined below), there needs to be a gap between setting the volume and playing the sound. The delay(500); adds a sufficient delay.

We also want to keep track of the sounds, so place these in a comment in the Sounds.ino tab just under the code.

/*
We only use the rx port on the sound module

Files need to be in the folder 00
01 - milshot
02 - empty
03 - clip out
04 - end reload (clip in)
05 - near miss
06 - damage
07 - dead
08 - powerup
09 - beep
10 - buzz
11 - add health (Medic)
12 - add ammo
13 - add shild
14 - game over
15 - explosion
16 - shield hit
17 - flag
18 - flag score siren
19 - clone ok
20 - sensor fail
21 - sci shot
22 - sci end reload
23 - score ok
24 - repair
25 - silenced
26 - disarm
27 - low batt
28 - zombie1
29 - zombie2
30 - stunned

 */

3. Troubleshoot the sound

The sound library wants the sound to be fully played before the program does anything else. This means that when the sound is being played, you cannot shoot or be shot. This is called ‘blocking’ code and is not good for multi-tasking.

We will test this out by playing the shooting sound (sound 01) continuously in the main loop. In the game you want to just hear the first part of the sound before the next sound is played. That way the sound produced by the gun will match the required shot rate. Otherwise you could only shoot as quickly as the sound file, which is not quick enough.

  1. Make your main loop look like the following:
void loop() {
   AsyncElegantOTA.loop();
   mp3.playFileByIndexNumber(1);
}

When you run the program you will first get the “ready to engage” sound, then you will hear a gun shot. This will loop around every time the sound file completes OR after 1 second. We want the loop not to wait for the sound to complete. This will allow us to fire many shots in short succession.

To do this we will need to modify the library file.

  1. Locate the library file JQ6500_Serial.cpp. This will usually be located in documents/arduino/libraries/JQ6500_Serial/src. You can open the file with notepad++
  2. In the JQ6500_Serial.cpp file you will need to add if(!responseBuffer && !bufferLength) return; on line 263. This will allow you to play a MP3 sound without any pauses
 _serial->write((byte)MP3_CMD_END);

  if(!responseBuffer && !bufferLength) return; //this line was added

  unsigned int i = 0;


4. Towards the end of the file around line 305 add maxWaitTime =0; to prevent any pausing. The above code seems to have fixed the issue, this will prevent any further pausing.

// Waits until data becomes available, or a timeout occurs
int JQ6500_Serial::waitUntilAvailable(unsigned long maxWaitTime)
{
  maxWaitTime = 0; // override any waiting
  unsigned long startTime;
  int c = 0;
  startTime = millis();
  do {
    c = _serial->available();
    if (c) break;
  } while(millis() - startTime < maxWaitTime);
  
  return c;
}

5. Run the program and you should just get the start of the MP3 files.

When you run the program now, you should just get the start of the MP3 files. It will not sound that good, but will work better once we put shot delays in.

Take away the shot sound

We will remove the shot sound from the main loop() now that we have tested that the sound works correctly as in the video above.

 void loop() {
   AsyncElegantOTA.loop();
}

Note: When sending sounds to the sound module, it can only handle a sound request every 125-150ms. This will allow us to have shot rates at approximately 400-500 rounds per minute. If sound requests are put to the sound module faster than that, it seems to repeat the sound for a few times when you do not want it to play.

Devotion

In the lasertag game, we need our sounds to be clear so people can understand the meaning. It could be a gun shot, or to indicate the start or end of the game. If the sounds were randomised, it would make for a very confusing game.

The Bible talks about how important communication is, especially in the church.

1 Corinthians 14:6–11 (HCSB)
But now, brothers, if I come to you speaking in other languages, how will I benefit you unless I speak to you with a revelation or knowledge or prophecy or teaching? Even inanimate things that produce sounds—whether flute or harp —if they don’t make a distinction in the notes, how will what is played on the flute or harp be recognised? In fact, if the trumpet makes an unclear sound, who will prepare for battle? In the same way, unless you use your tongue for intelligible speech, how will what is spoken be known? For you will be speaking into the air. There are doubtless many different kinds of languages in the world, and all have meaning. Therefore, if I do not know the meaning of the language, I will be a foreigner to the speaker, and the speaker will be a foreigner to me. 

1 Corinthians 14:19 (HCSB)
yet in the church I would rather speak five words with my understanding, in order to teach others also, than 10,000 words in another language.

There have been many times and places in history when if you had gone to church, you would not have understood what was going on because it was said in a foreign language. Sometimes, even when it is spoken in a language we understand, it can be still difficult to understand the message or what is happening. Throughout history, Christians have strived to make the Christian message easy to understand. And for this reason, Christians have translated the Bible into almost 2000 languages. This is so people can understand the Bible in their own language. Like the examples above, if you cannot understand the message, how will it benefit you.

William Tyndale was convinced that the Bible was the ultimate authority in the church. In 1525 translated the Bible from Greek into English. But the church authorities at the time resisted this in England, so he fled to Germany to make the translation before copies of the English Bible were smuggled back into England in 1526. Tyndale then continued to work the Old Testament translation, but was captured and burned at the stake in Vilvoorde in 1536. By this time, 18,000 copies of his New Testament had been printed. He was keen for people to understand the Bible, so it would benefit them.

If you cannot understand the message, how will it benefit you?

In the laser tag game, there are lots of sound messages, whether it be a beep or a sound effect. If you cannot understand these messages, they will be of no benefit to you. Likewise, if you go to church and cannot understand the message, then it will also be no benefit to you.

If you want to know more about Christianity, make sure you talk to Christians you know or read the Bible so that you can understand its message and ensure it will be a benefit to you.

Leave a Reply