Hello, I am using the DFPlayerMini_Fast library.
I have a 3-stage switch as in the photo. (0-1-2)
I am communicating between the nRF24L01 module and 2 arduinos according to the status of this switch.
- On the arduino, a 3-stage switch is connected, when the switch is at the first stage, I play the first sound, when it is at the second stage, I play the second sound.
The second arduino takes the data from this switch and plays sound with DFPlayer accordingly.
but I have a problem: when the ignition is in the first position, player.play(1); I call the command and the music plays, but when I turn the key to the second position, the second song plays after waiting for the first song to finish. player.play(2);
How can I make this more organized? That is, when the switch is in the first position, the first song will play, when the key is in the second position, the first song will stop and the second song will play. I can't stop the first song.
I have one more question, if I take the key from the zero position to the second position (0-2) quickly, not gradually, or if I take it from the second position to the first position, I want to make a separate check that the same song will not play again. How can I achieve this?
Each song will play once if the key has not reached the zero position.
Thank you in advance for your help.
TX Code
//NRF Verici Kodu
/*
nRF24L01 Modül Bağlantıları:
1 - GND
2 - VCC 5V
3 - CE Arduino pin 9
4 - CSN Arduino pin 8
5 - SCK Arduino pin 13
6 - MOSI Arduino pin 11
7 - MISO Arduino pin 12
8 - Kullanmıyoruz
-
*/
/*-----( nRF24L01 Gerekli Kütüphaneleri Yüklüyoruz )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( nRF24L01 Değişkenlerimizi tanımlıyoruz )-----*/
#define CE_PIN 9
#define CSN_PIN 8
#define kontak1Buton 4
#define kontak2Buton 2
int kontakDurum = 0;
// nRF24L01 NOT: Sondaki "LL" "LongLong" tipi demek
const uint64_t pipe = 0xE8E8F0F0E1LL; // yayın adresi diyebiliriz iki tarafta da aynı olmalı ki haberleşebilsinler (transmit pipe deniyor)
/*-----( nRF24L01 kütüphanesi ile radio adında bir haberleşme nesnesi yaratıyoruz )-----*/
RF24 radio(CE_PIN, CSN_PIN); // radio yaratıldı
/*-----( nRF24L01 Girdileri tutacağımız diziyi tanımlayalım )-----*/
int verilerim[2]; //
int veriKontrol = 0;
void setup() {
Serial.begin(9600);
radio.begin(); //nRF24L01 iletişimi başlattık
radio.openWritingPipe(pipe); //nRF24L01 adrese bağlanıyoruz
pinMode(butonBig, INPUT);
pinMode(butonSmall, INPUT);
pinMode(kontak1Buton, INPUT);
pinMode(kontak2Buton, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(kontak1Buton) == HIGH) {
//Serial.println("KONTAK 1");
kontak1(1);
} else if (digitalRead(kontak2Buton) == HIGH) {
//Serial.println("KONTAK 222");
kontak2(2);
} else {
Serial.println("kontak KAPALI");
verilerim[0] = 0;
verilerim[1] = 0;
veriGonder();
}
//Serial.print("KONTAK LOOP DURUM = ");
//Serial.println(kontakDurum);
}
void veriGonder() {
radio.write( verilerim, sizeof(verilerim) ); //verileri gönderilmek üzere çıkışa yazdırıyoruz
}
void kontak1(int durum) {
kontakDurum = durum;
if (kontakDurum == durum) {
//Serial.print("KONTAK 1 DURUM = ");
//Serial.print(durum);
verilerim[0] = 1;
verilerim[1] = 0;
veriGonder();
kontakDurum = 0;
} else {
verilerim[0] = 0;
veriGonder();
}
}
void kontak2(int durum) {
kontakDurum = durum;
if (kontakDurum == durum) {
//Serial.print("KONTAK 2 DURUM = ");
//Serial.print(durum);
verilerim[0] = 0;
verilerim[1] = 1;
veriGonder();
kontakDurum = 0;
} else {
verilerim[1] = 0;
veriGonder();
}
}
RX Code
//NRF24L01 alıcı kodu
/*
nRF24L01 Modül Bağlantıları:
1 - GND
2 - VCC 5V
3 - CE Arduino pin 9
4 - CSN Arduino pin 8
5 - SCK Arduino pin 13
6 - MOSI Arduino pin 11
7 - MISO Arduino pin 12
8 - Kullanmıyoruz
-
*/
#define KEY1 0 // nRF24L01 diziden gelecek değişkenler için tanımladık (bu örnekte 2 veri alacağız)
#define KEY2 0
/*-----( nRF24L01 kütüphaneleri yükleyelim )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( DFPLAYER )-----*/
#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>
//nRF24L01 modülün pinleri
#define CE_PIN 9
#define CSN_PIN 8
const uint64_t pipe = 0xE8E8F0F0E1LL; // nRF24L01 adres
/*-----( nRF24L01 radio nesnesi yaratılıyor)-----*/
RF24 radio(CE_PIN, CSN_PIN); //
//nRF24L01 radiodan gelen 2 adet veriyi almak için bir dizi oluşturuyoruz
int verilerim[] = {KEY1, KEY2}; // keylerimiz
#define PIN_MP3_TX 5
#define PIN_MP3_RX 6
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX); // RX, TX df player
DFPlayerMini_Fast player;
/*-----( DFPLAYER )-----*/
void setup() {
Serial.begin(9600);
//radio ile iletişim kuruyoruz
Serial.println("Nrf24L01 Alıcı Başlatılıyor");
radio.begin();
radio.openReadingPipe(1, pipe);
radio.startListening();
softwareSerial.begin(9600);
if (player.begin(softwareSerial)) {
Serial.println("OK");
// Set volume to maximum (0 to 30).
player.volume(25);
// Play the first MP3 file on the SD card
delay(1000);
} else {
Serial.println("Connecting to DFPlayer Mini failed!");
}
}
void loop() {
if ( radio.available() ) { //eğer radyo ile temas kurabilmişse
// done değişkeni false yapıyoruz ve while içinde sonsuz döngü oluşturarak verileri toplayacağız
bool done = false;
while (!done)
{
// veriyi alıyoruz.
done = radio.read( verilerim, sizeof(verilerim) ); //ne kadar veri varsa al demek
//serial monitörden verileri görmek için;
Serial.print("KEY1 = ");
Serial.print(verilerim[0]);
Serial.print(" KEY2 = ");
Serial.print(verilerim[1]);
Serial.println();
*/
if (verilerim[0] == 1) {
if (!player.isPlaying()) {
player.play(1);
}
} else if(verilerim[1] == 1){
if (!player.isPlaying()) {
player.play(2);
}
}
}//while
}//ilk if
//eğer radyo bulunamazsa
else {
Serial.println("Verici Bulunamadı");
//verileri sıfırla
verilerim[0] = 0;
verilerim[1] = 0;
}
}