Hello once again!
I do apologize as I feel as if I keep asking roughly the same questions over and over as this project progresses; however, I am in need of a bit of assistance. The concept of the project I am working on is a two-way communication that is controlled by a simple LCD/Button/Potentiometer setup I have wired using an nrf24 and two Arduino Mega. In theory, this is what I am looking for, using zone 4 as an example:
- The potentiometer controls four zones on ArduinoTX
- Each zone will be selected when button one is pressed on ArduinoTX
- When button one is pressed, ArduinoTX sends a message to ArduinoRX
- For confirmation, ArduinoRX sends a message back to ArduinoTX
5a. If ArduinoTX recieves this message, it starts a DFPlayer to play .mp3 files
5b. If ArduinoTX does not receive a message from ArduinoRX, a failure message is displayed on the LCD
5c. If ArduinoTX receives a message from ArduinoRX, but the DFPlayer does not start correctly, a different error message is displayed on the LCD - If the DFPlayer is started on the ArduinoTX setup, ArduinoTX sends a message to ArduinoRX
- If ArduinoRX receives confirmation that the DFPlayer started, an LED pattern will play on ArduinoRX
- Once ArduinoRX has finished the LED sequence, and ArduinoTX has finished playing the .mp3 file, ArduinoRX begins looking for a zone message again and ArduinoTX is ready to send the first start command.
- At ANY time, if button two is pressed, all operations on both ArduinoTX and ArduinoRX stop and default to what is depicted in step 8.
Currently, I am relatively successful in this process; however, it is a bit finicky and sometimes ArdunioTX messages are not successfully received on ArduinoRX and vice versa, causing the code to get stuck in unusual or dysfunctional states. I will admit, I am still a bit limited with my knowledge of how the NRF24 library works. At the moment, in order to send the messages between the two Arduinos, I disable TX and enable RX by resetting the writing/reading pipes, but I feel this is the cause of most of my problems. Is there an easier way to check for confirmations between the two Arduinos? I have included my current "working" versions of the TX and RX codes below. Please let me know if there is anything I am doing terribly wrong or if there is an easier way, and I do apologize for the abysmal formatting. Primarily, I am only focused on getting zone four to work, as I am sure the rest of the zones will follow a similar format. Any help is appreciated.
Thanks,
Mike C.
Transmit Code:
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(4, 5); // CE, CSN
int sng = 0;
int start;
const byte addresses[][6] = {"00001","00002"};
static const uint8_t PIN_MP3_TX = 13;
static const uint8_t PIN_MP3_RX = 12;
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
#include <LiquidCrystal.h>
LiquidCrystal lcd(6, 7, 8, 9, 10, 11); //(rs, enable, d4, d5, d6, d7)
DFRobotDFPlayerMini player;
int SONG;
int DIFF;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
softwareSerial.begin(9600);
radio.begin();
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
radio.setPALevel(RF24_PA_HIGH);
radio.stopListening();
}
void loop()
{
radio.write(&sng, sizeof(sng));
if (analogRead(A0) <= 255 && analogRead(A0) >= 0) //ZONE 4 START
{
if (analogRead(A2) >= 1000)
{
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
radio.setPALevel(RF24_PA_HIGH);
radio.stopListening();
delay(250);
sng = 4;
radio.write(&sng, sizeof(sng));
DIFF = 4-SONG;
SONG = 4;
Serial.println("//////////////////////");
Serial.print("Difference = ");
Serial.println(DIFF);
Serial.println("//////////////////////");
Serial.print("Song Number = ");
Serial.println(SONG);
Serial.println("//////////////////////");
lcd.clear();
lcd.print("Loading");
radio.write(&sng, sizeof(sng));
delay(50);
sng = 4;
radio.write(&sng, sizeof(sng));
delay(500);
radio.openReadingPipe(1, addresses[1]);
radio.openWritingPipe(addresses[0]);
radio.startListening();
delay(2500);
if (radio.available())
{
radio.stopListening();
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
Serial.println("SNG 8");
sng = 8;
delay(200);
radio.write(&sng, sizeof(sng));
lcd.clear();
lcd.print("Request");
lcd.setCursor(5, 1);
lcd.print("Accepted");
delay(700);
radio.closeReadingPipe(addresses);
radio.startListening();
if (player.begin(softwareSerial))
{
player.stop();
delay(500);
player.volume(20);
player.play(4);
radio.stopListening();
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
delay(200);
delay(50);
delay(1000);
lcd.clear();
lcd.print("Now Playing:");
lcd.setCursor(5, 1);
lcd.print("Song #4");
delay(2000);
}
else
{
lcd.clear();
lcd.print("Failed");
delay(2000);
}
}
else
{
lcd.clear();
lcd.print("Poor Connection.");
lcd.setCursor(5, 1);
lcd.print("Retry?");
delay(2000);
}
}
else
{
if (analogRead(A1) >= 1000)
{
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
radio.setPALevel(RF24_PA_HIGH);
radio.stopListening();
delay(500);
sng = 0;
radio.write(&sng,sizeof(sng));
player.stop();
lcd.clear();
lcd.print("Song Stopped");
delay(2000);
sng = 0;
radio.write(&sng,sizeof(sng));
}
else
{
radio.openReadingPipe(1, addresses[1]);
radio.openWritingPipe(addresses[0]);
radio.startListening();
radio.read(&start,sizeof(start));
if(start==70)
{
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
radio.setPALevel(RF24_PA_HIGH);
radio.stopListening();
delay(185);
sng = 0;
radio.write(&sng,sizeof(sng));
Serial.println("Finished");
radio.openWritingPipe(addresses[1]);
radio.closeReadingPipe(addresses);
radio.setPALevel(RF24_PA_HIGH);
radio.stopListening();
delay(2000);
sng = 0;
radio.write(&sng,sizeof(sng));
lcd.clear();
lcd.print("Song Finished");
delay(1000);
sng = 0;
radio.write(&sng,sizeof(sng));
delay(1000);
sng = 0;
radio.write(&sng,sizeof(sng));
delay(1000);
}
else
{
lcd.clear();
lcd.print("Current Program:");
lcd.setCursor(5, 1);
lcd.print("Song #4");
delay(200);
}
}
}
} //ZONE 4 END
if (analogRead(A0) <= 511 && analogRead(A0) >= 256) { //ZONE 3 START
if (analogRead(A2) >= 1000)
{
if(SONG==4){
DIFF = -1;
SONG = 3;
}
else{
DIFF = 3-SONG;
SONG = 3;
}
Serial.println("//////////////////////");
Serial.print("Difference = ");
Serial.println(DIFF);
Serial.println("//////////////////////");
Serial.print("Song Number = ");
Serial.println(SONG);
Serial.println("//////////////////////");
lcd.clear();
lcd.print("Loading");
delay(2500);
if (player.begin(softwareSerial)) {
player.stop();
delay(500);
player.volume(20);
player.play(3);
delay(1200);
lcd.clear();
lcd.print("Program Sent");
delay(2500);
}
else{
Serial.println("Player Not Initialized");
}
}
else {
if (analogRead(A1) >= 1000)
{
player.stop();
lcd.clear();
lcd.print("Song Stopped");
delay(2000);
}
else{
lcd.clear();
lcd.print("Current Program:");
lcd.setCursor(5, 1);
lcd.print("Song #3");
delay(200);
}
}
}
//ZONE 3 END
if (analogRead(A0) <= 768 && analogRead(A0) >= 512) { //ZONE 2 START
if (analogRead(A2) >= 1000)
{
if(SONG>=3){
if(SONG==4){
DIFF=-2;
SONG = 2;
}
else{
DIFF=-1;
SONG = 2;
}
}
else{
DIFF = 2-SONG;
SONG = 2;
}
Serial.println("//////////////////////");
Serial.print("Difference = ");
Serial.println(DIFF);
Serial.println("//////////////////////");
Serial.print("Song Number = ");
Serial.println(SONG);
Serial.println("//////////////////////");
lcd.clear();
lcd.print("Loading");
delay(2500);
if (player.begin(softwareSerial)) {
player.stop();
delay(500);
player.volume(20);
player.play(2);
delay(1200);
lcd.clear();
lcd.print("Program Sent");
delay(2500);
}
else{
Serial.println("Player Not Initialized");
}
}
else {
if (analogRead(A1) >= 1000)
{
player.stop();
lcd.clear();
lcd.print("Song Stopped");
delay(2000);
}
else{
lcd.clear();
lcd.print("Current Program:");
lcd.setCursor(5, 1);
lcd.print("Song #2");
delay(200);
}
}
} //ZONE 2 END
if (analogRead(A0) <= 1023 && analogRead(A0) >= 769) { //ZONE 1 START
if (analogRead(A2) >= 1000){
if(SONG>=3){
if(SONG==4){
DIFF=-3;
SONG=1;
}
else{
DIFF=-2;
SONG=1;
}
}
else{
if(SONG==2){
DIFF=-1;
SONG=1;
}
else{
if(SONG==1){
DIFF=0;
SONG=1;
}
else{
DIFF=1;
SONG=1;
}
}
}
Serial.println("//////////////////////");
Serial.print("Difference = ");
Serial.println(DIFF);
Serial.println("//////////////////////");
Serial.print("Song Number = ");
Serial.println(SONG);
Serial.println("//////////////////////");
lcd.clear();
lcd.print("Loading");
delay(2500);
if (player.begin(softwareSerial)) {
player.stop();
delay(500);
player.volume(20);
player.play(1);
delay(1200);
lcd.clear();
lcd.print("Program Sent");
delay(2500);
}
else{
Serial.println("Player Not Initialized");
}
}
else {
if (analogRead(A1) >= 1000)
{
player.stop();
lcd.clear();
lcd.print("Song Stopped");
delay(2000);
}
else{
lcd.clear();
lcd.print("Current Program:");
lcd.setCursor(5, 1);
lcd.print("Song #1");
delay(200);
}
}
} //ZONE 1 END
}
Recieve Code
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const int Channel1 = 30;
const int Channel2 = 31;
const int Channel3 = 32;
const int Channel4 = 33;
const int Channel5 = 34;
const int Channel6 = 35;
const int Channel7 = 36;
const int Channel8 = 37;
const int Channel9 = 40;
const int Channel10 = 41;
const int Channel11 = 42;
const int Channel12 = 43;
const int Channel13 = 44;
const int Channel14 = 45;
const int Channel15 = 46;
const int Channel16 = 47;
RF24 radio(4, 5); // CE, CSN
int sng;
int start = 0;
int Counter = 0;
int Cycle = 0;
const byte addresses[][6] = {"00001","00002"};
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, addresses[1]);
radio.openWritingPipe(addresses[0]);
radio.setPALevel(RF24_PA_HIGH);
radio.startListening();
pinMode(Channel1, OUTPUT);
pinMode(Channel2, OUTPUT);
pinMode(Channel3, OUTPUT);
pinMode(Channel4, OUTPUT);
pinMode(Channel5, OUTPUT);
pinMode(Channel6, OUTPUT);
pinMode(Channel7, OUTPUT);
pinMode(Channel8, OUTPUT);
pinMode(Channel9, OUTPUT);
pinMode(Channel10, OUTPUT);
pinMode(Channel11, OUTPUT);
pinMode(Channel12, OUTPUT);
pinMode(Channel13, OUTPUT);
pinMode(Channel14, OUTPUT);
pinMode(Channel15, OUTPUT);
pinMode(Channel16, OUTPUT);
}
void loop() {
int Cycle;
if (Counter % 2 == 0)
{
if (radio.available())
{
if(Cycle!=0)
{
Serial.println("Overflow");
}
else
{
radio.read(&sng, sizeof(sng));
Serial.println(sng);
if(sng==12)
{
Serial.println("Lights");
}
if(sng==4)
{
Counter ++;
delay (1000);
}
if(sng==8)
{
if(Cycle==0);
Cycle = 1;
Serial.println(Cycle);
radio.read(&sng, sizeof(sng));
delay(100);
digitalWrite(30, HIGH); digitalWrite(32, HIGH);digitalWrite(34, HIGH); digitalWrite(36, HIGH);
delay(500);
digitalWrite(30, LOW); digitalWrite(32, LOW);digitalWrite(34, LOW); digitalWrite(36, LOW);
delay(500);
digitalWrite(30, HIGH); digitalWrite(32, HIGH);digitalWrite(34, HIGH); digitalWrite(36, HIGH);
delay(500);
digitalWrite(30, LOW); digitalWrite(32, LOW);digitalWrite(34, LOW); digitalWrite(36, LOW);
delay(500);
digitalWrite(30, HIGH); digitalWrite(32, HIGH);digitalWrite(34, HIGH); digitalWrite(36, HIGH);
delay(500);
digitalWrite(30, LOW); digitalWrite(32, LOW);digitalWrite(34, LOW); digitalWrite(36, LOW);
delay(600);
Serial.println(Cycle);
Cycle = 2;
sng=0;
radio.stopListening();
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
start = 70;
delay(250);
radio.write(&start, sizeof(start));
radio.openReadingPipe(1, addresses[1]);
radio.openWritingPipe(addresses[0]);
radio.setPALevel(RF24_PA_HIGH);
radio.startListening();
sng=0;
Cycle = 3;
Serial.println(Cycle);
radio.read(&sng, sizeof(sng));
delay(100);
radio.read(&sng, sizeof(sng));
delay(100);
radio.read(&sng, sizeof(sng));
delay(100);
radio.read(&sng, sizeof(sng));
Cycle = 4;
Serial.println(Cycle);
delay(500);
exit;
}
}
}
}
if (Counter % 2 != 0)
{
if(sng==4)
{
radio.stopListening();
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
delay(100);
start = 4;
radio.write(&start, sizeof(start));
Serial.println(start);
delay(2000);
radio.openReadingPipe(1, addresses[1]);
radio.openWritingPipe(addresses[0]);
radio.startListening();
delay(2000);
if (radio.available())
{
Counter++;
}
}
}
if(sng==0)
{
Serial.println("Stop");
radio.startListening();
delay(75);
Cycle = 0;
}
}