Hi,
I currently making project - Custom Pc lightning. U using Arduino Leonardo + USB Serial Stream.
Here’s my Arduino code:
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
//BLYNK
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(4, 5); // RX, TX
#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleStream.h>
char auth[] = "*****";
#define STRIP1LEN 19
#define STRIP2LEN 19
#define STRIP3LEN 11
#define STRIP1 2
#define STRIP2 9
#define STRIP3 8
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(STRIP1LEN, STRIP1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(STRIP2LEN, STRIP2, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip3 = Adafruit_NeoPixel(STRIP3LEN, STRIP3, NEO_GRB + NEO_KHZ800);
int brightness = 250;
String text = "Hello world!!!";
int brightnessLVL = 13;
int lightMode = 1;
String freeval = "";
int spee = 25;
int r = 24 ;
int g = 107;
int b = 0;
int r2 = 255;
int g2 = 255;
int b2 = 255;
//================= LED COLORS ======================
uint32_t RED;
uint32_t WHITE;
uint32_t BLUE;
uint32_t GREEN;
void setup() {
Serial.begin(9600);
//================= LED STRIPS ======================
strip1.begin();
strip1.show(); // Initialize all pixels to 'off'
strip2.begin();
strip2.show(); // Initialize all pixels to 'off'
strip3.begin();
strip3.show(); // Initialize all pixels to 'off'
strip1.setBrightness(brightness);
strip2.setBrightness(brightness);
strip3.setBrightness(brightness);
RED = strip1.Color(255, 0, 0);
WHITE = strip1.Color(255, 255, 255);
BLUE = strip1.Color(0, 0, 255);
GREEN = strip1.Color(0, 255, 0);
//================= BLYNK ======================
DebugSerial.begin(9600);
Blynk.begin(auth, Serial);
setbrightnessLVL(250);
setLightMode();
}
//================= MAIN LOOP ======================
void loop() {
Blynk.run();
setLightMode();
}
BLYNK_CONNECTED() {
Blynk.syncAll();
}
BLYNK_WRITE(V1) {
lightMode = param.asInt();
}
BLYNK_WRITE(V2) {
// prevent changing parameters when doing 'long' loops
if ( lightMode != 2 || lightMode != 3 || lightMode != 4 ) {
r = param[0].asInt();
g = param[1].asInt();
b = param[2].asInt();
}
}
BLYNK_WRITE(V3) {
// prevent changing parameters when doing 'long' loops
if ( lightMode != 2 || lightMode != 3 || lightMode != 4 ) {
r2 = param[0].asInt();
g2 = param[1].asInt();
b2 = param[2].asInt();
}
}
BLYNK_WRITE(V4) {
// prevent changing parameters when doing 'long' loops
if ( lightMode != 2 || lightMode != 3 || lightMode != 4 ) {
int val = param.asInt();
setbrightnessLVL(val);
}
}
BLYNK_WRITE(V5) {
// prevent changing parameters when doing 'long' loops
if ( lightMode != 2 || lightMode != 3 || lightMode != 4 ) {
spee = param.asInt();
}
}
void setLightMode(){
switch (lightMode) {
case 1:
stripsOff();
break;
case 2:
rainbowCycle(spee/5);
break;
case 3:
rainbowSingleColor(spee);
break;
case 4:
stripOff(strip3);
singlePixel( spee );
break;
case 5:
singleColor(r, g , b);
break;
case 6:
twoColors(strip1.Color(r, g, b), strip1.Color(r2, g2, b2));
break;
default:
stripsOff();
break;
}
}
void setbrightnessLVL(int bright){
strip1.setBrightness(bright);
strip2.setBrightness(bright);
strip3.setBrightness(bright);
}
//================= MODES ======================
void rainbowCircle(uint8_t wait) {
uint16_t i, j;
for(j = 0; j < 256*2; j++) {
for(i=0; i< strip1.numPixels(); i++) {
strip1.setPixelColor(i, Wheel(((i * 256 / strip1.numPixels()) + j) & 255));
}
strip1.show();
}
}
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for (j=0; j<256*1; j++) {
for(i=0; i < strip1.numPixels(); i++) {
uint32_t col = Wheel( ( (i * 256 / strip1.numPixels() ) + j ) & 255);
strip1.setPixelColor(i, col);
strip2.setPixelColor(i, col);
strip3.setPixelColor(i, col);
}
uDelay(wait);
strip1.show();
strip2.show();
strip3.show();
}
}
void rainbowSingleColor(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*1; j++) {
for(i=0; i<strip1.numPixels(); i++) {
strip1.setPixelColor(i, Wheel((i+j) & 255));
strip2.setPixelColor(i, Wheel((i+j) & 255));
strip3.setPixelColor(i, Wheel((i+j) & 255));
}
uDelay(wait);
strip1.show();
strip2.show();
strip3.show();
}
}
void singlePixel(uint8_t wait) {
strip2.show();
for(uint16_t i=0; i <= strip2.numPixels(); i++) {
strip2.setPixelColor(i, strip1.Color(r, g, b));
strip2.setPixelColor(i-1, strip1.Color( 0, 0, 0));
strip2.show();
delay(wait);
}
strip2.show();
strip1.show();
for(uint16_t i = strip1.numPixels(); i != 0 ; i--) {
strip1.setPixelColor(i, strip1.Color( 0, 0, 0));
strip1.setPixelColor(i-1, strip1.Color(r, g, b));
strip1.show();
delay(wait);
}
strip1.setPixelColor(0, strip1.Color( 0, 0, 0));
strip1.show();
}
void singleColor(int r, int g, int b) {
for (int i = 0; i < strip1.numPixels(); i++) {
strip1.setPixelColor( i, strip1.Color(r, g, b) );
strip2.setPixelColor( i, strip1.Color(r, g, b) );
strip3.setPixelColor( i, strip1.Color(r, g, b) );
}
strip1.show();
strip2.show();
strip3.show();
delay(60);
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (int i=0; i < strip1.numPixels(); i=i+3) {
strip1.setPixelColor(i+q, c); //turn every third pixel on
}
strip1.show();
for (int i=0; i < strip2.numPixels(); i=i+3) {
strip2.setPixelColor(i+q, c); //turn every third pixel on
}
strip2.show();
uDelay(wait);
for (int i=0; i < strip1.numPixels(); i=i+3) {
strip1.setPixelColor(i+q, 0); //turn every third pixel off
}
for (int i=0; i < strip2.numPixels(); i=i+3) {
strip2.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
void colorSwitch(uint32_t Color1, uint32_t Color2, uint8_t wait) {
int i,j;
for(j =0; j < 2 ; j++) {
for (i = 0; i < strip1.numPixels(); i++) {
if ( (i + j) % 2 == 0) {
strip1.setPixelColor(i, Color1);
strip2.setPixelColor(i, Color1);
strip3.setPixelColor(i, Color1);
}
else {
strip1.setPixelColor(i, Color2);
strip2.setPixelColor(i, Color2);
strip3.setPixelColor(i, Color2);
}
strip1.show();
strip2.show();
strip3.show();
}
delay(wait * 6); //skalar
}
}
void stripOff(Adafruit_NeoPixel strip) {
for(int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip1.Color( 0, 0, 0));
uDelay(1);
}
strip.show();
}
void stripsOff() {
uint16_t i, j;
for(i=0; i < strip1.numPixels(); i++) {
strip1.setPixelColor(i, strip1.Color( 0, 0, 0));
strip2.setPixelColor(i, strip1.Color( 0, 0, 0));
strip3.setPixelColor(i, strip1.Color( 0, 0, 0));
}
uDelay(20);
strip1.show();
strip2.show();
strip3.show();
}
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(Adafruit_NeoPixel strip, uint8_t wait) {
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
}
strip.show();
delay(wait);
}
}
}
void twoColors(uint32_t Color1, uint32_t Color2) {
int TotalSteps = strip1.numPixels();
for (int Index = 0; Index < TotalSteps; Index++) {
uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(Color2) * Index)) / TotalSteps;
uint8_t green = ((Green(Color1) * (TotalSteps - Index)) + (Green(Color2) * Index)) / TotalSteps;
uint8_t blue = ((Blue(Color1) * (TotalSteps - Index)) + (Blue(Color2) * Index)) / TotalSteps;
strip1.setPixelColor(Index, strip1.Color(red, green, blue));
strip2.setPixelColor(Index, strip1.Color(red, green, blue));
strip3.setPixelColor(Index, strip1.Color(red, green, blue));
}
strip1.show();
strip2.show();
strip3.show();
delay(20);
}
//================= OTHER ======================
void uDelay(int waitTime) {
unsigned long startTime = micros();
unsigned long delayTime = waitTime * 1000;
unsigned long count = 0;
while ( micros() - startTime < delayTime) {
count++;
}
}
uint8_t Red(uint32_t color) {
return (color >> 16) & 0xFF;
}
uint8_t Green(uint32_t color){
return (color >> 8) & 0xFF;
}
uint8_t Blue(uint32_t color){
return color & 0xFF;
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip1.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if(WheelPos < 170) {
WheelPos -= 85;
return strip1.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return strip1.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
The main problem is receiving data from app when Arduino is running functions like: rainbowCycle() or rainbowSingleColor(). I know that I should avoid use delay(), but it’s necessary to slow down animation. I made custom uDelay function (there may function to receive and update values).
I dont know how to change well ‘shared’ variables like ‘lightMode’. Sometimes when app calls BLYNK_WRITE(V1), lightMode shoud be changed to new value, so in next call setLightMode() in main loop program should enter to correct case but it doesn’t.
How to start fixing my problem ?