0

i am trying to write two scripts, A Tx and a Rx, These are controlled using serial data sent from vixen lights software. RX script

#include <FastLED.h>
#include <RF24.h>

#define DATA_PIN 6
#define NUM_LEDS 10
CRGB leds[NUM_LEDS];

RF24 radio(9, 10);

byte addresses[][6] = {"Vixen1"};

void setup() {
  Serial.begin(115200);
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
  
  radio.begin();
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(124);
  radio.openReadingPipe(0, addresses[0]);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    // Calculate the number of pixels based on the number of LEDs we are expecting
    unsigned int num_leds = 10; // Or however many LEDs you have

    byte incomingByte[num_leds * 3];
    radio.read(&incomingByte, sizeof(byte) * num_leds * 3);

    for (int i = 0; i < num_leds; i++) {
      int channel = i * 3;
      byte r = incomingByte[channel];
      byte g = incomingByte[channel + 1];
      byte b = incomingByte[channel + 2];
      leds[i] = CRGB(r, g, b);
    }

    // Tell the FastLED Library it is time to update the strip of pixels
    FastLED.show();
  }

  // Other tasks to perform during each iteration of the loop can be added here.
}

TX script

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

// How many channels Vixen will control
#define MAX_CHANNELS 300

// Stores the value of each channel
byte outgoingByte[MAX_CHANNELS];

// Uses SPI bus + two digital pins for chip enable (CE) and chip select (CSN)
RF24 radio(9, 10);

// An address used by the transmitter and receiver
// Changing this address would allow multiple Vixen server instances to control
// different sets of clients while still operating on the nRF channel.
byte addresses[][6] = {"Vixen1"};

void setup() {
  Serial.begin(115200);

  radio.begin();
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(124);
  radio.openWritingPipe(addresses[0]);
}

void loop() {

  // Keeps track of place in byte array and how many channels are left to be read
  unsigned int remaining;
  unsigned int channel;

  for (;;) {

    // Header character is used to keep the Arduino in sync with first channel as Vixen sequentially
    // writes out each byte for every channel.
    while (!Serial.available());
    if (Serial.read() != '>') {
      continue;
    }

    remaining = MAX_CHANNELS;
    channel = 0;

    do {
      while (!Serial.available());
      outgoingByte[channel++] = Serial.read();
    }
    while (--remaining);

    // Write out the byte array of channel values to the nRF
    radio.write(&outgoingByte, sizeof(byte) * MAX_CHANNELS);

  }
}

I have been using these scripts, I'm thinking now, Because the Nrf can only transmit 32 bytes.

I have tried searching the internet but no solution so far. I am only able to control the First 10 Pixels accurately on my strip I am using a Header in my Vixen lights software that is > I think it will be a scrap it and new approach type job I'm stumped. Any tips, Advice much appreciated. I think I am going to have to take the approach whereby I break it into packets and reassemble the Data received on the receiver side and then feed the LEDs, but I'm stuck :/ Thanks.

1 Answer 1

0

You can use multiple sending with frame indexing, by setting an index in unused byte 30 and 31 of radio packet. (I assume you sequentially received each led RGB data by Serial - You have to adapt this snippet to your code)

sender example :

int nbLed = 1000;
int ledIndex = 0;
int byteCount = 0;

void loop() {

  while (!Serial.available());

  int frameIndex = ledIndex / 10; //range offset increase each 10 leds

  //assuming each led require 3 bytes received by serial
  outgoingByte[ledIndex] = Serial.read();     //write led R color
  outgoingByte[ledIndex + 1] = Serial.read(); //write led G color
  outgoingByte[ledIndex + 2] = Serial.read(); //write led N color
  byteCount++;

  if (byteCount > 9) { //bytes 0-29 filled
    outgoingByte[frameIndex * 30] = frameIndex >> 0;       //convert frame index to byte and write in pos 30/31 + offset
    outgoingByte[frameIndex * 30 + 1] = frameIndex >> 8;
    byteCount = 0;
    radio.write(&outgoingByte, sizeof(byte) * 32); //send packet
  }

  ledIndex++; //next led
  if (ledIndex > nbLed) ledIndex = 0; //restart from first led
}

receiver :

void loop() {

  int ledNum;
  byte incomingByte[32];  //2 last bytes used to index frame
  radio.read(&incomingByte, sizeof(byte) * 32);
  int frameIndex = incomingByte[31] * 16 + incomingByte[30] ;

  for (int i = 0; i < 10; i++) {
    int ledNum = (frameIndex * 10) +  (i * 3);
    byte r = incomingByte[ledNum];
    byte g = incomingByte[ledNum + 1];
    byte b = incomingByte[ledNum + 2];
    leds[ledNum] = CRGB(r, g, b);
  }

  // Tell the FastLED Library it is time to update the strip of pixels
  FastLED.show();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.