Fading an LED on ESP32

Hello everyone,
as part of a larger project I am trying to get an LED to fade in and out when a virtual pin is toggled on, and then turn off when the virtual pin is off. I have several codes that work outside of blynk, meaning that i can use each one as a standalone code to get the LED to fade the way i want it, but when i try to import the code into my blynk project i get nothing. Obviously i am missing something, but after days and several approaches, i am yet to see what that is. Can anyone point me in the right direction?
Thanks!

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#define BLYNK_AUTH_TOKEN ""

#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32_SSL.h>

char ssid[] = "";
char pass[] = "";

BlynkTimer timer;

int blinkLed=0;
unsigned long UnlockCheckM;
unsigned long LockCheckM;
unsigned long proximityM =0;
int proximityCounter=0;

int bck = 18; 
int fwd = 5; 
int pirsensor=25; 
int bckswtch=32; 
int fwdswtch=33;
int chrgprt=2; 
int light = 19;
int chargelvl;
int blink = 13;
int emglock=35;

double lon=;
double lat=;

BLYNK_WRITE(V0){
  if(param.asInt()==1){
    digitalWrite(chrgprt,HIGH);
    Blynk.setProperty(V0, "url", 1);
    Blynk.setProperty(V11, "isHidden", false);
   } else {
    digitalWrite(chrgprt,LOW);
    Blynk.setProperty(V0, "url", 0);
    Blynk.setProperty(V11, "isHidden", true);
  }
}

BLYNK_WRITE(V1){
  if(param.asInt()==1){
    digitalWrite(light,HIGH);
    blinkLed=0;
    Blynk.virtualWrite(V7,0);
    digitalWrite(blink,LOW);
   } else {
    digitalWrite(light,LOW);
  }
}

BLYNK_WRITE(V4){
  if(param.asInt()==1){
    digitalWrite(fwd,HIGH);
    LockCheckM=millis();
    Blynk.setProperty(V5,"isDisabled",true);
   } else {
    digitalWrite(fwd,LOW);
    Blynk.setProperty(V5,"isDisabled",false);
  }
}

BLYNK_WRITE(V5){
  if(param.asInt()==1){
    digitalWrite(bck,HIGH);
    UnlockCheckM=millis();
    Blynk.setProperty(V4,"isDisabled",true);
   } else {
    digitalWrite(bck,LOW);
    Blynk.setProperty(V4,"isDisabled",false);
  }
}

BLYNK_WRITE(V7){
 int blinkLed= param.asInt();
}


BLYNK_WRITE(V11){
;
}

void lockSafety()
{
  if(digitalRead(fwdswtch)==LOW){
  Blynk.virtualWrite(V6,0);
  } else if (digitalRead(fwdswtch)==HIGH){
  Blynk.virtualWrite(V6,1);
  digitalWrite(fwd,LOW);
  Blynk.virtualWrite(V4,0);
  Blynk.setProperty(V5,"isDisabled",false);
  }
}

void unlockSafety()
{
  if(digitalRead(bckswtch)==LOW){
  Blynk.virtualWrite(V2,0);
  } else if (digitalRead(bckswtch)==HIGH){
  Blynk.virtualWrite(V2,1);
  digitalWrite(bck,LOW);
  Blynk.virtualWrite(V5,0);
  Blynk.setProperty(V4,"isDisabled",false);
  }
}

void gauge()
{
 int ldrVal=analogRead(chargelvl);
 Blynk.virtualWrite(V3,ldrVal);
}

void failSafeUnlock(){
  if(millis()-UnlockCheckM>=4000 && digitalRead(bck)==HIGH){
  digitalWrite(bck,LOW);
  Blynk.virtualWrite(V5,0);
  Blynk.setProperty(V4,"isDisabled",false);
  }
}

void failSafeLock(){
  if(millis()-LockCheckM>=4000 && digitalRead(fwd)==HIGH){
  digitalWrite(fwd,LOW);
  Blynk.virtualWrite(V4,0);
  Blynk.setProperty(V5,"isDisabled",false);
  }
}

void blink_led()
{
  if(blinkLed == 1){
  int time = millis();
  value = 128+127*cos(2*PI/10000*time);
  analogWrite(blink, value); 
  }else{
  analogWrite(blink,LOW)
  }
}

void proximity(){
 if (digitalRead(fwdswtch)==HIGH && digitalRead(pirsensor)==LOW && proximityCounter==0){
    proximityM=millis();
    proximityCounter ++;
    Serial.println(proximityCounter);
    }else if (digitalRead(fwdswtch)==HIGH && digitalRead(pirsensor)==LOW){
    proximityCounter ++;
    Serial.println(proximityCounter);
} else {
  proximityCounter=0;
}
}

void proximityEvent(){
  if(proximityCounter==4 && millis()-proximityM <= 4000){
    Blynk.logEvent("proximity");
    Blynk.virtualWrite(V9,1);
    proximityCounter=0;
    }else{
  Blynk.virtualWrite(V9,0);
}
}

void gmap(){
  Blynk.virtualWrite(V10,lon,lat);
}

void emergencylock() {
  if(digitalRead(emglock)==LOW){
    digitalWrite(fwd,LOW);
    digitalWrite(bck,LOW);
    Blynk.setProperty(V4,"isDisabled",false);
    Blynk.virtualWrite(V5,0);
    Blynk.setProperty(V5,"isDisabled",false);
    Blynk.virtualWrite(V4,0);
  }
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(100L, lockSafety);
  timer.setInterval(100L, unlockSafety);
  timer.setInterval(60000L,gauge);
  timer.setInterval(50L,blink_led);
  timer.setInterval(100L,failSafeUnlock);
  timer.setInterval(100L,failSafeLock);
  timer.setInterval(800L,proximity);
  timer.setInterval(100L,proximityEvent);
  timer.setInterval(60000L,gmap);
  timer.setInterval(100L,emergencylock);
  //timer.setInterval(1000L,safetyBlink);
  pinMode(fwd,OUTPUT);
  pinMode(bck,OUTPUT);
  pinMode(chrgprt,OUTPUT);
  pinMode(light,OUTPUT);
  pinMode(blink,OUTPUT);
  pinMode(chargelvl,INPUT);
  pinMode(emglock,INPUT_PULLUP);
  pinMode(bckswtch,INPUT_PULLUP);
  pinMode(fwdswtch,INPUT_PULLUP);
  pinMode(pirsensor,INPUT_PULLUP);
}

void loop()
{
  Blynk.run();
  timer.run();
}

Specifically, the function i am using to try and get this to work is blink_led(), connected to virtual pin 7.
The pwm pin i am using for the LED is pin number 13.

Thank you in advance!

The int at the beginning of this line of code declares a local copy of the blinkLed variable, and the value assigned to that variable is only visible inside the BLYNK_WRITE(V7) function.

Remove the int, so that the value will be assigned to the global variable you assigned here…

which will be visible and can be evaluated inside your blink_led() function.

Pete.

1 Like

Thanks a lot Pete, as always! That was the issue, it works great now!

1 Like