Fade LED on button press

Hey,
Im new to Blynk and still learning.
In my project I have an arduino nano with a HC-05 bluetooth module and two LEDs.

Basically the leds are on Pin D9 and im trying to get them to fade off when I press a button on V1 and to then fade back on again when I release V1. The idea is to make it look like eyes blinking open & close.

I will attach my current code but it just won’t work. It compiles and uploads but that is it.

#define BLYNK_USE_DIRECT_CONNECT

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3);

#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleSerialBLE.h>

char auth[] = "xxxxxx";

int leds = 9;
int fadeAmount = 5;
int brightness = 0;
int pinValue = 0;


void setup()
{
  // Debug console
  DebugSerial.begin(9600);

  DebugSerial.println("Waiting for connections...");
  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  pinMode(leds, OUTPUT);
  
}

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

BLYNK_WRITE(V1)
{
  pinValue = param.asInt();
  if (pinValue == 1) {
    (brightness + fadeAmount <= 255);
    brightness += fadeAmount;
  }
  else if (pinValue == 0) {
    (brightness - fadeAmount >= 0);
    brightness -= fadeAmount;

  analogWrite(leds, brightness);
  delay(60);
  }
}

Any help would be much appreciated. I can control D9 through a slider and a button on Blynk, just not the fade code on V1.

Many Thanks

The BLYNK_WRITEvPin) command executes once only, when the value of the widget that is attached to the virtual pin.
It appears that you are expecting it to loop repeatedly, until the LED is at maximum or minimum brightness. This won’t happen unless you paw something like a for loop.

But, a for loop which contains a delay will probably cause a disconnection from Blynk, unless a Blynk.run is included in the loop.

Pete.

Hey Pete,
Thank you for your reply.
I don’t want to take the mickey, but how would I do this in my project script? Its just my first time using Blynk.

Thanks

This isn’t a Blynk related issue, it’s basic C++ programming. Do a bit of Googling and you’ll learn how for loops work. Then, read what I wrote above and create a for loop within your BLYNK_WRITE functions.

Pete.

Alternatively remove all the led functuoning code from the BLYNK_WRITE leave only the pinvalue=psram.asInt. Create a separate void function with all your led stuff which reads the state of the BLYNK_WRITE, include the blynk timer and the in your setup create a timer which calls your new void function at an appropriate interval

Hey,
Thank you for your help proietti.
I finally got it sorted, I went with the for loops suggested by PeteKnight.
Below is the code I went for and it is working a treat!

#define BLYNK_USE_DIRECT_CONNECT

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleSerialBLE.h>

char auth[] = "xxxxxx";

int LEDEyes = 9;        // Arduino Pin for LED Eyes
int FadeBlink;          // Int - Fade Blink
int Blink;              // Int - Blink
int TrueBrightness;     // Int - True Brightness of LED Eyes (0-255)
int Brightness;         // Int - True Brightness converted to percentage (0-100%)
int FadeAmount;         // Int - How long to fade the LED Eyes by
int Power;              // Int - Power On/Off button

void setup()
{
  DebugSerial.begin(9600);

  DebugSerial.println("Waiting for connections...");
  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  pinMode(LEDEyes, OUTPUT);
}

void loop()

{
  Brightness = map(TrueBrightness,0,100,0,255);   // Convert True LED Brightness (0-255) to percentage (0-100%)
  Blynk.run();
}

BLYNK_WRITE(V2)   // Virtual Button (V2) - True Brightness iof LED Eyes
{
  TrueBrightness = param.asInt();
  {
  analogWrite(LEDEyes, Brightness);
}}

BLYNK_WRITE(V3)   // Virtual Button (V3) - How long to fade the LED Eyes by
{
  FadeAmount = param.asInt();
}

BLYNK_WRITE(V4)   // Virtual Button (V4) - Blink
{
  Blink = param.asInt();
  if (Blink == 0) {
  for (int i = -1; i<Brightness; i++)
  {
      analogWrite(LEDEyes, i);
   }}
   if (Blink == 1)
   {
    for (int i = Brightness; i>-1; i--)
    {
      analogWrite(LEDEyes, i);
}
}}

BLYNK_WRITE(V5)   // Virtual Button (V5) - Power On/Off button
{
  Power = param.asInt();
  if (Power == 0) {
  for (int i = -1; i<Brightness; i++)
  {
      analogWrite(LEDEyes, i);
   }}
   if (Power == 1)
   {
    for (int i = Brightness; i>-1; i--)
    {
      analogWrite(LEDEyes, i);
}
}}

BLYNK_WRITE(V1)   // Virtual Button (V1) - Fade Blink
{
  FadeBlink = param.asInt();
  if (FadeBlink == 0) {
  for (int i = 0; i<Brightness; i++)
  {
      analogWrite(LEDEyes, i);
      delay(FadeAmount);
   }}
   if (FadeBlink == 1)
   {
    for (int i = Brightness; i>-1; i--)
    {
      analogWrite(LEDEyes, i);
      delay(FadeAmount);
}
  }  }

Thank you for all your help!

Ben

1 Like