Forum: Mikrocontroller und Digitale Elektronik Probleme mit HC-12 Modulen


von Laurin (wildatheart)


Lesenswert?

Hallo

Ich habe kürzlich einen Code für ein Laser Tag Game (Infrarot) versucht 
so zu erweitern, dass ein Treffer dem Spieler per Funk (HC-12 module) 
mitgeteilt wird.

Dazu habe ich die HC-12 Module an Pin 4 und 10 (Rx, Tx) gehängt und den 
Code entsprechend ergänzt. Doch die Feedback-Funktion funktioniert 
nicht. Ich habe mit dem Oszi die Sendesignale an den HC-12 geprüft - OK. 
Die Module senden auch (mit SDR geprüft), doch ich empfange nichts, auch 
wenn ich das empfangende Modul über den Serial Monitor laufen lasse.

Nachfolgend der Code bis zum main loop:
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
}


Und hier noch die Schlaufe für das empfangene Signal vom HC-12, die nach 
positivem Vergleich eine akkustische Rückmeldung gibt.


1
void shoot() {
2
  if(FIRE == 1){ // Has the trigger been pressed?
3
    Serial.println("FIRE 1");
4
    sendPulse(IRtransmitPin, 4); // Transmit Header pulse, send pulse subroutine deals with the details
5
    delayMicroseconds(IRpulse);
6
 
7
    for(int i = 0; i < 8; i++) { // Transmit Byte1
8
      if(byte1[i] == 1){
9
        sendPulse(IRtransmitPin, 1);
10
        //Serial.print("1 ");
11
      }
12
      //else{Serial.print("0 ");}
13
      sendPulse(IRtransmitPin, 1);
14
      delayMicroseconds(IRpulse);
15
    }
16
17
    for(int i = 0; i < 8; i++) { // Transmit Byte2
18
      if(byte2[i] == 1){
19
        sendPulse(IRtransmitPin, 1);
20
       // Serial.print("1 ");
21
      }
22
      //else{Serial.print("0 ");}
23
      sendPulse(IRtransmitPin, 1);
24
      delayMicroseconds(IRpulse);
25
    }
26
    
27
    if(myParity == 1){ // Parity
28
      sendPulse(IRtransmitPin, 1);
29
    }
30
    sendPulse(IRtransmitPin, 1);
31
    delayMicroseconds(IRpulse);
32
    Serial.println("");
33
    Serial.println("DONE 1");
34
35
    myDFPlayer.play(1);
36
  }
37
38
39
  if(FIRE == 2){ // Where a secondary fire mode would be added
40
    Serial.println("FIRE 2");
41
    sendPulse(IRtransmitPin, 4); // Header
42
    Serial.println("DONE 2");
43
  }
44
FIRE = 0;
45
ammo = ammo - 1;
46
}
47
48
void HitFeedback () {
49
  while (HC12.available()) {
50
    HitPlayer = (HC12.read());
51
    if (HitPlayer = myPlayerID) {
52
    myDFPlayer.play(4);
53
    }

: Verschoben durch Moderator
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.