Stroboscope with virtual pins

Hi there,

Can you please help me with the stroboscope script?
What if I want to control two different functions with two different buttons with the strobe script.

For example I want V1 & V2 to control D3, & and V3 & V4 to control D4?

I have only been able to control both D3 & D4 at the same time with either V1 or V2.

Thanks

Hello. Please post your code and explain what you want to achieve with it so we could help. It is not very clear for me from description.

Have you tried looking at http://docs.blynk.cc/#blynk-main-operations-virtual-pins

Yes, of course.

I hope it is maybe somewhat clearer now.

Code:

#define LED_PIN1 3
#define LED_PIN2 4

SimpleTimer timer;
int t1;

// Enable/disable blinking using virtual pin 1
BLYNK_WRITE(V1)
{
  if (param.asInt()) {
    timer.enable(t1);
  } else {
    timer.disable(t1);
    digitalWrite(LED_PIN1, LOW);
  }
}
// Change blink interval using virtual pin 2
BLYNK_WRITE(V2)
{
  long interval = param.asLong();
  boolean wasEnabled = timer.isEnabled(t1);
  timer.deleteTimer(t1);
  t1 = timer.setInterval(interval, ledBlynk);
  if (!wasEnabled) {
    timer.disable(t1);
  }
}

BLYNK_WRITE(V3)
{
  if (param.asInt()) {
    timer.enable(t1);
  } else {
    timer.disable(t1);
    digitalWrite(LED_PIN2, LOW);
  }
}

BLYNK_WRITE(V4)
{
  long interval = param.asLong();
  boolean wasEnabled = timer.isEnabled(t1);
  timer.deleteTimer(t1);
  t1 = timer.setInterval(interval, ledBlynk);
  if (!wasEnabled) {
    timer.disable(t1);
  }
}

// Toggle LED
void ledBlynk()
{
  digitalWrite(LED_PIN1, !digitalRead(LED_PIN1));
  
}

void setup()
{
  // Configure Blynk
  Serial.begin(9600);
  Blynk.begin(auth);

  // Configure LED and timer
  pinMode(LED_PIN1, OUTPUT);
  t1 = timer.setInterval(500L, ledBlynk);
  timer.disable(t1);
  
  
  pinMode(LED_PIN2, OUTPUT);
  t1 = timer.setInterval(500L, ledBlynk);
  timer.disable(t1);
}




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

@Erik_Hagstrom are you planning to use any Blynk libraries in your sketch?

Here is the libraries that are included in the sketch.

[quote]// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h> //SoftwareSerial.h
SoftwareSerial SwSerial(2, 3); // RX, TX
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h> //BlynkSimpleSerial.h
#include <SimpleTimer.h> //SimpleTimer.h
[/quote]

invisible libraries, don’t you just hate them.

I don’t really know why they didn’t pop up, I added the names to them with // in front.

set a global variable of:

int t2;

and then change your V3 and V4 to use t2.

please learn how to format your sketches as it annoys the hell out of me to see such rubbish.

Study this until you can do it in your sleep Blynk.begin seems to change my long values. without Blynk.begin it work ok

1 Like

Code snippets should be formatted. Please edit your initial post:

How to do that:


 ``` cpp <--put 3 backticks BEFORE your code starts  ("cpp" means C++ language) 

   //Put your code here
   //..................
   //..................

 ``` <--insert 3 backticks AFTER your code

**This makes your code readable and with highlighted syntax, like this

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

//comment goes here 
void helloWorld() { 
   String message =  "hello" + "world"; 
}

Thanks,
Unfortunately I still have the same problem with V1 and V2 still triggers both LED_PIN1 and LED_PIN2, the same goes with V3 and V4.

// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3); // RX, TX
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>
#include <SimpleTimer.h>

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



#define LED_PIN1 3
#define LED_PIN2 4

SimpleTimer timer;
int t1;
int t2;

// Enable/disable blinking using virtual pin 1
BLYNK_WRITE(V1)
{
  if (param.asInt()) {
    timer.enable(t1);
  } else {
    timer.disable(t1);
    digitalWrite(LED_PIN1, LOW);
  }
}
// Change blink interval using virtual pin 2
BLYNK_WRITE(V2)
{
  long interval = param.asLong();
  boolean wasEnabled = timer.isEnabled(t1);
  timer.deleteTimer(t1);
  t1 = timer.setInterval(interval, ledBlynk);
  if (!wasEnabled) {
    timer.disable(t1);
  }
}

BLYNK_WRITE(V3)
{
  if (param.asInt()) {
    timer.enable(t2);
  } else {
    timer.disable(t2);
    digitalWrite(LED_PIN2, LOW);
  }
}

BLYNK_WRITE(V4)
{
  long interval = param.asLong();
  boolean wasEnabled = timer.isEnabled(t2);
  timer.deleteTimer(t2);
  t2 = timer.setInterval(interval, ledBlynk);
  if (!wasEnabled) {
    timer.disable(t2);
  }
}

// Toggle LED
void ledBlynk()
{
  digitalWrite(LED_PIN1, !digitalRead(LED_PIN1));
  digitalWrite(LED_PIN2, !digitalRead(LED_PIN2));
}

void setup()
{
  // Configure Blynk
  Serial.begin(9600);
  Blynk.begin(auth);

  // Configure LED and timer
  pinMode(LED_PIN1, OUTPUT);
  t1 = timer.setInterval(500L, ledBlynk);
  timer.disable(t1);
  
  
  pinMode(LED_PIN2, OUTPUT);
  t2 = timer.setInterval(500L, ledBlynk);
  timer.disable(t2);
}

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

@Erik_Hagstrom take a look at the following sketch which is in ESP format rather than Arduino format. You just need to convert it back to Arduino for you to use. For the ESP I used LED_PIN1 and LED_PIN2 both as the onboard LED and it is active LOW.

// Stroboscope.ino mod by Costas 9/10/16
//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

char auth[] = "xxxxxxxxxxxxxxx";

#define LED_PIN1 2   // was 3 for Arduino
#define LED_PIN2 2   // was 4 for Arduino

SimpleTimer timer;
int t1;
int t2;

// Enable/disable blinking using virtual pin 1
BLYNK_WRITE(V1)
{
  if (param.asInt()) {
    timer.enable(t1);
  } else {
    timer.disable(t1);
    digitalWrite(LED_PIN1, HIGH);  // LED active LOW
  }
}
// Change blink interval using virtual pin 2
BLYNK_WRITE(V2)
{
  long interval = param.asLong();
  boolean wasEnabled = timer.isEnabled(t1);
  timer.deleteTimer(t1);
  t1 = timer.setInterval(interval, ledBlynk1);
  if (!wasEnabled) {
    timer.disable(t1);
  }
}

BLYNK_WRITE(V3)
{
  if (param.asInt()) {
    timer.enable(t2);
  } else {
    timer.disable(t2);
    digitalWrite(LED_PIN2, HIGH);  // LED active LOW
    //digitalWrite(LED_PIN1, HIGH);  // LED active LOW
  }
}

BLYNK_WRITE(V4)
{
  long interval = param.asLong();
  boolean wasEnabled = timer.isEnabled(t2);
  timer.deleteTimer(t2);
  t2 = timer.setInterval(interval, ledBlynk2);
  if (!wasEnabled) {
    timer.disable(t2);
  }
}

// Toggle LED_PIN1
void ledBlynk1()
{
  //digitalWrite(LED_PIN1, !digitalRead(LED_PIN1));
  digitalWrite(LED_PIN2, !digitalRead(LED_PIN2));
}

// Toggle LED_PIN2
void ledBlynk2()
{
  //digitalWrite(LED_PIN1, !digitalRead(LED_PIN1));
  digitalWrite(LED_PIN2, !digitalRead(LED_PIN2));
}

void setup()
{
  Serial.begin(115200);
  Serial.println("\nStarted");
  Blynk.begin(auth, "MTN WIFI 19996", "xxxxxx", "192.168.10.229", 8442);
  int mytimeout = millis() / 1000;
  while (Blynk.connect() == false) { 
    if((millis() / 1000) > mytimeout + 4){ // try for less than 5 seconds
      break;
    }
  }
  if(Blynk.connected()){
    Serial.println("Connected to Blynk");
  }
  else{
    Serial.println("Failed to connect to Blynk");
  }  

  // Configure LED and timer
  pinMode(LED_PIN1, OUTPUT);
  digitalWrite(LED_PIN1, HIGH);  // on board LED off
  t1 = timer.setInterval(500L, ledBlynk1);
  timer.disable(t1);
  
  
  pinMode(LED_PIN2, OUTPUT);
  digitalWrite(LED_PIN2, HIGH);  // on board LED off
  t2 = timer.setInterval(500L, ledBlynk2);
  timer.disable(t2);
}

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

Thanks a lot, after some tweaking I finally got it to work just the way I want it.
Many many thanks @Costas

show us the tweaks!

@Dave1829 Absolutely! Here’s the sketch I’m using at the moment.

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3); // RX, TX
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>
#include <SimpleTimer.h>

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



#define LED_PIN1 3
#define LED_PIN2 4

SimpleTimer timer;
int t1;
int t2;

// Enable/disable blinking using virtual pin 1
BLYNK_WRITE(V1)
{
  if (param.asInt()) {
    timer.enable(t1);
  } else {
    timer.disable(t1);
    digitalWrite(LED_PIN1, LOW);
  }
}
// Change blink interval using virtual pin 2
BLYNK_WRITE(V2)
{
  long interval = param.asLong();
  boolean wasEnabled = timer.isEnabled(t1);
  timer.deleteTimer(t1);
  t1 = timer.setInterval(interval, ledBlynk);
  if (!wasEnabled) {
    timer.disable(t1);
  }
}

BLYNK_WRITE(V3)
{
  if (param.asInt()) {
    timer.enable(t2);
  } else {
    timer.disable(t2);
    digitalWrite(LED_PIN2, LOW);
  }
}

BLYNK_WRITE(V4)
{
  long interval = param.asLong();
  boolean wasEnabled = timer.isEnabled(t2);
  timer.deleteTimer(t2);
  t2 = timer.setInterval(interval, ledBlynk);
  if (!wasEnabled) {
    timer.disable(t2);
  }
}

// Toggle LED
void ledBlynk()
{
  digitalWrite(LED_PIN1, !digitalRead(LED_PIN1));
  digitalWrite(LED_PIN2, !digitalRead(LED_PIN2));
}

void setup()
{
  // Configure Blynk
  Serial.begin(9600);
  Blynk.begin(auth);

  // Configure LED and timer
  pinMode(LED_PIN1, OUTPUT);
  t1 = timer.setInterval(500L, ledBlynk);
  timer.disable(t1);
  
  
  pinMode(LED_PIN2, OUTPUT);
  t2 = timer.setInterval(500L, ledBlynk);
  timer.disable(t2);
}




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