SimpleTimer LED Dimmer for Standby Mode - Intermittent strobing

I have a dimmer function tied to a virtual pin to indicate “standby.” The dimming function works well but–seemingly–randomly the dimmer will flicker instead. It seems like it is tied to when the input is actuated and when the input enters the cyclic.

Here is a link to some video. At second 27 the function works properly and the LED dims smoothly. The rest of the time it flickers.

https://goo.gl/photos/7uDBveRj7N178uGj8

Here’s the code.

/**************************************************************
  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Social networks:            http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

**************************************************************

  This example shows how to use DFRobot Bluno or BLE Link module
  to connect your project to Blynk.

  Please be sure to update your DFRobot firmware to at least V1.97:
    https://github.com/DFRobot/BLE_firmware_V1.9

  For Bluno, read instructions here:
    https://www.dfrobot.com/wiki/index.php/Bluno_SKU:DFR0267

  For BLE-Link, read instructions here:
    https://www.dfrobot.com/wiki/index.php/BLE-Link_(SKU:TEL0073)

  NOTE: BLE support is in beta!

**************************************************************/

#define BLYNK_USE_DIRECT_CONNECT

#include <BlynkSimpleSerialBLE.h>
#include <max6675.h>
#include <SimpleTimer.h>

// Blynk auth token
char auth[] = "#######################################3";

// --- globals ---

int sliderVal;
int tempVal;
int LEDstate;
int standby = 1;
int tempBias = -5;
int brightness = 0;
int fadeAmount = 1;
int tempSwing = 5;
int fadeDelay = 20;  // ms
int tempFreq = 1000; // ms

const int relayPin = 4;   // relay output
const int standbyLED = 5; // PWM pin for LED

SimpleTimer timer;

// Attach virtual pins to widgets

WidgetTerminal terminal(V2);
WidgetLED led1(V0);


// --- TC reading ---

int ktcSO = 8;
int ktcCS = 9;
int ktcCLK = 10;

MAX6675 ktc(ktcCLK, ktcCS, ktcSO);

// --- standby dimmer ---

void dimmer()
{
  if (standby == HIGH)
  {

    // set the brightness of the standby pin
    analogWrite(standbyLED, brightness);

    // reverse the direction of the fading at the ends of the fade:
    if (brightness <= 0 || brightness >= 255) {
      fadeAmount = -fadeAmount;
    }
  } else if (standby == LOW) { // turn on the LED full
    brightness = 255;
    analogWrite(standbyLED, brightness);
  }

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;
}
// --- heater control ---

void blkCtrl()
{
  tempVal = ktc.readFarenheit() + tempBias;

  if (standby == HIGH)
  {
    digitalWrite(relayPin, LOW);
    led1.off();
  } else if (standby == LOW) {
    if (tempVal < (sliderVal - tempSwing))
    {
      digitalWrite(relayPin, HIGH);
      led1.on();
    } else if (tempVal > (sliderVal + tempSwing)) {
      digitalWrite(relayPin, LOW);
      led1.off();
    }
  }
}

// --- serial terminal ---

// You can send commands from Terminal to your hardware. Just use
// the same Virtual Pin as your Terminal Widget

BLYNK_WRITE(V2)
{
  LEDstate = digitalRead(standbyLED);

  terminal.print("You said:");
  terminal.write(param.getBuffer(), param.getLength());
  terminal.println();
  terminal.print("Temperature is: ");
  terminal.println(tempVal);
  terminal.print("Slider value is: ");
  terminal.println(sliderVal);
  terminal.print("Sdby = ");
  terminal.println(standby);
  terminal.print("LED Pin = ");
  terminal.println(LEDstate);

  // Ensure everything is sent
  terminal.flush();
}

BLYNK_WRITE(V3) // Blynk app slider value
{
  sliderVal = param.asInt(); // assigning incoming value from pin V3 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
}

BLYNK_WRITE(V4)
{
  standby = param.asInt(); // assigning incoming value from pin V4 to a variable
}

//BLYNK_WRITE(V5) {
//}

BLYNK_READ(V6) //Blynk app uses V6 for thermocouple data
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V6, tempVal);
}

void setup()
{
  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(115200);
  Blynk.begin(Serial, auth);

  // wait for MAX chip to stabilize
  delay(500);

  pinMode(relayPin, OUTPUT);
  pinMode(standbyLED, OUTPUT);

  // This will print Blynk Software version to the Terminal Widget when
  // your hardware gets connected to Blynk Server
  terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
  terminal.println(F("-------------"));
  terminal.flush();

  // temp polling timer interval
  timer.setInterval(tempFreq, blkCtrl);
  // LED dimmer
  timer.setInterval(fadeDelay, dimmer);

  // multiple timers can be set by calling another function
  // timer.setInterval(#####, someFunction);
}

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

I see you are using BLE or Bluetooth… As shown in the sketch notes, it is in BETA and the developers are still working on it.

I have seen similar “glitches” with my various BLE/BT experiments, sometimes it seems rock solid, other times strange things like delayed reactions, App <–> Hardware communication stuttering (probably what you are experiencing) and even seemingly one way operation (all displays show incoming data, but no response to outgoing commands)… as I said… BETA :wink:

Basically, let us know the particulars like app version, phone model, and BLE/BT module type, etc and await the developers feedback.

Thanks.

PS, I formatted your code for proper display… please use this method for future:

Sorry, I know better.

  • It is a Bluno V2.0 board. I’m not sure how to determine exact firmware version, at the moment. I have not updated the firmware out of the box. I may try that.
  • Blynk 2.13.1
  • I’ve run the code on a Nexus 5, Android 6.0.1; LG-D2, Android 5.0.2
  • Tried running on two iOS phones. The app would connect to the BLE device but trying to run the Blynk project would flag not connected to device. (I understand at the time of writing this that there is an iOS update pending Apple review)

Can do on the formatting of the code. I looked for the protocol but missed it.

Thanks!

OK, I tried to read through your code, and even loaded it up…

I can duplicate the issue and it appears to have nothing to do with the BLE interface or probably even Blynk itself, rather it appears to be how you are using SimpleTimer to process the fading… particularly when it starts up the first time.

I broke it down to just the basic fade routine (which I was also able to test with my connection settings and see the strobing issue) and essential supporting commands (NOTE - this will not compile as is) so that someone else more familiar with SimpleTimer might actually look it over :wink:

PS I also tweaked your title to better relate to the issue.

// --- globals ---
int standby = 1;
int brightness = 0;
int fadeAmount = 1;
int fadeDelay = 20;  // ms
const int standbyLED = 5; // PWM pin for LED

void dimmer()
{
  if (standby == HIGH)
  {
    // set the brightness of the standby LED
    analogWrite(standbyLED, brightness);
    // reverse the direction of the fading at the ends of the fade:
    if (brightness <= 0 || brightness >= 255) {
      fadeAmount = -fadeAmount;
    }
  } else if (standby == LOW) {  // turn on the LED On
    brightness = 255;
    analogWrite(standbyLED, brightness);
  }
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;
}

BLYNK_WRITE(V4)  // Button Widget in Switch mode
{
  standby = param.asInt();
}

void setup()
{
  timer.setInterval(fadeDelay, dimmer);
}

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

Wow, thanks Gunner.

I wired in a physical switch and the same blinking happened. I assume that SimpleTimer runs natively and not through the Blynk service so that points to hardware, SimpleTimer, or poor programming skills.

There is new Bluno firmware to install but I was having problems with the installer and Ubuntu so I’ll work on that. I also saw that the latest Blynk libraries (v0.4.7) have replaced SimpleTimer. I’ll give that a try.

Thanks again!