Hi everyone, I’m trying to implement a project using FastLED and Blynk, but I’m having some issues with connection dropping.
I’m using:
• Arduino Uno with HC-06 bluetooth connection
• Android 8.0
• Blynk library v0.5.2
I divided my code into 4 tabs but I guess the relevant parts are just the following:
Main
#include <FastLED.h>
#include <MovingAverage.h>
#include <SoftwareSerial.h>
#include <BlynkSimpleStream.h>
//definig values for led strip
#define COLOR_ORDER BRG
#define NUM_LEDS 100
#define CHIPSET WS2811
#define MIN_BRIGHTNESS 1
#define MAX_BRIGHTNESS 255
//function declarations
//--led control
void rainbow();
void solid_color();
//bluethoot stuff
#define BLYNK_PRINT Serial
SoftwareSerial SwSerial(10, 11); // RX, TX
SoftwareSerial SerialBLE(10, 11); // RX, TX
char auth[] = "6d71e39059e84302be5cf30fab6466e6";
//declarating pins and led array
const int led_pin=7;
CRGB leds[NUM_LEDS];
int mic_pin = A0;
int photo_pin = A1;
//defining blynk stuff
#define zebra_vpin V0
#define led_vpin V1
#define delay_slider_vpin V2
#define music_slider_vpin V3
#define bright_slider_vpin V4
#define menu_vpin V5
//declarating blynk parameters
WidgetLED led_colorW(led_vpin);
CRGB color = CRGB::Red;
int delay_time=100;
int sample_window = 70;
int Wbright=100;
int behavior=1;
bool reconnect=true;
//declarating timers
BlynkTimer timer;
int rainbow_timer=timer.setTimer(delay_time, rainbow,1);
int solid_timer=timer.setTimeout(delay_time,solid_color);
//declarating avarages
MovingAverage raverage(0.6), max_av, min_av;
void setup() {
//begin serail comunication
Serial.begin(9600);
Serial.println("Starting...");
delay( 3000 ); // power-up safety delay
Serial.println("Looking for connection...");
SerialBLE.begin(9600);
Blynk.begin(SerialBLE, auth);
Serial.println("Connection enstablished");
//setting leds and maximum power consumption
FastLED.addLeds<CHIPSET, led_pin, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalSMD5050 );
//photo resistor EMAs
raverage.reset( 0.0 );
max_av.reset( 0.0 );
min_av.reset( 0.0 );
Serial.println("Setting up application...");
setup_app();
Serial.println("Starting...");
}
void loop()
{
switch(behavior){
case 1:
solid_color();
break;
case 2:
strobo();
break;
case 3:
read_mic();
break;
case 4:
read_mic_adaptive();
break;
case 5:
multi_chase(1);
break;
case 6:
rainbow();
break;
}
timer_set(behavior);
// in case of riconnectio from phone reinitializate widgets with current values
if(!Blynk.connected()){
reconnect=true;
Serial.println("disconnected");
//Blynk.connect();
}
else if(reconnect){
Serial.println("reconnected");
setup_app();
}
Blynk.run();
timer.run();
}
Blynk Stuff
//zebra rgb
BLYNK_WRITE(zebra_vpin) {
int r,b,g;
r=param[0].asInt();
g=param[1].asInt();
b=param[2].asInt();
Serial.print("recieved color :");
Serial.print(r);
Serial.print(",");
Serial.print(g);
Serial.print(",");
Serial.println(b);
color = CRGB(r,g,b);
Blynk.setProperty(V1, "color", rgb2hex(r,g,b));
}
//delay slider
BLYNK_WRITE(delay_slider_vpin) {
delay_time=param.asInt();
Serial.print("changed dalay time to :");
Serial.println(delay_time);
}
//music sample window
BLYNK_WRITE(music_slider_vpin) {
sample_window=param.asInt();
Serial.print("changed sample_window to :");
Serial.println(sample_window);
}
//solid color brightness
BLYNK_WRITE(bright_slider_vpin) {
Wbright=param.asInt();
Serial.print("brightness changed to :");
Serial.println(Wbright);
}
BLYNK_WRITE(menu_vpin) {
behavior=param.asInt();
Serial.print("behavior changed to :");
Serial.println(behavior);
}
void setup_app(){
//setting initial blynk variables to initial values
Blynk.virtualWrite(delay_slider_vpin,delay_time);
Blynk.virtualWrite(music_slider_vpin,sample_window);
Blynk.virtualWrite(bright_slider_vpin,Wbright);
Blynk.setProperty(menu_vpin,"labels",
"solid color","strobo","music","adaptive music","chasing","rainbow");
Blynk.virtualWrite(menu_vpin,behavior);
led_colorW.on();
Blynk.setProperty(led_vpin,"color","#D3435C");
reconnect=false;
}
Led Control
void multi_chase( int num_chasing)
{
int space_chase = NUM_LEDS / num_chasing;
bool to_break = false;
int chaser[num_chasing];
int idx;
//initializing position array
for (int j = 0; j < num_chasing; j++)
{
chaser[j] = space_chase * j;
}
for (int i = 0; i < NUM_LEDS; i++) {
//for every element in the position array
for (int j = 0; j < num_chasing; j++) {
//if the j-th led is outside the array
if (chaser[j] + i >= NUM_LEDS) {
//set i=0 to get to the starting postition
i = 0;
//break ath the end of the cycle
to_break = true;
}
//color the led
idx = chaser[j] + i;
leds[idx] = color;
//set bright
FastLED.setBrightness(Wbright);
//update blynk
Blynk.run();
}
//show the led
FastLED.show();
// Wait a little bit if it's not the first iteration (prevent lag)
if (i) {
delay(delay_time);
}
//turn all leds off
for (int j = 0; j < NUM_LEDS; j++) {
leds[j] = CRGB::Black;
}
if (to_break) {
break;
}
}
}
void multi_chase_grad( int num_chasing)
{
CRGBArray<NUM_LEDS> chaser_array;
fill_solid(leds, 3, CRGB(0, 0, 200));
FastLED.show();
delay(1000);
fadeToBlackBy(leds, 3, 200);
chaser_array.fadeToBlackBy(100);
}
int DISPLAYTIME=20;
int BLACKTIME=0;
void rainbow() {
// draw a generic, no-name rainbow
static uint8_t starthue = 0;
fill_rainbow( leds + 5, NUM_LEDS - 5, --starthue, 20);
// Choose which 'color temperature' profile to enable.
uint8_t secs = (millis() / 1000) % (DISPLAYTIME * 2);
if ( secs < DISPLAYTIME) {
FastLED.setTemperature( Tungsten100W ); // first temperature
leds[0] = Tungsten100W; // show indicator pixel
} else {
FastLED.setTemperature( OvercastSky ); // second temperature
leds[0] = OvercastSky; // show indicator pixel
}
// Black out the LEDs for a few secnds between color changes
// to let the eyes and brains adjust
if ( (secs % DISPLAYTIME) < BLACKTIME) {
memset8( leds, 0, NUM_LEDS * sizeof(CRGB));
}
FastLED.setBrightness(Wbright);
FastLED.show();
}
//Strobo
void strobo() {
FastLED.setBrightness(Wbright);
fill_solid(leds, NUM_LEDS, color);
FastLED.show();
delay(delay_time);
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
delay(delay_time);
}
void mic_led(int bright){
FastLED.setBrightness(bright);
fill_solid(leds, NUM_LEDS, color);
FastLED.show();
}
void solid_color(){
FastLED.setBrightness(Wbright);
fill_solid(leds, NUM_LEDS, color);
FastLED.show();
delay(delay_time);
}
The problem is the following:
I start the whole program having the solid color function on, while the rest is off.
As you can see this function has a delay (I know i shouldn’t use them, I’m working on it), the problem is that if I remove the delay, the program speeds up and I have connections drops (In main the if (!Blynk.connected())
returns true). On the other hand, if I increase the daly above circa 250ms the connections drops again, as expected.
I tried to use timers instead of delays but it results in no connection at all (just the first one in the setup).
It seems as if I’m sending to much data throught the stream, but the only data output (from the arduino to the phone) is the setup_app()
function which is only called when the connection drops.
Can anyone help me please?