NRF24 Christmas Light Two Way Communication

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:

  1. The potentiometer controls four zones on ArduinoTX
  2. Each zone will be selected when button one is pressed on ArduinoTX
  3. When button one is pressed, ArduinoTX sends a message to ArduinoRX
  4. 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
  5. If the DFPlayer is started on the ArduinoTX setup, ArduinoTX sends a message to ArduinoRX
  6. If ArduinoRX receives confirmation that the DFPlayer started, an LED pattern will play on ArduinoRX
  7. 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.
  8. 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;
}
}

It appears that you are role swapping the function of the transceivers on the two sides to implement your own communications protocol. Another, probably less complicated way, may be to use the acknowledgement feature of the nrf24L01. Here each side would have a fixed role (TX or RX). The TX sends a message and the RX sends its acknowledgement packet back containing user defined status data.

1 Like

Thanks for the reply! Sorry for the late response, its been a busy week. I will give it a shot.

Alright, I have done a complete overhaul of the code and it seems that all of the acknowledgments work correctly. I have included the TX and RX codes below:

TX Code

#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(4, 5);  // CE, CSN
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;
bool playerStatus = false;
bool Start = false;
bool StartSong = false;
int ZoneCommand = 0;
int ZonePlaying = 0;
int ScreenReset = 0;
bool Acknowledge;
int Ack;
void setup() {
  pinMode(A0, INPUT);
  pinMode(34, INPUT);
  pinMode(35, INPUT);
  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();
  radio.enableAckPayload();
  lcd.clear();
  delay(250);
}


void loop() {
  Serial.println(ZoneCommand);
  DFRobotDFPlayerMini player;
  while (playerStatus == 1) {

    Playing();
  }

  {
    if (Start % 2 == 0) {
      int Zone = (analogRead(A0));
      int StopButton = digitalRead(35);
      int StartButton = digitalRead(34);
      switch (Zone) {
        case 0 ... 255:  // Zone 4 Detection

          switch (StopButton) {
            case 0:
              switch (StartButton) {
                case 0:
                  lcd.setCursor(0, 0);
                  lcd.print("Current Program:");
                  lcd.setCursor(5, 1);
                  lcd.print("Song #4");
                  ZoneCommand = 0;
                  radio.write(&ZoneCommand, sizeof(ZoneCommand));                  
                  break;
                case 1:
                  ScreenReset = 0;
                  ZoneCommand = 4;
                  Start++;
                  break;
              }
              break;
            case 1:
              ScreenReset = 0;
              ZoneCommand = 0;
              radio.write(&ZoneCommand, sizeof(ZoneCommand));
              player.stop();
              Stop();
              break;
          }
          break;




        case 256 ... 511:  // Zone 3 Detection
          switch (StopButton) {
            case 0:
              switch (StartButton) {
                case 0:
                  lcd.setCursor(0, 0);
                  lcd.print("Current Program:");
                  lcd.setCursor(5, 1);
                  lcd.print("Song #3");
                  ZoneCommand = 0;
                  radio.write(&ZoneCommand, sizeof(ZoneCommand));   
                  //(&ZoneCommand,sizeof(ZoneCommand));
                  break;
                case 1:
                  ScreenReset = 0;
                  ZoneCommand = 3;
                  Start++;
                  break;
              }
              break;
            case 1:
              ScreenReset = 0;
              ZoneCommand = 0;
              radio.write(&ZoneCommand, sizeof(ZoneCommand));
              player.stop();
              Stop();
              break;
          }
          break;

        case 512 ... 767:  // Zone 2 Detection
          switch (StopButton) {
            case 0:
              switch (StartButton) {
                case 0:
                  lcd.setCursor(0, 0);
                  lcd.print("Current Program:");
                  lcd.setCursor(5, 1);
                  lcd.print("Song #2");
                  ZoneCommand = 0;
                  radio.write(&ZoneCommand,sizeof(ZoneCommand));
                  break;
                case 1:
                  ScreenReset = 0;
                  ZoneCommand = 2;
                  Start++;
                  break;
              }
              break;
            case 1:
              ScreenReset = 0;
              ZoneCommand = 0;
              radio.write(&ZoneCommand, sizeof(ZoneCommand));
              player.stop();
              Stop();
              break;
          }

          break;

        case 768 ... 1023:  // Zone 1 Detection
          switch (StopButton) {
            case 0:
              switch (StartButton) {
                case 0:
                  lcd.setCursor(0, 0);
                  lcd.print("Current Program:");
                  lcd.setCursor(5, 1);
                  lcd.print("Song #1");
                  ZoneCommand = 0;
                  radio.write(&ZoneCommand,sizeof(ZoneCommand));
                  break;
                case 1:
                  ScreenReset = 0;
                  ZoneCommand = 1;
                  Start++;
                  break;
              }
              break;
            case 1:
              ScreenReset = 0;
              ZoneCommand = 0;
              radio.write(&ZoneCommand, sizeof(ZoneCommand));
              player.stop();
              Stop();
              break;
          }

          break;
      }
      return;
    } else {
      Starting();
    }
  }
}







void Stop() {
  if (ScreenReset % 2 == 0) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Stopping Program");
    ZoneCommand = 0;
    ZonePlaying = 0;
    radio.write(&ZoneCommand, sizeof(ZoneCommand));
    ScreenReset++;
    playerStatus == false;
    player.stop();
    delay(1000);
  } else {
    ZoneCommand = 0;
    radio.write(&ZoneCommand, sizeof(ZoneCommand));
    Start = false;
    Acknowledge = false;
    playerStatus == false;
    return;
  }
}







void Starting() {
  radio.read(&Acknowledge, sizeof(Acknowledge));
  int StopButton = digitalRead(35);
  int StartButton = digitalRead(34);
  if (ScreenReset % 2 == 0) {
    lcd.clear();
    delay(50);
    ScreenReset++;
  } else {
    switch (StopButton) {
      case 0:
        lcd.setCursor(0, 0);
        lcd.print("Transmitting:");
        lcd.setCursor(5, 1);
        lcd.print("Song #");
        lcd.print(ZoneCommand);
        radio.write(&ZoneCommand, sizeof(ZoneCommand));

        if (radio.available()) {
          Serial.println(Acknowledge);
          switch (Acknowledge) {
            case false:
              lcd.clear();
              break;

            case true:
              Serial.println("Acknowledged Zone:");
              Serial.println(ZoneCommand);
              lcd.clear();
              lcd.print("Acknowledged");
              Begin();
          }
        } else {
          Serial.println("Waiting");
        }
        //ZoneCommand = 0;
        //Start = false; // Exit
        break;

      case 1:
        Stop();
        break;
    }
  }
}

void Begin() {
  int StopButton = digitalRead(35);
  int StartButton = digitalRead(34);
  switch (StopButton) {
    case 0:
      Serial.println("Begin");
      delay(5000);
      Start = false;
      delay(250);
      startMusic();
      break;

    case 1:
      ZoneCommand = 0;
      radio.write(&ZoneCommand, sizeof(ZoneCommand));
      break;
  }
}

void startMusic() {
  int StopButton = digitalRead(35);
  Serial.println("Hold");
  if (player.begin(softwareSerial)) {
    player.stop();
    delay(500);
    player.volume(20);
    Serial.println("Music Started");
    player.play(ZoneCommand);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Now Playing");
    lcd.setCursor(5, 1);
    lcd.print("Song #");
    lcd.print(ZoneCommand);
    playerStatus = 1;
    delay(250);
  } else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Audio Playback");
    lcd.setCursor(0, 1);
    lcd.print("Not Initialized");
    delay(2500);
    lcd.clear();
  }
  return;
}







void Playing() {
  int StopButton = digitalRead(35);
  int StartButton = digitalRead(34);
  int State;

  if (ZonePlaying == 0) {
    switch (ZoneCommand) {
      case 1:
        ZoneCommand = 11;
        ZonePlaying = 1;
        break;

      case 2:
        ZoneCommand = 12;
        ZonePlaying = 1;
        break;

      case 3:
        ZoneCommand = 13;
        ZonePlaying = 1;
        break;

      case 4:
        ZoneCommand = 14;
        ZonePlaying = 1;
        break;
    }
  } else {
    switch (StopButton) {
      case 0:
        State = player.readState();
        switch (State) {
          case 1:
            switch (ZoneCommand) {
              case 11:
                radio.write(&ZoneCommand, sizeof(ZoneCommand));
                Serial.println(ZoneCommand);
                break;

              case 12:
                radio.write(&ZoneCommand, sizeof(ZoneCommand));
                Serial.println(ZoneCommand);
                break;

              case 13:
                radio.write(&ZoneCommand, sizeof(ZoneCommand));
                Serial.println(ZoneCommand);
                return;
                break;

              case 14:
                radio.write(&ZoneCommand, sizeof(ZoneCommand));
                Serial.println(ZoneCommand);
                break;
            }
            break;

          case -1:
            playerStatus = 0;
            player.stop();
            Stop();
            return;
        }
        break;

      case 1:
        playerStatus = 0;
        ZoneCommand = 0;
        radio.write(&ZoneCommand, sizeof(ZoneCommand));
        player.stop();
        Stop();
    }
  }

  return;
}

RX Code

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
int SongDelay = 0;
bool Halt = false;
int bpmDelay = 0;
int ZonePlaying;
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 ZoneCommand;
unsigned long currentMillis = 0;
bool Acknowledge = 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();
  radio.enableAckPayload();
  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() {

  if (radio.available()) {
    radio.read(&ZoneCommand, sizeof(ZoneCommand));
    switch (ZoneCommand) {
      case 0:
        Acknowledge = false;
        Serial.println(ZoneCommand);
        Stop();
        break;

      case 1:
        Serial.println(ZoneCommand);
        Acknowledge = true;
        radio.writeAckPayload(00001, &Acknowledge, sizeof(Acknowledge));
        Halt = false;
        break;

      case 2:
        Serial.println(ZoneCommand);
        Acknowledge = true;
        radio.writeAckPayload(00001, &Acknowledge, sizeof(Acknowledge));
        Halt = false;
        break;


      case 3:
        Serial.println(ZoneCommand);
        Acknowledge = true;
        radio.writeAckPayload(00001, &Acknowledge, sizeof(Acknowledge));
        Halt = false;
        break;


      case 4:
        Serial.println(ZoneCommand);
        Acknowledge = true;
        radio.writeAckPayload(00001, &Acknowledge, sizeof(Acknowledge));
        Halt = false;
        break;


      case 11:
        bpmDelay = 1621.6;
        Serial.println(ZoneCommand);
        
      break;


      case 12:
        Serial.println(ZoneCommand);
        break;


      case 13:
        Serial.println(ZoneCommand);
        break;

      case 14:
        Serial.println(ZoneCommand);
        break;
    }
  } else {
    //Serial.println("Waiting");
  }
}




void Stop() {
  Halt == true;
  bpmDelay = 0;
  digitalWrite(Channel1, LOW);
  digitalWrite(Channel2, LOW);
  digitalWrite(Channel3, LOW);
  digitalWrite(Channel4, LOW);
  digitalWrite(Channel5, LOW);
  digitalWrite(Channel6, LOW);
  digitalWrite(Channel7, LOW);
  digitalWrite(Channel8, LOW);
  digitalWrite(Channel9, LOW);
  digitalWrite(Channel10, LOW);
  digitalWrite(Channel11, LOW);
  digitalWrite(Channel12, LOW);
  digitalWrite(Channel13, LOW);
  digitalWrite(Channel14, LOW);
  digitalWrite(Channel15, LOW);
  digitalWrite(Channel16, LOW);
  return;
}

Right now I am focussing on getting zone 1 (or 11) to work correctly. The way this currently functions is, if the potentiometer on TX is set to zone 1 and button one is pressed, the TX box sends ZoneCommand = 1. When the RX box receives this, it sends back an acknowledgment that ZoneCommand 1 was received. On the TX box, if the acknowledgment is received, a DF player is started then a ZoneCommand = 11 is sent to the RX box. All of this so far works correctly, and if button 2 on the TX box is pressed, ZoneCommand = 0 is accepted on both the TX and RX box, stopping everything and restarting from the beginning. The only issue I am having currently is, I am looking for the TX box to send ZoneCommand 11 while the DF player is running, which it currently does, and the RX box to execute a loop while ZoneCommand is equal to 11; however, this is an example of what I am looking to put in the ZoneCommand 11 loop:

digitalWrite(Channel1, HIGH);
delay(500);
digitalWrite(Channel2, HIGH);
digitalWrite(Channel1, LOW;
delay(500);
digitalWrite(Channel3, HIGH);
digitalWrite(Channel2, LOW;
delay(500);

...etc, and this is not a set function to increment lights, there will be random patterns for a Christmas light display.

The problem I am having with this is, if I use delay, when I send a ZoneCommand 0 from the TX box, the switch case on the RX box will not update from ZoneCommand 11 to Zone command 0 until the entire ZoneCommand 11 loop is completed. I have tried a minor implementation of millis() with no luck. Is there any way around this that is recommended? Would it be a bad idea to have ZoneCommand 11 write a digital pin high, then have a second Arduino run the loop as long as its corresponding digital input is high, or would I get roughly the same result? Please let me know when you get the chance.

Happy Holidays!!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.