BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

LED fading


#1

Rookie needs help!! :see_no_evil:
Does someone know how to modify the widget led-sketch from sketch builder so, that it starts with an input.
For exampe if button V1 is pressed, it will start do fade up until its highest, than again down until its lowest and so on. Essentially I want know only where I can put the condition to start it.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "YourAuthToken";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

WidgetLED led2(V2);

BlynkTimer timer;

void fadeLedWidget()
{
  static int value = 0;
  static int delta = 30;
  value += delta;
  if (value > 255 || value < 0) {
    delta = -delta;
  } else {
    Serial.print("LED on V2: ");
    Serial.println(value);
    led2.setValue(value);
  }
}
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(300L, fadeLedWidget);
}
void loop()
{
  Blynk.run();
  timer.run();
}

Normally I program everything so, that I call every function from an other function but here it does not works, because the command to fade up and to fade down become called usually every 100ms from the timer.

How I can say:

{
  int button = param.asInt();
  if (button == 1) {
////////////////////////////////
Only if these condition above is right, the led should start to fade up and down
////////////////////////////////
  }

Would be very nice, if someone could help a rookie like me
:smiley:


#2

You could use that button variable (must be changed to a global) and insert it as a if() flag in your fade routine.

https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

The timer will keep calling the routine, but the if() will only process the fade if the button == 1

You could also do something simular to disable and enable the timer as needed… check out the various options with timers

https://playground.arduino.cc/Code/SimpleTimer#Functions


#3

Here is some untested code that may do what you are asking. Normally we do not write code for people, but I had a little time to kill, and it seemed within my skill-set. Hopefully it works.

I believe this is what @Gunner is describing in the second half of his post above.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "YourAuthToken";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

WidgetLED led2(V2);

int timerLED;

BlynkTimer timer;

BLYNK_CONNECTED() 
{
Blynk.syncVirtual(V1);
}

BLYNK_WRITE(V1)
{   
  int button = param.asInt(); // Get value as integer
   if (button == 1) 
  {
        timer.enable(timerLED);
  }
  else
  {
     timer.disable(timerLED);
  }
}

void fadeLedWidget()
{
  static int value = 0;
  static int delta = 30;
  value += delta;
  if (value > 255 || value < 0) {
    delta = -delta;
  } else {
    Serial.print("LED on V2: ");
    Serial.println(value);
    led2.setValue(value);
  }
}
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timerLED = timer.setInterval(300L, fadeLedWidget);
  timer.disable(timerLED);
}
void loop()
{
  Blynk.run();
  timer.run();
}

#4

Thanks guys!
So keep on programming! :hugs:


#5

Could you please explain me you solution more detailed. I do not understand what you mean with

You could use that button variable (must be changed to a global) and insert it as a if() flag in your fade routine.
The timer will keep calling the routine, but the if() will only process the fade if the button == 1

I only heard once something from global variables, but this is also the only thing I understood and what I can

How is it possible to insert a for in a loop. Can not find any example.
Sorry, but I come from the planet where people are still using simple delays. :joy: instead of this crazy shit :stuck_out_tongue:
That is all new for me, you understand. I also read on webpage about SimpleTimer, but I do not understand it really. So last but not least I tought, that if I am not able to code alone, I can copy the sketch from Toro_Blanco. But it also does not work :tired_face: #couldkillme. When I compile it I get usually the error “timerLED’ was not declared in this scope”. I would really interessed to know how both methonds works.
Thx! :v:


#6

#7

I have edited the code above, it should compile now.


#8

Thanks bro! :wink:
Hope to understand it now. I lerned at most if I have an example on which base I can work on :blush: