1

This is my setup:

  • Arduino Mega;
  • HM-10 bluetooth module (bought off ebay, so not sure if genuine but works);
  • Adafruit Dotstar addressable LED strip;
  • iOS Blynk app;

Hardware-wise, everything is connected and working properly.

My ultimate goal is to implement this LED strip on my car in the headlights.

At this point though, I want to have everything working off the Blynk app with 3 on/off switch buttons, linked to 3 virtual pins and not consider any code priority for the lights.

I have 3 light "shows" for the strip, which are in their own void functions.

These are:

  • Daytime runing lights: DRL();
  • Signal turn indicator: STI();
  • Police display: POL();

This is my first serious project so consider me a first time coder. Each switch button should be linked to each light show and turn the strip on and off.

What I want more specifically: when I press the DRL switch, DRL should stay on. Only turn off when switch is pressed again. This works fine.

The STI I want to loop. but when I push the switch, it goes through the function only once. This is what I don't understand, as the Blynk.run() is in the void loop function, and the state of the virtual pin hasn't changed.

It's the same for the police lights, it goes through the sequence only once, but i want it looped.

So my question is: How can I loop the STI and POL functions in my code?

I have tried with a while function but i can't get out of it by reading the the virtual pin within the function.

Here is the entire code:

I am aware that the code might be messy, any input is appreciated.

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <BlynkSimpleSerialBLE.h>

#include <Adafruit_DotStar.h>
#define NUMPIXELS 72

#include <SoftwareSerial.h>

Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DOTSTAR_BRG);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "***";

uint32_t amber = 0x020600;
uint32_t white = 0x020202;
uint32_t red = 0x000200;
uint32_t blue = 0x000002;
//SoftwareSerial SerialBLE(10, 11); // RX, TX


BLYNK_WRITE(V1)
{
  int pinValue1 = param.asInt(); // assigning incoming value from pin V1     to a variable

  if (pinValue1 == 1) {
    Serial.print("Daytime running lights: ON ");
    Serial.println(pinValue1); //showing the actual virtual pin status 0/1
    DRL();
  }
  else {
    Serial.print("Daytime running lights: OFF ");
    Serial.println(pinValue1); //showing the actual virtual pin status 0/1
    OFF();
    Serial.print("off ok"); // this just helps me check where the code has   progressed
  }
}

BLYNK_WRITE(V2)
{
  int pinValue2 = param.asInt(); // assigning incoming value from pin V2 to    a variable

  if (pinValue2 == 1) {
    Serial.print("Dynamic turn indicator: ON ");
    Serial.println(pinValue2);  //showing the actual virtual pin status 0/1
    STI();
  }
  else {
    Serial.print("Dynamic turn indicator: OFF ");
    Serial.println(pinValue2);  //showing the actual virtual pin status 0/1
    OFF();
    Serial.print("off ok"); // this just helps me check where the code has  progressed
  }
}

BLYNK_WRITE(V3)
{
  int pinValue3 = param.asInt(); // assigning incoming value from pin V3 to a variable

  if (pinValue3 == 1) {
    Serial.print("Police Lights: ON ");
    Serial.println(pinValue3);
    POL();
  }
  else {
    Serial.print("Police Lights: OFF ");
    Serial.println(pinValue3);
    OFF();
    Serial.print("off ok"); // this just helps me check where the code has progressed
  }
}

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Blynk.begin(Serial1, auth);
  Serial.println("Waiting for connections...");

  strip.begin();  //initializes the strip
  strip.show();  //turn off LEDs within NUMPIXELS
}

void OFF() {
  for (int i; i <= 72; i++) { //turn off strip
    strip.setPixelColor(i, 0);
    strip.show();
  }
}

void DRL() {
  for (int i = 0; i <= 72; i++) {
    strip.setPixelColor(i, white);
    strip.show();
    delay(4);
  }
}

void STI() {
  //turn on dinamic indicator
  for (int i = 0; i <= 72; i++) {
    strip.setPixelColor(i, amber);
    strip.show();
    delay(2);
  }

  //turn off dinamic indicator
  for (int i; i <= 72; i++) {
    strip.setPixelColor(i, 0);
  }
  delay (200);  //on condition delay
  strip.show();
  delay (500);  //off condition delay
}

void POL() {

  for (int i = 0; i <= 36; i++) {
    strip.setPixelColor(i, red);
    strip.show();
  }
  delay(30);

  for (int i = 0; i <= 36; i++) {
    strip.setPixelColor(i, 0);
    strip.show();
  }
  delay(30);

  for (int i = 0; i <= 36; i++) {
    strip.setPixelColor(i, red);
    strip.show();
  }
  delay(30);

  for (int i = 0; i <= 36; i++) {
    strip.setPixelColor(i, 0);
    strip.show();
  }
  delay(30);

  for (int i = 0; i <= 36; i++) {
    strip.setPixelColor(i, red);
    strip.show();
  }
  delay(30);

  for (int i = 0; i <= 36; i++) {
    strip.setPixelColor(i, 0);
    strip.show();
  }
  delay(30);

  for (int i = 0; i <= 36; i++) {
    strip.setPixelColor(i, red);
    strip.show();
  }
  delay(30);

  for (int i = 0; i <= 36; i++) {
    strip.setPixelColor(i, 0);
    strip.show();
  }
  delay(100);

  for (int i = 37; i <= 72; i++) {
    strip.setPixelColor(i, blue);
    strip.show();
  }
  delay(30);

  for (int i = 37; i <= 72; i++) {
    strip.setPixelColor(i, 0);
    strip.show();
  }
  delay(30);

  for (int i = 37; i <= 72; i++) {
    strip.setPixelColor(i, blue);
    strip.show();
  }
  delay(30);

  for (int i = 37; i <= 72; i++) {
    strip.setPixelColor(i, 0);
    strip.show();
  }
  delay(30);

  for (int i = 37; i <= 72; i++) {
    strip.setPixelColor(i, blue);
    strip.show();
  }
  delay(30);

  for (int i = 37; i <= 72; i++) {
    strip.setPixelColor(i, 0);
    strip.show();
  }
  delay(30);

  for (int i = 37; i <= 72; i++) {
    strip.setPixelColor(i, blue);
    strip.show();
  }
  delay(30);

  for (int i = 37; i <= 72; i++) {
    strip.setPixelColor(i, 0);
    strip.show();
  }
  delay(100);
}

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

1 Answer 1

1

The way the BLYNK_WRITE functions works is that they are called every time device gets an update of Virtual Pin value from the app. Now if you want to run something on a timer, or in a loop there is a function called BlynkTimer. You write your own function that you want to be looped and use this feature to define how often it should repeat. You can control execution by setting a boolean flag in the BLYNK_WRITE function and checking it in the loop, or by running the enable() and disable() methods of the timer. So, you can do something like this (without libraries, just an example):

BlynkTimer timer; // create timer

void myTimer() 
{
    // toggle an LED
    static bool state;
    digitalWrite(13, state);
    state = !state;
}

void setup()
{
  Blynk.begin(details); // blynk start
  timer.setInterval(1000L, myTimer); // repeat the timer once a second
  pinMode(13, OUTPUT); // LED pin as output
}

void loop()
{
  Blynk.run(); // Runs all Blynk stuff
  timer.run(); // runs BlynkTimer
}

You can read more about it in the docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.