Hi
I’ve encountered a really annoying bug in the Arduino IDE that makes my code useless… I’d really appreciate some help with my project and avoiding this bug.
The plan is to make a light for my aquarium with an NodeMCU and addressable RGBW strips. Below are my code and a link the Arduino forum, where I posted about the bug.
#include <BlynkSimpleStream.h>
#include <Adafruit_NeoPixel.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
char auth[] = "MyToken";
#define PIN 2
#define NUMPIXELS 15
#define NUMCOLORS 3
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
BlynkTimer timer;
int maxColorPosition;
int fadeTimer;
int fadeTime = 1000;
int fadeSteps = 100;
double intensity = 0;
double currentIntensity = 0;
int setColor[NUMCOLORS];
double setColorProcent[NUMCOLORS];
int color[NUMCOLORS];
double colorProcent[NUMCOLORS];
double currentColorProcent[NUMCOLORS] = {0, 0, 0};
double fadeStepProcent[NUMCOLORS];
double fadeIntensity;
//---------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Blynk.begin(Serial, auth);
strip.begin();
Blynk.syncAll();
}
void loop() {
Blynk.run();
timer.run();
}
//---------------------------------------------------------------------------------
BLYNK_WRITE(V0) {
setColor[0] = param.asInt();
return;
}
BLYNK_WRITE(V1) {
setColor[1] = param.asInt();
return;
}
BLYNK_WRITE(V2) {
setColor[2] = param.asInt();
return;
}
BLYNK_WRITE(V3) {
calcSetColorProcent();
calcFadeStep();
fadeTimer = timer.setTimer(10, setStripFade, 100);
return;
}
BLYNK_WRITE(V4) {
intensity = param.asDouble() / 255;
return;
}
BLYNK_WRITE(V9) {
for (int i = 0; i < NUMCOLORS; i++) {
setColorProcent[i] = 0;
}
setStripColor();
return;
}
BLYNK_WRITE(V30) {
Blynk.virtualWrite(V31, setColorProcent[0]);
return;
}
//---------------------------------------------------------------------------------
void calcFadeStep() {
for (int i = 0; i < NUMCOLORS; i++) {
if (currentColorProcent[i] == setColorProcent[i]) {
fadeStepProcent[i] = 0;
} else {
fadeStepProcent[i] = (setColorProcent[i] - currentColorProcent[i]) / fadeSteps;
}
}
return;
}
void calcMaxColorPosition() {
int tempMax = 0;
for (int i = 0; i < NUMCOLORS; i++) {
if (tempMax < setColor[i]) {
maxColorPosition = i;
tempMax = setColor[i];
}
}
return;
}
void calcSetColorProcent() {
calcMaxColorPosition();
for (int i = 0; i < NUMCOLORS; i++) {
setColorProcent[i] = (double)setColor[i]/setColor[maxColorPosition];
}
}
bool checkColorChange() {
for (int i = 0; i < NUMCOLORS; i++){
if (color[i] != setColor[i]) {
return true;
}
}
return false;
}
bool checkIntensityChange() {
if (currentIntensity != intensity) {
return true;
}
return false;
}
void setStripColor() {
for (int i = 0; i < NUMCOLORS; i++) {
color[i] = setColorProcent[i] * intensity * 255;
currentColorProcent[i] = (double)color[i] / 255;
}
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, color[0], color[1], color[2]);
}
strip.show();
return;
}
void setStripFade() {
double temp;
for (int i = 0; i < NUMCOLORS; i++) {
temp = setColorProcent[i] - currentColorProcent[i];
if (abs(temp) < abs(fadeStepProcent[i])) {
colorProcent[i] = setColorProcent[i];
} else {
colorProcent[i] = currentColorProcent[i] + fadeStepProcent[i];
}
color[i] = colorProcent[i] * intensity * 255;
currentColorProcent[i] = (double)color[i] / 255;
}
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, color[0], color[1], color[2]);
}
strip.show();
return;
}