Run Loop when a button is pressed

I want to make a lithophane lamp with a fading animation for the LEDs. For that i have a 5050 LED strip and 3 mosfets to controll the color. When i want to do the fade, i first tried a while command inside the blynk_write function. However there the fade can not be stopped anymore, so i read about timer functions, so I tried this code. If I press the button now, it does nothing. Can anybody help there?

Thanks in advance.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "X-xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
BlynkTimer timer;

#define REDPIN D2
#define GREENPIN D1
#define BLUEPIN D0
int FADESPEED = 3;
int pinValue =0;
int SliderValue =3;
int StartDoTimer;

void setup()
{
  // Debug console
  Serial.begin(9600);
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  Blynk.begin(auth, ssid, pass);
 LEDfadeTimer = timer.setInterval(1000L, LEDfade);
  timer.disable(LEDfadeTimer); // Turn StartDoTimer off
}

BLYNK_WRITE(V2)
{
SliderValue = param.asInt();
int FADESPEED = SliderValue;
}

BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); 
 if(param.asInt()==1)
{ 
timer.enable(LEDfadeTimer );
  Serial.print("V1 IS ON");
}
  else
{
  Serial.print("V1 IS OFF ");
timer.disable(LEDfadeTimer );
}
}

void LEDfade(){ 
  if (pinValue == 1){
   int r, g, b;
  // fade from blue to violet
  for (r = 0; r < 1023; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from violet to red
  for (b = 1022; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from red to yellow
  for (g = 0; g < 1023; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
  // fade from yellow to green
  for (r = 1022; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from green to teal
  for (b = 0; b < 1023; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from teal to blue
  for (g = 1022; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
  }
  }
  
void loop()
{
  Blynk.run();
  timer.run();
}

pinValue is a local variable here, you can’t use it elsewhere.

as a result, pinValue is equal to 0 here.

Pete.

2 Likes

Thank you. So just
int pinValue =0;
in front of the void setup() and then
pinValue = param.asInt(); in the Blynk_write function and it should work right?

Yo can declare pinValue as global (as you says at the beggining of the code), and use it everywhere, even inside the BLYNK_WRITE(V1)

Correct.

When you’re trying to debug this type of situation, it’s often helpful to put a Serial.print statement in your code, just before the ‘if’ statement to show the value of the variable (pinValue in this case). You’d then realise that the if statement wasn’t evaluating to true, because pinValue was 0, not 1.

Even when you do declare pinValue as a global variable (by putting the declaration at the top of your code), the compiler will allow you to re-declare it as local within a function, which is why you have to use pinValue = param.asInt(); in your BLYNK_WRITE function, not int pinValue = param.asInt(); as this would zero-out the value again.

Is it working as expected with these changes?

I gave a feeling that the execution of the loop, along with the delays you’re using, will give Blynk disconnections during the fade operation. You can overcome this with some Blynk.run(); statements in your fade loop, to ensure that the Blynk library isn’t starved of processor time.

Pete.

2 Likes