Need help by making Wake-up light with WeMos d1 R2 + TEA5767 FM Radio

I don’t know what I want. My setup includes the pinMode…
I want to make a variable timer to make a fade in for my wake up light but don’t know yet how…
But now I get various errors when I want to write to any pinOut

@hutjeOk I have loaded your sketch into the compiler.

Not the reason for your problems but I will mention them anyway:

  1. Whitespace is free so perhaps use some between each fucntion.
  2. Everything within a function should be indented including the setup() function. Your while statement looked like it was something outside setup() because it isn’t indented.

I always do the pinMode as the first thing in setup(), you might want to try moving it.

I’ll look to see if I can find anything else.

Sprinkle your sketch with Serial.print’s specifically before row 155 (analogWrite crash):

Serial.print("Red at row 155 is );
Serial.print(red);
Serial.print(redCur at row 157 is ");
Serial.print(redcur);

Do I need to physically connect these pins during debugging?

Found the problem!!!

I need to declare the pins with D1, D2, D3 etc. instead of 1, 2 ,3 etc. :smiley:

I hope I am on the right way here… :neutral_face:

    void fadeInRGB(){
      
      if (wakeUpModeON == true && wakeUpFirstStart == true){
        int firstMillis = millis();
        int lastMillis = firstMillis + timeOfFade;
        int triggerMillis = 1000;
        int cycleMillis = firstMillis; 
        int currentMillis; 
        int redStep = redAlarm / timeOfFade; 
 
   // MAYBE THE STEPS ARE TOO SMALL... 
    //IF redAlarm = 1023 and timeOfFade = 1800000 , 1023/1800000= 5,683e-4
       
        int greenStep = greenAlarm / timeOfFade;
        int blueStep = blueAlarm / timeOfFade;
        wakeUpFirstStart = false;
        
      if (wakeUpModeON == true && wakeUpFirstStart == false){
        currentMillis = millis();
        if (currentMillis >= (cycleMillis + triggerMillis)){ // after roll out millis() could error
          cycleMillis = currentMillis;
          redCur += redStep;
          greenCur += greenStep;
          blueCur += blueStep;
          colorChange();
        }
      }
        
      }
    }
    void loop(){
      Blynk.run();
      fadeInRGB();
    }

This will probably interfere with Blynk a great deal. It’s not recommended to call functions from the loop(). Have you tried it? I think you will need to call the fadeInf routine using SimpleTimer. That may prove more stable and the result is even better readable code because you don’t have to mess with millis(), which, at best, makes for really unreadable code.

Okay, but I think the step variables will also be too low to read… if the steps are 0,0005683 for each milli the light has to fade. I will update the lights every 10 seconds I think.

Yeah, that’s not gonna fly. Steps have to be a minimum of 1. There are a couple of options out there you can use. Two of them are called constrain() and map(). They can handle a series of numbers and do some interesting stuff with it. Constrain() keeps values within boundaries and with map() you can map values from one into the other. For example, map(blabla,0, 1023, 0, 255) will make the values from variable blabla, which goes from 0 - 1023 from 0 - 255. So if the value is, about 4, the map() will put it to 1. You can also reverse ranges of numbers with that.

I found a library that can fade leds without using delay, only I get an error. #include <avr/pgmspace.h>
I need to have this library but can’t find the file to download…
Apparently the esp8266 doesn’t work with all the AVR libraries… :frowning:

I had that too with another library. It’s basically setup wrong in the library itself. It contains a path to the pgmspace.h which is not correct. Can you show me which lib? I’ll see if I can fix it (I probably need it too at some point, LOL).

I’m currently doing a three step-light for my sunrise lamp. It’s doing three different colors based on the time of sunrise/sunset. Now for the fading…

The library which I can’t use is only for AVR structure chips… Apparently not for esp8266 for what I have found out…

Already found it:

/* 
 * File:   Curve.h
 * Author: cameron
 *
 * Created on October 22, 2013, 1:07 AM
 */

#ifndef CURVE_H
#define	CURVE_H

#include <pgmspace.h>

class Curve {
 static const uint8_t etable[] PROGMEM;
public:
 static uint8_t exponential(uint8_t);
 static uint8_t linear(uint8_t);
 static uint8_t reverse(uint8_t);
};

#endif	/* CURVE_H */

The include in the original Curve.h file points to a hard path. Remove the avr/ part and it’ll compile, not sure if it’ll work, but at least it compiles :wink: I think the IDE will figure out on it’s on which pgmspace it needs for which architecture based on the board selection you make.

The LED fader doesn’t work with this…

But the SimpleTimer does!!!

#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>
#define PWMRANGE 1023
char auth[] = "41a8ad5b2ad54e6cbdcd9cef7ffbf09c"; // == thuis
char ssid[] = "onsthuis-1";
char pass[] = "2beh-er57-3k6m";

SimpleTimer timer;
int alarm1;

//Pin LAYOUT
const int red = D4;
const int green = D5;
const int blue = D6;
const int fan = D7;
//Pin values

double fanSpeed; // = redCur+greenCur+blueCur/3
double redCur; //Current value
double greenCur; //Current value
double blueCur;  //Current value

//Virtual pin declarations

//LIGHT CONTROL
boolean alarmTrigger; // V1 trigger of alarm
boolean alarmToggle; //V2 Toggle alarm
//V3 Reads fanSpeed value
boolean lightToggle; //V4 Toggles light

int redWidget; //V6 ZeRGBa red
int greenWidget; //V7 ZeRGBa green
int blueWidget; //V8 ZeRGBa blue
boolean colorChanged; // prevents writing lightValue if already did
//ALARM SETTINGS
int redAlarm; //V9 Max value red alarm
int greenAlarm; //V10 Max value green alarm
int blueAlarm; //V11 Max value blue alarm
double timeOfFade; //V12 Index of fading time alarm [5min, 10min, 15min, 20min, 30min]
int maxAlarmRadioVolume; //V13 max volume Radio at end of timeOfFade
boolean alarmRadio; //V14 alarm radio on/off
boolean wakeUpModeON = false; //Blocks RGB Control during alarm !ONLY IN CODE NOT IN BLYNK-APP!
boolean wakeUpFirstStart = true; // Makes settings for first start wakeUpModeON.
double redStep;
double greenStep;
double blueStep;

//RADIO CONTROL
boolean radioToggle; //V15 Toggles radio on/off
boolean volumeUp; //V16 Button Volume up
boolean volumeDown; //V17 Button Volume down
int volume; //volume of radio
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.
WidgetLCD lcdRadio(V21);

void setup() {

  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(fan, OUTPUT);
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, "RASPBERRYPI");

  while (Blynk.connect() == false) {
  }
  Blynk.syncAll();
}

//BLYNK Declarations
BLYNK_CONNECTED() {
  Blynk.syncAll();
}

BLYNK_WRITE(V1) {
  alarmTrigger = param.asInt(); // is HIGH when alarm goes off
  if (alarmToggle == HIGH && alarmTrigger == HIGH) { //Starts WakeUpMode when I turned on the alarm && alarm goes off
    wakeUpModeON = true; //Blocks RGBControl during wakeUpMode
    wakeUpMode();
  }
  if (alarmTrigger == LOW || alarmToggle == LOW) {
    wakeUpFirstStart = true;
    wakeUpModeON = false;
    

  }
}

BLYNK_WRITE(V2) {
  alarmToggle = param.asInt(); // Gives the current value of the alarmToggle
  if (alarmToggle == LOW || alarmTrigger == LOW) {
    wakeUpFirstStart = true;
    wakeUpModeON = false;
    timer.deleteTimer(alarm1);
  }
}

BLYNK_READ(V3) {
  Blynk.virtualWrite(3, fanSpeed); // Writes the speed of the fan to value widget
}
BLYNK_WRITE(V4) {
  lightToggle = param.asInt(); // Gives the current value of the lightToggle
  ZeRGBa();
}


BLYNK_WRITE(V6) {
  redWidget = param.asInt(); // writes red value ZeRGBa to redWidget
  if (redWidget != redCur) {
    ZeRGBa();
  }
}

BLYNK_WRITE(V7) {
  greenWidget = param.asInt(); // writes green value ZeRGBa to greenWidget
  if (greenWidget != greenCur) {
    ZeRGBa();
  }
}

BLYNK_WRITE(V8) {
  blueWidget = param.asInt(); // writes blue value ZeRGBa to blueWidget
  if (blueWidget != blueCur) {
    ZeRGBa();
  }
}

BLYNK_WRITE(V9) {
  redAlarm = param.asInt(); // writes max red value to redAlarm

}

BLYNK_WRITE(V10) {
  greenAlarm = param.asInt(); // writes max green value to greenAlarm
}

BLYNK_WRITE(V11) {
  blueAlarm = param.asInt(); // writes max blue value to blueAlarm
}

BLYNK_WRITE(V12) {
  timeOfFade = param.asInt() * 60; // Writes time of fade to seconds

}

BLYNK_WRITE(V13) {
  maxAlarmRadioVolume = param.asInt(); // max volume Radio at end of timeFade
}

BLYNK_WRITE(V14) {
  alarmRadio = param.asInt();
}

BLYNK_WRITE(V15) {
  radioToggle = param.asInt();
  startRadio();
}

BLYNK_WRITE(V16) {
  volume += param.asInt();
  volumeRadio();
}

BLYNK_WRITE(V17) {
  volumeDown -= param.asInt();
  volumeRadio();
}

BLYNK_WRITE(V18) {
  scanForward = param.asInt();
}

BLYNK_WRITE(V19) {
  scanBackward = param.asInt();
}

BLYNK_WRITE(V20) {
  channelPreset = param.asInt();
  //1=Q-Music, 2=Sky-Radio, 3=3FM, 4=538, 5=NPO
}

//V21 LCD RadioController

//COLOR CHANGE
void colorChange() {
  if (colorChanged == true) {
    analogWrite(red, redCur);
    analogWrite(green, greenCur);
    analogWrite(blue, blueCur);
    Blynk.virtualWrite(6, redCur);
    Blynk.virtualWrite(7, greenCur);
    Blynk.virtualWrite(8, blueCur);
    fanSpeedControl();
    colorChanged = false;
  }
}

//AUTOMATION

void wakeUpMode() {
  //  if (alarmRadio == HIGH) {
  //    startRadio();
  //  }
  if (wakeUpModeON = true) {
    Serial.println("Enter wakeUpMode");
    if (wakeUpFirstStart == true) {
      alarmSetup();
      Serial.print("Step RED: ");
      Serial.println(redStep);
      Serial.print("timeOfFade ");
      Serial.println(timeOfFade);
      wakeUpFirstStart = false;
      int alarm1 = timer.setTimer(1000, alarmFader, timeOfFade);
      timer.enable(alarm1);
    }

    
  }
}

//FADING

void alarmSetup() {
  redStep = redAlarm / timeOfFade;
  greenStep = greenAlarm / timeOfFade;
  blueStep = blueAlarm / timeOfFade;
  
}

void alarmFader() {
  redCur += redStep;
  greenCur += greenStep;
  blueCur += blueStep;
  colorChanged = true;
    colorChange();

}

//Fan Control
void fanSpeedControl() {
  if (redCur >= 0 || greenCur >= 0 || blueCur >= 0) {
    fanSpeed = (redCur + greenCur + blueCur) / 3;
    if (fanSpeed < 0)fanSpeed = 0;
    if (fanSpeed > 1023)fanSpeed = 1023;
  }
  analogWrite(fan, fanSpeed);
}

void ZeRGBa() {
  if (lightToggle == false) {
    redCur = 0;
    greenCur = 0;
    blueCur = 0;
    colorChanged = true;
    colorChange();
  }
  if (wakeUpModeON == false && lightToggle == true) {
    redCur = redWidget;
    greenCur = greenWidget;
    blueCur = blueWidget;
    colorChanged = true;
    colorChange();

  }
}
//Radio Control

void startRadio() {
  // Start and stop radio command
}
void scanRadio() {
  //scan forward and backward
}
void volumeRadio() {
  //Controls volume Radio
}
 
void loop() {
  Blynk.run();
  timer.run();



}

That looks pretty nifty. I think it can be more squeezed and optimized, but I’m sure it’ll get the job done! For example, why use all the double variables? I think int will suffice and I think it’ll save some memory space.

I think I’ll implement something similar, it looks as if this is what I need to, except I’m going to predefined colors, but basically the same logic.

Because when I make the steps for the fade, redStep = redAlarm / timeOfFade;
So redStep could be 0.44444 when time op fade is 1800 seconds (30min*60) and my redAlarm value could be 800.
800/1800=0.4444 so it need small steps. The analogWrite shall ignore the decimal numbers, but I still want to count up the redCur with the redSteps. So maybe the pinout gets 8,2 and a second later 8,7 and still the pin doesn’t change, because the pin ignores the decimal. So when the pinout gets 9,2 the pin will be 9.

Also, I found out I couldn’t use the zeRGBa when I want to realtime fade, because I send too much data to the board and it will reset. I now have three individual faders which I can realtime fade because I only move 1 fader at a time. In the zeRGBa you move 3 faders simultaneously.

I have tested it in the original setting… Everything went well, until I was setting all things up with the power still on…:sob:
Now the WeMos died…
Now I have to wait a month until my new WeMos has arrived.

But at least your new WeMos will have an external aerial port and 16M.
You are buying the Mini Pro, aren’t you?

Yes, but I need everything to work with 12V so I also buy the WeMos D1 R2 again.