Hi
I tried to add a hit feedback function to an existing code for laser tag (infrared) gaming. I use HC-12 modules (SoftwareSerial HC12) to send back the received playerID when a player gets hit. The player making a hit gets the feedback by audio (mp3-module).
The code worked fine. But with my additional code, the UNO gets stuck while initialyzing the DFPlayer mini here:
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
The SD card is fine. When I upload the old code the DFPlayer and everything works fine.
Here is the first half of the code:
#include "DFRobotDFPlayerMini.h"
#include "SoftwareSerial.h"
SoftwareSerial mySoftwareSerial(8, 11);
SoftwareSerial HC12(4, 10);
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
// Digital IO's
int triggerPin = 3; // Push button for primary fire. Low = pressed
int trigger2Pin = 13; // Push button for secondary fire. Low = pressed
int speakerPin = 14; // Direct output to piezo sounder/speaker
int HitVibration = 9; // Audio Trigger. Can be used to set off sounds recorded in the kind of electronics you can get in greetings card that play a custom message.
int lifePin = 6; // An analogue output (PWM) level corresponds to remaining life. Use PWM pin: 3,5,6,9,10 or 11. Can be used to drive LED bar graphs. eg LM3914N
int ammoPin = 5; // An analogue output (PWM) level corresponds to remaining ammunition. Use PWM pin: 3,5,6,9,10 or 11.
int hitPin = 7; // LED output pin used to indicate when the player has been hit.
int IRtransmitPin = 2; // Primary fire mode IR transmitter pin: Use pins 2,4,7,8,12 or 13. DO NOT USE PWM pins!! More info: http://j44industries.blogspot.com/2009/09/arduino-frequency-generation.html#more
//int IRtransmit2Pin = 8; // Secondary fire mode IR transmitter pin: Use pins 2,4,7,8,12 or 13. DO NOT USE PWM pins!!
int IRreceivePin = 12; // The pin that incoming IR signals are read from
int IRreceive2Pin = 15; // Allows for checking external sensors are attached as well as distinguishing between sensor locations (eg spotting head shots)
//Minimum gun requirements: trigger, receiver, IR led, hit LED.
// Player and Game details
int myTeamID = 1; // 1-7 (0 = system message)
int myPlayerID = 1; // Player ID
int myGameID = 1; // Interprited by configureGane subroutine; allows for quick change of game types.
int myWeaponID = 0; // Deffined by gameType and configureGame subroutine.
int myWeaponHP = 0; // Deffined by gameType and configureGame subroutine.
int maxAmmo = 0; // Deffined by gameType and configureGame subroutine.
int maxLife = 0; // Deffined by gameType and configureGame subroutine.
int automatic = 0; // Deffined by gameType and configureGame subroutine. Automatic fire 0 = Semi Auto, 1 = Fully Auto.
int automatic2 = 0; // Deffined by gameType and configureGame subroutine. Secondary fire auto?
//Incoming signal Details
int received[18]; // Received data: received[0] = which sensor, received[1] - [17] byte1 byte2 parity (Miles Tag structure)
int check = 0; // Variable used in parity checking
// Stats
int ammo = 0; // Current ammunition
int life = 0; // Current life
// Code Variables
int timeOut = 0; // Deffined in frequencyCalculations (IRpulse + 50)
int FIRE = 0; // 0 = don't fire, 1 = Primary Fire, 2 = Secondary Fire
int TR = 0; // Trigger Reading
int LTR = 0; // Last Trigger Reading
int T2R = 0; // Trigger 2 Reading (For secondary fire)
int LT2R = 0; // Last Trigger 2 Reading (For secondary fire)
// Signal Properties
int IRpulse = 600; // Basic pulse duration of 600uS MilesTag standard 4*IRpulse for header bit, 2*IRpulse for 1, 1*IRpulse for 0.
int IRfrequency = 36; // Frequency in kHz Standard values are: 38kHz, 40kHz. Choose dependant on your receiver characteristics
int IRt = 0; // LED on time to give correct transmission frequency, calculated in setup.
int IRpulses = 0; // Number of oscillations needed to make a full IRpulse, calculated in setup.
int header = 4; // Header lenght in pulses. 4 = Miles tag standard
int maxSPS = 10; // Maximum Shots Per Seconds. Not yet used.
int TBS = 0; // Time between shots. Not yet used.
// Transmission data
int byte1[8]; // String for storing byte1 of the data which gets transmitted when the player fires.
int byte2[8]; // String for storing byte1 of the data which gets transmitted when the player fires.
int myParity = 0; // String for storing parity of the data which gets transmitted when the player fires.
// Received data
int memory = 10; // Number of signals to be recorded: Allows for the game data to be reviewed after the game, no provision for transmitting / accessing it yet though.
int hitNo = 0; // Hit number
// Byte1
int player[10]; // Array must be as large as memory
int team[10]; // Array must be as large as memory
// Byte2
int weapon[10]; // Array must be as large as memory
int hp[10]; // Array must be as large as memory
int parity[10]; // Array must be as large as memory
int HitPlayer = 0;
void setup() {
// Serial coms set up to help with debugging.
mySoftwareSerial.begin(9600);
Serial.begin(9600);
HC12.begin(9600);
Serial.println("Startup...");
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(25); //Set volume value. From 0 to 30
// Pin declarations
pinMode(triggerPin, INPUT);
pinMode(trigger2Pin, INPUT);
pinMode(speakerPin, OUTPUT);
//pinMode(audioPin, OUTPUT);
pinMode(lifePin, OUTPUT);
pinMode(ammoPin, OUTPUT);
pinMode(hitPin, OUTPUT);
pinMode(IRtransmitPin, OUTPUT);
//pinMode(IRtransmit2Pin, OUTPUT);
pinMode(IRreceivePin, INPUT);
pinMode(IRreceive2Pin, INPUT);
pinMode(HitVibration, OUTPUT);
frequencyCalculations(); // Calculates pulse lengths etc for desired frequency
configureGame(); // Look up and configure game details
tagCode(); // Based on game details etc works out the data that will be transmitted when a shot is fired
digitalWrite(triggerPin, HIGH); // Not really needed if your circuit has the correct pull up resistors already but doesn't harm
digitalWrite(trigger2Pin, HIGH); // Not really needed if your circuit has the correct pull up resistors already but doesn't harm
myDFPlayer.play(3);
for (int i = 1;i < 254;i++) { // Loop plays start up noise
analogWrite(ammoPin, i);
playTone((3000-9*i), 2);
}
// Next 4 lines initialise the display LEDs
analogWrite(ammoPin, ((int) ammo));
analogWrite(lifePin, ((int) life));
lifeDisplay();
ammoDisplay();
Serial.println("Ready....");
}
// Main loop most of the code is in the sub routines
void loop(){
receiveIR();
if(FIRE != 0){
shoot();
ammoDisplay();
}
triggers();
}