Hey Blynkers,
I need some advice and help. I used the following code and my RGB light fades between all the colors slowly. Looks awesome. But when I try to convert it to Blynk, it just flashes quickly, repeatedly. I think my problem lies with the timers, just can’t figure out what to do. I have not even started using the app , as I want it to continuously run when on, so no virtual pins have been created.
OLD WORKING CODE:
const int red = D8;
const int green = D7;
const int blue = D6;
int r = 255; /* red led value is temporally 255 and it will be the first led to light up */
int b; /* blue led value is temporally 0 */
int g; /* green led value is temporally 0 */
int t = 1000; /* "t" (time) 1000 milliseconds, feel free to change it */
void setup() {/* no setup */}
void loop() {
for (/* no initialization */; r>=0, b<255; b++, r--) /*red -> blue*/
{
analogWrite(red, r);
analogWrite(blue, b);
delay(t);
}
for (/* no initialization */; b>=0, g<255; g++, b--) /*blue -> green*/
{
analogWrite(blue, b);
analogWrite(green, g);
delay(t);
}
for (/* no initialization */; g>=0, r<255; r++, g--) /*green -> red*/
{
analogWrite(red, r);
analogWrite(green, g);
delay(t);
}
}
BLYNK CODE (NOT WORKING):
/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID "XXXXXXXXXX"
#define BLYNK_DEVICE_NAME "XXXXXXXXXX"
#define BLYNK_AUTH_TOKEN "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
char auth[] = BLYNK_AUTH_TOKEN;
const char* ssid;
const char* pass;
#include <ESP8266WiFi.h> //ESP STUFF
#include <BlynkSimpleEsp8266.h> //BLYNK STUFF
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <Ticker.h> //ALSO PART OF WIFI MANAGER
//CREATING OBJECTS:
BlynkTimer timer; // Creating a timer object
Ticker ticker;
WiFiManager wifiManager;
//TIME INT
int fade = 1000; //RGB FADE
/********************************** LDR LIGHT SENSOR SETTINGS START *********************/
//A0 = LDR = V4
#define ldr A0
int lx = analogRead(ldr);
/********************************** LDR LIGHT SENSOR SETTINGS END **********************/
/********************************** RGB SETTINGS START *********************/
//D6 - R - V10
//D7 - G - V11
//D8 - B - V12
//D6 & D7 & D8 = RGB = Smooth RGB Fade
const int red = D6; /* connected to PWM pin D6 */
const int green = D7; /* connected to PWM pin D7 */
const int blue = D8; /* connected to PWM D8 */
int r = 255; /* red led value is temporally 255 and it will be the first led to light up */
int b; /* blue led value is temporally 0 */
int g; /* green led value is temporally 0 */
//int sec = 50; /* "sec" (time) 1000 milliseconds, feel free to change it for rainbow */
/*************************************** RGB SETTINGS END **********************/
/*************************************** WIFI MANAGER STTINGS START ********************************/
void tick() { //Ticker...
//toggle state
int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin
digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state
}
void configModeCallback(WiFiManager * myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
Serial.println(myWiFiManager->getConfigPortalSSID());
ticker.attach(0.2, tick);
}
/************************************ WIFI MANAGER SETTINGS END **********************************/
/*********************************** LDR LIGHT SENSOR FUNCTIONS START *********************/
//A0
void sendSensorLDR() {
//int lx = analogRead(ldr);
lx = map(lx, 0, 1023, 0, 100);
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V4, lx);
Serial.print("\n");
Serial.print("Light lx in % is:");
Serial.println(lx);
Serial.print("\n");
(millis() / fade);
}
/************************************** LDR LIGHT SENSOR FUNCTIONS END ***********************/
/************************************** RGB RAINBOW START ***********************************/
void rainbowRGB() {
if (lx <= 30); //If it is less than 30%, turn on Rainbow.
{
Serial.print("RGB MODE: \n");
Serial.println("Rainbow\n");
Serial.println(lx); //Mode: Rainbow
//delay(sec);
//Smooth Fade
for ( /* no initialization */ ; r >= 0, b < 255; b++, r--) /*red -> blue*/ {
analogWrite(red, r);
analogWrite(blue, b);
//delay(sec);
(millis() / fade);
}
for ( /* no initialization */ ; b >= 0, g < 255; g++, b--) /*blue -> green*/ {
analogWrite(blue, b);
analogWrite(green, g);
//delay(sec);
(millis() / fade);
}
for ( /* no initialization */ ; g >= 0, r < 255; r++, g--) /*green -> red*/ {
analogWrite(red, r);
analogWrite(green, g);
//delay(sec);
(millis() / fade);
}
for ( /* no initialization */ ; r >= 0, b < 255; b++, r--) /*red -> blue*/ {
analogWrite(red, r);
analogWrite(blue, b);
//delay(sec);
(millis() / fade);
}
for ( /* no initialization */ ; b >= 0, g < 255; g++, b--) /*blue -> green*/ {
analogWrite(blue, b);
analogWrite(green, g);
//delay(sec);
(millis() / fade);
}
for ( /* no initialization */ ; g >= 0, r < 255; r++, g--) /*green -> red*/ {
analogWrite(red, r);
analogWrite(green, g);
//delay(sec);
(millis() / fade);
}
for ( /* no initialization */ ; r >= 0, b < 255; b++, r--) /*red -> blue*/ {
analogWrite(red, r);
analogWrite(blue, b);
//delay(sec);
(millis() / fade);
}
for ( /* no initialization */ ; b >= 0, g < 255; g++, b--) /*blue -> green*/ {
analogWrite(blue, b);
analogWrite(green, g);
//delay(sec);
(millis() / fade);
}
for ( /* no initialization */ ; g >= 0, r < 255; r++, g--) /*green -> red*/ {
analogWrite(red, r);
analogWrite(green, g);
//delay(sec);
(millis() / fade);
}
}
}
/************************************** RGB RAINBOW END ***************************************/
void setup() {
// Debug console
Serial.begin(9600);
/*************************** WIFI MANAGER SETUP START ********************************/
//START WIFI MANAGER: Declared object above: WiFiManager wifiManager;
ticker.attach(0.6, tick);
wifiManager.setTimeout(180);
wifiManager.setAPCallback(configModeCallback);
if (!wifiManager.autoConnect("XXXXXXXX")) // <-- PUT YOUR APP NAME HERE IT WILL SHOW THIS NAME WHEN CONNECTING TO WIFI
/*
*********** IMPORTANT: CONNECT TO 192.168.4.1 IN YOUR BROWSER **********
*/
{
Serial.println("failed to connect and hit timeout");
ESP.reset();
} //
ticker.detach();
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
/************************************ WIFI MANAGER SETUP END **********************************/
//WIFI MANAGER WORKAROUND - COMMENT OUT IF HARD CODING THE DEVICE
ssid = WiFi.SSID().c_str();
pass = WiFi.psk().c_str();
//BEGIN STUFF GOES HERE:
//Blynk.begin(auth, ssid, pass);
Blynk.config(auth); //try this, uncomment the one above if it doesn't work
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
//TIMERS
timer.setInterval(15000L, sendSensorLDR); //LDR SENSOR READINGS SENT TO BLYNK
timer.setInterval(1000L, rainbowRGB); // change RGB to rainbow in low light
void loop() {
Blynk.run();
timer.run();
}
I am using a D1 mini.
Any help will be appreciated.
Thanks.