Transmitting signal to other Neo pixle with button click

Hey Guys,

I come up with one more problem in my project. I worked on Neo pixel which is changing color as per button click.

Now, I am working on to make wireless communication with NRF24L01. I am trying to get a way to communicate between 2 NRF24L01 where master Neo pixel is Red than it is transmitting a signal to slave and other neo pixels turn into Red as well.

I am posting code for both transmitter and receiver. Can anyone help me out about what is wrong in it?

Transmitter Code



#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <nRF24L01.h> 
#include <RF24.h>

#define BUTTON_PIN   2    // Digital IO pin connected to the button.  This will be
                          // driven with a pull-up resistor so the switch should
                          // pull the pin to ground momentarily.  On a high -> low
                          // transition the button press logic will execute.

#define PIXEL_PIN    6    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 7

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int msg[1];
int showType = 0;
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;



void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

  Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);

}

void loop() {
  // Get current button state.
  bool newState = digitalRead(BUTTON_PIN);

  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      showType++;
      if (showType > 5)
        showType=0;
      startShow(showType);
}  
  }
if (digitalRead(BUTTON_PIN) == HIGH){
msg[0] = 111;
radio.write(msg, 1);
}
      
  // Set the last button state to the old state.
  oldState = newState;
}

void startShow(int i) {
  switch(i){
    case 1: colorWipe(strip.Color(0, 255, 0), 50);    // Green
            break;
    case 2: colorWipe(strip.Color(255, 0, 0), 50);  // Red
            break;
    case 3: colorWipe(strip.Color(0, 0, 255), 50);  // blue
            break;
    case 4: colorWipe(strip.Color(255, 0, 255), 50);  // Magneta
            break;
    case 5: colorWipe(strip.Color(0, 255, 255), 50);  // Aqua
            break;
  }
}

// Fill the dots one after the other with a color

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}
Receiver Code


#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>


#define PIXEL_PIN    6    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 2

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int msg[1];

void setup(void){
  
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
pinMode(PIXEL_PIN, OUTPUT);
}

void loop(void){

while (radio.available())
{
radio.read(msg[1], 1); // Read information from the NRF24L01

if (msg[0] == 111) // Indicates switch is pressed
{
for(int x = 0; x != PIXEL_COUNT ; x++)
{
 colorWipe(strip.Color(255, 0, 0), 50);  // Red

}
}
else
{
for(int x = 0; x != PIXEL_COUNT; x++) 
{
colorWipe(strip.Color(0, 255, 0), 50);  // Green

}
}
delay(10);
}
}

// Fill the dots one after the other with a color

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

Can anyone help me out about what is wrong in it?

The first thing you must do is tell us what is wrong. In detail, to the best of your ability. Does the transmitter transmit? Does the receiver receive? Just saying it doesn't work isn't really helpful for those of us that don't have the hardware right in front of us. We cannot see what you see unless you tell us.

If you have not proved out your NRF24L01 setup, use this tutorial:

https://forum.arduino.cc/index.php?topic=421081.0

I do note the following issues with your code:

1) Your receiver code is questionable in that it uses a while loop rather than an if test for the presence of a message in the receiver.

2) You call the read routine with the address of msg[1] and then act on msg[0] which is wrong.

3) You only send one message to the slave, 111 decimal. The only thing the receiver can do is a colorWipe with RED. The GREEN code will never execute.

As per you mentioned, Transmitter is transmitting signal and the receiver is receiving signal. The problem is it doesn't show me red light there. Moreover, When my master circuit is on green still it shows me a Red.

I am trying to get that when my master neo pixel changes a color than slave needs to change it as well.

What is the right way for transmitting a signal to a slave?