1 | #include "DFRobotDFPlayerMini.h"
|
2 | #include "SoftwareSerial.h"
|
3 |
|
4 | SoftwareSerial mySoftwareSerial(8, 11);
|
5 | SoftwareSerial HC12(4, 10);
|
6 |
|
7 | DFRobotDFPlayerMini myDFPlayer;
|
8 | void printDetail(uint8_t type, int value);
|
9 |
|
10 | // Digital IO's
|
11 | int triggerPin = 3; // Push button for primary fire. Low = pressed
|
12 | int trigger2Pin = 13; // Push button for secondary fire. Low = pressed
|
13 | int speakerPin = 16; // Direct output to piezo sounder/speaker
|
14 | 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.
|
15 | 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
|
16 | int ammoPin = 5; // An analogue output (PWM) level corresponds to remaining ammunition. Use PWM pin: 3,5,6,9,10 or 11.
|
17 | int hitPin = 7; // LED output pin used to indicate when the player has been hit.
|
18 | 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
|
19 | //int IRtransmit2Pin = 8; // Secondary fire mode IR transmitter pin: Use pins 2,4,7,8,12 or 13. DO NOT USE PWM pins!!
|
20 | int IRreceivePin = 12; // The pin that incoming IR signals are read from
|
21 | int IRreceive2Pin = 17; // Allows for checking external sensors are attached as well as distinguishing between sensor locations (eg spotting head shots)
|
22 |
|
23 | //Minimum gun requirements: trigger, receiver, IR led, hit LED.
|
24 |
|
25 | // Player and Game details
|
26 | int myTeamID = 1; // 1-7 (0 = system message)
|
27 | int myPlayerID = 1; // Player ID
|
28 | int myGameID = 1; // Interprited by configureGane subroutine; allows for quick change of game types.
|
29 | int myWeaponID = 0; // Deffined by gameType and configureGame subroutine.
|
30 | int myWeaponHP = 0; // Deffined by gameType and configureGame subroutine.
|
31 | int maxAmmo = 0; // Deffined by gameType and configureGame subroutine.
|
32 | int maxLife = 0; // Deffined by gameType and configureGame subroutine.
|
33 | int automatic = 0; // Deffined by gameType and configureGame subroutine. Automatic fire 0 = Semi Auto, 1 = Fully Auto.
|
34 | int automatic2 = 0; // Deffined by gameType and configureGame subroutine. Secondary fire auto?
|
35 |
|
36 | //Incoming signal Details
|
37 | int received[18]; // Received data: received[0] = which sensor, received[1] - [17] byte1 byte2 parity (Miles Tag structure)
|
38 | int check = 0; // Variable used in parity checking
|
39 |
|
40 | // Stats
|
41 | int ammo = 0; // Current ammunition
|
42 | int life = 0; // Current life
|
43 |
|
44 | // Code Variables
|
45 | int timeOut = 0; // Deffined in frequencyCalculations (IRpulse + 50)
|
46 | int FIRE = 0; // 0 = don't fire, 1 = Primary Fire, 2 = Secondary Fire
|
47 | int TR = 0; // Trigger Reading
|
48 | int LTR = 0; // Last Trigger Reading
|
49 | int T2R = 0; // Trigger 2 Reading (For secondary fire)
|
50 | int LT2R = 0; // Last Trigger 2 Reading (For secondary fire)
|
51 |
|
52 | // Signal Properties
|
53 | int IRpulse = 600; // Basic pulse duration of 600uS MilesTag standard 4*IRpulse for header bit, 2*IRpulse for 1, 1*IRpulse for 0.
|
54 | int IRfrequency = 36; // Frequency in kHz Standard values are: 38kHz, 40kHz. Choose dependant on your receiver characteristics
|
55 | int IRt = 0; // LED on time to give correct transmission frequency, calculated in setup.
|
56 | int IRpulses = 0; // Number of oscillations needed to make a full IRpulse, calculated in setup.
|
57 | int header = 4; // Header lenght in pulses. 4 = Miles tag standard
|
58 | int maxSPS = 10; // Maximum Shots Per Seconds. Not yet used.
|
59 | int TBS = 0; // Time between shots. Not yet used.
|
60 |
|
61 | // Transmission data
|
62 | int byte1[8]; // String for storing byte1 of the data which gets transmitted when the player fires.
|
63 | int byte2[8]; // String for storing byte1 of the data which gets transmitted when the player fires.
|
64 | int myParity = 0; // String for storing parity of the data which gets transmitted when the player fires.
|
65 |
|
66 | // Received data
|
67 | 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.
|
68 | int hitNo = 0; // Hit number
|
69 | // Byte1
|
70 | int player[10]; // Array must be as large as memory
|
71 | int team[10]; // Array must be as large as memory
|
72 | // Byte2
|
73 | int weapon[10]; // Array must be as large as memory
|
74 | int hp[10]; // Array must be as large as memory
|
75 | int parity[10]; // Array must be as large as memory
|
76 | int HitPlayer = 0;
|
77 |
|
78 |
|
79 | void setup() {
|
80 | // Serial coms set up to help with debugging.
|
81 | mySoftwareSerial.begin(9600);
|
82 | Serial.begin(9600);
|
83 | HC12.begin(9600);
|
84 | Serial.println("Startup...");
|
85 |
|
86 | Serial.println();
|
87 | Serial.println(F("DFRobot DFPlayer Mini Demo"));
|
88 | Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
|
89 |
|
90 | if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
|
91 | Serial.println(F("Unable to begin:"));
|
92 | Serial.println(F("1.Please recheck the connection!"));
|
93 | Serial.println(F("2.Please insert the SD card!"));
|
94 | while(true){
|
95 | delay(0); // Code to compatible with ESP8266 watch dog.
|
96 | }
|
97 | }
|
98 | Serial.println(F("DFPlayer Mini online."));
|
99 |
|
100 | myDFPlayer.volume(25); //Set volume value. From 0 to 30
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | // Pin declarations
|
107 | pinMode(triggerPin, INPUT);
|
108 | pinMode(trigger2Pin, INPUT);
|
109 | pinMode(speakerPin, OUTPUT);
|
110 | //pinMode(audioPin, OUTPUT);
|
111 | pinMode(lifePin, OUTPUT);
|
112 | pinMode(ammoPin, OUTPUT);
|
113 | pinMode(hitPin, OUTPUT);
|
114 | pinMode(IRtransmitPin, OUTPUT);
|
115 | //pinMode(IRtransmit2Pin, OUTPUT);
|
116 | pinMode(IRreceivePin, INPUT);
|
117 | pinMode(IRreceive2Pin, INPUT);
|
118 | pinMode(HitVibration, OUTPUT);
|
119 |
|
120 |
|
121 | frequencyCalculations(); // Calculates pulse lengths etc for desired frequency
|
122 | configureGame(); // Look up and configure game details
|
123 | tagCode(); // Based on game details etc works out the data that will be transmitted when a shot is fired
|
124 |
|
125 |
|
126 | digitalWrite(triggerPin, HIGH); // Not really needed if your circuit has the correct pull up resistors already but doesn't harm
|
127 | digitalWrite(trigger2Pin, HIGH); // Not really needed if your circuit has the correct pull up resistors already but doesn't harm
|
128 | myDFPlayer.play(3);
|
129 |
|
130 | for (int i = 1;i < 254;i++) { // Loop plays start up noise
|
131 | analogWrite(ammoPin, i);
|
132 | playTone((3000-9*i), 2);
|
133 |
|
134 | }
|
135 |
|
136 | // Next 4 lines initialise the display LEDs
|
137 | analogWrite(ammoPin, ((int) ammo));
|
138 | analogWrite(lifePin, ((int) life));
|
139 | lifeDisplay();
|
140 | ammoDisplay();
|
141 |
|
142 | Serial.println("Ready....");
|
143 | }
|