Hi,
I am working on a wake-up light with the WeMos d1 R2 and Blynk local server on Raspberry Pi (Which is fully operational)
With a RGB floodlight.
I need to slowly dim the lights from 0 to 1023 in an amount of time.
The time of dimming needs to be adjusted from within the app. (Menu widget to select 5, 10 or 15 minutes)
The start time is easy to configure with the timer widget.
The final color is a fixed color, beginning with a red color and ending with an orange/yellow color.
The waking up state (alarm) can be stopped when pushing a button widget.
During the alarm, the zeRGBa widget can’t be used.
I have a PWM fan for cooling the RGB. The speed of this fan is calculated on the brightness of the RGB. (Example: Value R+G+B/3=fanSpeed)
Optional: I want to connect the TEA5767 FM Radio to wake up with music, controlling with I2C. (fading volume is inside the TEA5767)
I tried to make it work, but I don’t know if I have to put things inside of the loop. Otherwise I don’t know where/how I can properly program this.
Normally I program everything inside the loop and need to assign every pin if it’s an output or input.
#include <Wire.h>
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "xxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxx";
//Pin LAYOUT
int red = 4;
int green = 5;
int blue = 6;
int fan = 7;
//Virtual pin declarations
//LIGHT CONTROL
boolean alarmTimer; // V1 time of alarm
boolean alarm; //V2 Toggle alarm
//V3 Reads fanSpeed value
boolean lightToggle; //V4 Toggles light
//V5 LCD in Tab "Light Control"
int redWidget; //V6 ZeRGBa red
int greenWidget; //V7 ZeRGBa green
int blueWidget; //V8 ZeRGBa blue
//ALARM SETTINGS
int redAlarm; //V9 Max value red alarm
int greenAlarm; //V10 Max value green alarm
int blueAlarm; //V11 Max value blue alarm
int timeOfFade; //V12 Index of fading time alarm [5min, 10min, 15min, 20min, 30min]
int maxAlarmRadioVolume; //V13 Max alarm radio volume
int alarmRadio; //V14 alarm radio on/off [With, Without]
//RADIO CONTROL
boolean radioToggle; //V15 Toggles radio on/off
boolean volumeUp; //V16 Button Volume up
boolean volumeDown; //V17 Button Volume down
boolean scanForward; //V18 Button Scan radio channel forward
boolean scanBackward; //V19 Button scan radio channel backward
int channelPreset; //V20 Preset Radio frequencies [Q-Music, Sky-Radio, 3FM, 538, NPO.
//V21 LCD in tab "Radio Control"
//Pin values
int fanSpeed; // = redCur+greenCur+blueCur/3
int redCur; //Current value
int greenCur; //Current value
int blueCur; //Current value
void setup(){
Serial.begin(9600);
Blynk.begin(auth, ssid, pass, "RASPBERRYPI");
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(fan, OUTPUT);
}
//BLYNK Declarations
BLYNK_CONNECTED() {
Blynk.syncAll();
}
BLYNK_WRITE(V1){
int timer = param.asInt(); // assigning incoming value from pin V1 to a variable
if (timer == HIGH) {
alarm = true;
}
else {
alarm = false;
}
}
BLYNK_WRITE(V2){
button = param.asInt(); // assigning incoming value from pin V1 to a variable
}
BLYNK_WRITE(V6){
redWidget = param.asInt(); // assigning incoming value from pin V6 to a variable
}
BLYNK_WRITE(V7){
greenWidget = param.asInt(); // assigning incoming value from pin V7 to a variable
}
BLYNK_WRITE(V8){
redWidget = param.asInt(); // assigning incoming value from pin V8 to a variable
}
BLYNK_READ(V3){
Blynk.virtualWrite(3, fanSpeed); // Writes the speed of the fan to value widget
}
void loop(){
Blynk.run();
if (alarm == true){
//Time dimming code, SimpleTimer is a solution.
}
else(){
analogWrite(red, redWidget);
analogWrite(green, greenWidget);
analogWrite(blue, blueWidget);
}
//Fan Speed
if (redCur > 0 || greenCur > 0 || blueCur > 0) {
fanSpeed = (redCur + greenCur + blueCur) / 3;
if (fanSpeed < 0)fanSpeed = 0;
if (fanSpeed > 1023)fanSpeed = 1023;
}
//Fan driver
analogWrite(fan, fanSpeed);
}