Ich hoffe hier kann mir geholfen werden.
Ich möchte bei meinem RC Car eine Melodie-Hupe hinzufügen. Das Ganze will ich mit einem DFPlayer mini, einen Arduino Nano und NRF24L01 anzusteuern.
Beim einschalten des Empfängers läuft die eingestellte (1. mp3-Datei) normal ab (setup).
Beim drücken des Button 1 am Sender beginnt die 1. mp3 kurz anzuspielen. Das geht 3 mal hintereinander und dann spielt es die Datei normal bis zum Ende ab.
Meine Vermutung nach liegt es daran, das der Tastendruck zu lang ist und somit die mp3 mehrmals hintereinander gestartet wird. Deshalb habe ich das ganze versucht mit einer for-Schleife zu stoppen. Brachte aber keinen Erfolg.
Hier mal der Code.
/*
DIY Arduino based RC Transmitter Project
== Receiver Code ==
by Dejan Nedelkovski, www.HowToMechatronics.com
Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
RF24 radio(3, 2); // nRF24L01 (CE, CSN)
const byte address[6] = "00001";
SoftwareSerial mySoftwareSerial(8, 7); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;
// Variablen fuer Hupe
int Hupe;
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte j1PotX;
byte j1PotY;
byte j1Button;
byte j2PotX;
byte j2PotY;
byte j2Button;
byte pot1;
byte pot2;
byte tSwitch1;
byte tSwitch2;
byte button1;
byte button2;
byte button3;
byte button4;
};
Data_Package data; //Create a variable with the above structure
void setup() {
mySoftwareSerial.begin(9600);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.startListening(); // Set the module as receiver
resetData();
//Serial.begin(9600);
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(10); //Set volume value. From 0 to 30
myDFPlayer.play(1); //Play the first mp3
}
void loop() {
// Check whether there is data to be received
if (radio.available()) {
radio.read(&data, sizeof(Data_Package)); // Read the whole data and store it into the 'data' structure
lastReceiveTime = millis(); // At this moment we have received the data
}
// Check whether we keep receving data, or we have a connection between the two modules
currentTime = millis();
if ( currentTime - lastReceiveTime > 1000 ) { // If current time is more then 1 second since we have recived the last data, that means we have lost connection
resetData(); // If connection is lost, reset the data. It prevents unwanted behavior, for example if a drone has a throttle up and we lose connection, it can keep flying unless we reset the values
}
// Kontrolle Hupe
Hupe = data.button1;
if (Hupe == LOW) {
for (int zaehler = 1; zaehler == 1; zaehler = zaehler + 1) {
myDFPlayer.play(1);
}
}
// Print the data in the Serial Monitor
/* Serial.print("j1PotX: ");
Serial.print(data.j1PotX);
Serial.print("; j1PotY: ");
Serial.print(data.j1PotY);
Serial.print(" j1Button: ");
Serial.print(data.j1Button);
Serial.print("; j2PotX: ");
Serial.print(data.j2PotX);
Serial.print("; j2PotY: ");
Serial.print(data.j2PotY);
Serial.print(" j2Button: ");
Serial.print(data.j2Button);
Serial.print("; button1: ");
Serial.print(data.button1);
Serial.print("; button2: ");
Serial.print(data.button2);
Serial.print("; button3: ");
Serial.print(data.button3);
Serial.print("; button4: ");
Serial.print(data.button4);
Serial.print(" tSwitch1: ");
Serial.print(data.tSwitch1);
Serial.print(" tSwitch2: ");
Serial.print(data.tSwitch2);
Serial.print(" Pot1: ");
Serial.print(data.pot1);
Serial.print(" Pot2: ");
Serial.println(data.pot2);
*/
}
void resetData() {
// Reset the values when there is no radio connection - Set initial default values
data.j1PotX = 127;
data.j1PotY = 127;
data.j2PotX = 127;
data.j2PotY = 127;
data.j1Button = 1;
data.j2Button = 1;
data.pot1 = 1;
data.pot2 = 1;
data.tSwitch1 = 1;
data.tSwitch2 = 0;
data.button1 = 1;
data.button2 = 1;
data.button3 = 1;
data.button4 = 1;
}
type or paste code here