Я сделал платформу для беспроводной анаматроники, используя arduino pro mini и выполняя беспроводную связь с модулями nrf24l01. Я также использую Neopixels для освещения. Проблема в том, что что-то из-за того, что программа замедляет циклы и делает затухающие светодиодные анимации действительно резкими. Может ли кто-нибудь помочь мне найти, что мешает моему коду, или это просто arduino, который я использую, который не подходит.
Я включаю код из arduino pro mini, управляющего марионеткой.
#define PIN 3
#define NUMPIXELS 6
#define UP 0
#define DOWN 1
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
//const byte address[6] = "00001";
const byte addresses[][6] = {"00001", "00002"};
int colorMode = 0;
unsigned long previousFadeMillis;
int brightness;
int fadeInterval = 1;
const byte pwmLED = 5;
byte fadeDirection = UP;
int fadeValue = 0;
byte fadeIncrement = 5;
// define directions for LED fade
#define UP 0
#define DOWN 1
// constants for min and max PWM
const int minPWM = 100;
const int maxPWM = 255;
typedef struct A_t {
int X;
int Y;
int R;
int G;
int B;
int Br;
};
A_t SendData;
typedef struct B_t {
float nedVoltage;
int connectCount;
};
B_t NedData;
void setup() {
Serial.begin(9600);
radio.begin();
//radio.openReadingPipe(0, address);
//radio.setPALevel(RF24_PA_MIN);
radio.openWritingPipe(addresses[0]); // 00001
radio.openReadingPipe(1, addresses[1]); // 00002
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
NedData.connectCount = 0;
strip.begin();
strip.setPixelColor(4, 0, 200, 0);
strip.setPixelColor(5, 0, 200, 0);
}
void loop() {
unsigned long currentMillis = millis();
switch (colorMode) {
case 0:
doTheFade(currentMillis);
break;
case 1:
doTheFade(currentMillis);
break;
case 2:
doTheFade(currentMillis);
break;
case 3:
doTheFade(currentMillis);
break;
}
while (radio.available()) {
radio.read( &SendData, sizeof(SendData) );
//char text[32] = "";
//radio.read(&text, sizeof(text));
}
int sensorVoltage = analogRead(A0);
float nedVoltage = sensorVoltage * (4.2 / 1023.0);
NedData.nedVoltage = nedVoltage;
NedData.connectCount = NedData.connectCount + 1;
radio.stopListening();
bool ok = radio.write( &NedData, sizeof(NedData) );
radio.startListening();
Serial.print("fadeValue = ");
Serial.println(fadeValue);
Serial.print("NedData.connectCount = ");
Serial.println(NedData.connectCount);
Serial.print("NedData.nedVoltage = ");
Serial.println(NedData.nedVoltage);
Serial.print("SendData.X = ");
Serial.println(SendData.X);
Serial.print("SendData.Y = ");
Serial.println(SendData.Y);
Serial.print("SendData.R = ");
Serial.println(SendData.R);
Serial.print("SendData.G = ");
Serial.println(SendData.G);
Serial.print("SendData.B = ");
Serial.println(SendData.B);
Serial.print("SendData.Br = ");
Serial.println(SendData.Br);
}
void doTheFade(unsigned long thisMillis) {
// is it time to update yet?
// if not, nothing happens
if (thisMillis - previousFadeMillis >= fadeInterval) {
// yup, it's time!
if (fadeDirection == UP) {
fadeValue = fadeValue + fadeIncrement;
if (fadeValue >= maxPWM) {
// At max, limit and change direction
fadeValue = maxPWM;
fadeDirection = DOWN;
}
} else {
//if we aren't going up, we're going down
fadeValue = fadeValue - fadeIncrement;
if (fadeValue <= minPWM) {
// At min, limit and change direction
fadeValue = minPWM;
fadeDirection = UP;
}
}
// Only need to update when it changes
strip.setPixelColor(4, 0, fadeValue, 0);
strip.setPixelColor(5, 0, fadeValue, 0);
strip.show();
previousFadeMillis = thisMillis;
}
}```