我正在尝试编写两个脚本,一个 Tx 和一个 Rx,它们是使用从 vixen lights 软件发送的串行数据来控制的。 接收脚本
#include <RF24.h>
#define DATA_PIN 6
#define NUM_LEDS 100
#define MAX_CHANNELS NUM_LEDS * 3
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()) {
byte incomingByte[MAX_CHANNELS];
radio.read(&incomingByte, sizeof(byte) * MAX_CHANNELS);
// Calculate the number of pixels based on the incomingByte array
unsigned int num_leds = sizeof(incomingByte) / 3;
// Ensure the number of pixels does not exceed the number allowed
if (num_leds > NUM_LEDS) {
return;
}
// Loop through each of the pixels and read the values for each color
for (int i = 0; i < num_leds; i++) {
int channel = i * 3;
leds[i].r = incomingByte[channel];
leds[i].g = incomingByte[channel + 1];
leds[i].b = incomingByte[channel + 2];
}
// Tell the FastLED Library it is time to update the strip of pixels
FastLED.show();
}
}
TX脚本
#include "Arduino.h"
#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);
}
}
使用这些脚本
我试过搜索互联网但到目前为止没有解决方案。我只能从通用串行控制我的灯条上的前 6 个像素,我在我的 Vixen 灯软件中使用了一个标头,它是 <100> 100 表示 LED 的数量,每个 LED 有 3 个通道用于 RGB 值。我难住了。任何提示,建议非常感谢。 谢谢。