Is this possible with Blynk?

I have simple project using Arduino Nano and ENC28J60 using just buttons to send commands to external relays (that controls electronic of the gate). Is it possible to do with the blynk the following?

  1. Using button widget. By press send send (push) to D4 (manual up) and after analogue sensor (potentiometre) reaches desired value arduino should automatically send signal to D5 (manual stop)

  2. Using button widget. By press send signal (push) to D6 and after predefined period of time (eg 45s) send signal to D7. It would be great if it would be possible to easily change this time within application

  3. Using potentiometre and data from analogue sensor (pottentiometer). I can sellect desired position on blynk app with potentiometre and then arduino should send signal to D4 (manual UP) if value on analogue is below desired and then stop when it is reached or D3 (manual DOWN if value is abowe

  4. Schedule. Something like timer, but I need weekly calendar or even better real calendar (I need to send signal every second friday at 7.00 but it will be ok also if I send it every friday)

  5. Logging any change od 2 digital switches

1 Like

All is possible except point 3. Someday we will do that too =).

1 Like

3. is done with RTC Widget and a bit of code:

is it friday and 7am then do x.

If you want to change a schedule without changing the code just add terminal widget, and a bit more basic code, to take the new time and day.

1 Like

Can rtc widget recognize also days, not only hours? I was thinking about it but I thought it shows only hours. Btw, should it show anything in my application? I have it enabled, but it shows only symbol…

It is great to hear that every is possible. Can anyone please help me how?

1 Like

@monaco yes the RTC widget uses the time library which has a function called weekday().

I believe it is Paul Stoffregen’s library that they use and the first day of the week is Sunday and weekday() returns 0 (some time libraries had Sunday as the first day of the week but it was day 1).

Paul’s library and lots of his examples are at https://github.com/PaulStoffregen/Time

So for you something like this:

if(weekday() == 5){
   // do something
}

As you want the action every second Friday you can use virtualWrite to a Value Display to count for each second Friday.

Read http://docs.blynk.cc/ and look through the examples and then read it again.

Take a look at the projects created by other Blynkers at http://community.blynk.cc/c/projects and study the sketches they have provided. If you still have problems post full details of what you have done, what problem you have and all the equipment you are using as a follow up to this post.

I understand that I should use virtual pins for 1 and 2. I have also read docs in detail, but it seems that I do not have basic programming skills and I do not know how to start. I guess I will need to check projects once more to find someone who did something similar. There are no instructions for dummies for virtual pins as they are to start using Blynk :slight_smile:

I am building a smart gardening system which controls 4 relays according to sensor info.

If you like, I’d be happy to help modify my code to suit your needs.

I am using an UNO as the brains and an ESP to send/receive data from Blynk via SoftSerial so there will be quite a bit of code that can come out.

Thanks for all answers. I have problems with basic coding. I have spent a few evenings studying different tutorials but at the end it turned out that I do not understand half of the lines they used. Can anyone suggest good source/tutorial for starters. Or if anyone could write me the code for any of my examples with explanation so I can do the rest.

I know I should use virtual pins and I know the logic is

BLYNK_WRITE(V0) {
int SomeVariable = param.asInt();
if (SomeVariable == 1) {
// do something
}
else{
// do something different
}
}

But I have no idea how to write it for my problems:
PROBLEM 1
-send D4 (press button to trigger movement)
-if A3 is 900 (value from potentiometre - see problem below) send D5 (press button for manual stop.

PROBLEM2
-send D6
-after 45s send D7

PROBLEM3

  • if A3 < value from potentiometre, send signal to D4
  • if A3 > value from potentiometre, send signal to D3
  • if A3 = value from potentiometre, send signal to D5

PROBLEM4
basic code then could be

BLYNK_WRITE(V1)
if(weekday() == 5)
hour()==7
minute()==00
{
digitalWrite(4, LOW)
}
else
{
digitalWrite(4, HIGH)
}
if(weekday() == 5)
hour()==9
minute()==00
{
digitalWrite(5, LOW)
}
else
{
digitalWrite(5, HIGH)
}

}

PROBLEM5
i have no idea

I found out that I can even not use simple led widget. I would like to get led turned on if D8 is connected to ground. I use also virtual pin 7.

So my sketch should contain:

BLYNK_WRITE(V8)

void checkPin()
{
if (digitalRead(8))
{
led1.off();

} else {
led1.on();
}
}

void loop()
{
checkPin();

Do I need to add also something to void setup() or init or to add anything else into sketch?

Maybe stupid question… do I need to do all this programming via board connected to usb or can I do it remote via ethernet and maybe terminal?

Another problem are values from analog potentiometre connected to A3. For testing purposes I use Gauge or Value display. The problem is that everything is too sensitive. Values are from 0 to 1023 and the values are constantly changing even if hardware is still. How to reduce sensitivity? For me it would be enoug to have 10 or 20 times lower sensitivity. If the value is anywhere from 0-10 or 20 it should say 0 for example.

What youre asking for is quite a lot.
Youre code isnt going to be simple thats for sure.
I will post my smart gardening system which as similar logic to what youre asking.

#include <elapsedMillis.h>
#include <SoftwareSerial.h>
#include "DHT.h"
#include <SimpleTimer.h>
////////////////////////////////////////////////////////////
SoftwareSerial SerialTxWIFI(4, 5); // RX, TX // MAKE CONNECTION TO WIFI MODUALE
elapsedMillis timer1;
SimpleTimer timer_relayStates;
SimpleTimer timer_sendTemp;
SimpleTimer timer_sendHumid;
SimpleTimer timer_waterUsed;
SimpleTimer timer_SensorVals;
////////////////////////////////////////////////////////////
#define MS1 A0         // Moisture Sesnor 1
#define MS2 A1         // Moisture Sesnor 1
#define MS3 A2         // Moisture Sesnor 1
#define DHTPIN 7       // DHT Sensor
#define DHTTYPE DHT22  // DHT 22
#define RELAY1 11      // RELAY1
#define RELAY2 10      // RELAY2
#define RELAY3 9       // RELAY3
#define RELAY4 8       // RELAY4
#define ON LOW
#define OFF HIGH

////////////////////////////////////////////////////////////
DHT dht(DHTPIN, DHTTYPE);
////////////////////////////////////////////////////////////
const int buttonPin = 12;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
////////////////////////////////////////////////////////////
// Flow Meter Interrupt
byte sensorInterrupt = 0;  // 0 = digital pin 2
byte sensorPin       = 2;
// The hall-effect flow sensor outputs approximately 4.5 pulses per second per litre/minute of flow.
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
////////////////////////////////////////////////////////////
boolean ButtonCurState =    LOW;
boolean ButtonLastState =   LOW;
int ManualMode;
////////////////////////////////////////////////////////////
String postData;
String sendData;
String inData;
////////////////////////////////////////////////////////////
// Relay States
boolean RelayOneStatePrev;
boolean RelayTwoStatePrev;
boolean RelayThreeStatePrev;
boolean RelayFourStatePrev;
int RelayOneState;
int RelayTwoState;
int RelayThreeState;
int RelayFourState;

float AirTempPrev;
float AirHumidityPrev;

int SoilMoisturePercentageZone1Prev;
int SoilMoisturePercentageZone2Prev;
int SoilMoisturePercentageZone3Prev;

int moistureThreshold;
//////////////////////////////////////////////////////////////////////////////////////////
/////// SETUP ///////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
void setup() {

  // PinMode
  pinMode(buttonPin, OUTPUT);
  pinMode(ledPin,    OUTPUT);
  pinMode(sensorPin, INPUT );
  pinMode(RELAY1,    OUTPUT);
  pinMode(RELAY2,    OUTPUT);
  pinMode(RELAY3,    OUTPUT);
  pinMode(RELAY4,    OUTPUT);

  // Set Relays Off
  digitalWrite(RELAY1, OFF);
  digitalWrite(RELAY2, OFF);
  digitalWrite(RELAY3, OFF);
  digitalWrite(RELAY4, OFF);

  RelayOneStatePrev = 0;
  RelayTwoStatePrev = 0;
  RelayThreeStatePrev = 0;
  RelayFourStatePrev = 0;

  AirTempPrev = 0.00;
  AirHumidityPrev = 0.00;

  SoilMoisturePercentageZone1Prev = 0;
  SoilMoisturePercentageZone2Prev = 0;
  SoilMoisturePercentageZone3Prev = 0;

  moistureThreshold = 250;

  ManualMode = 0;

  timer_relayStates.setInterval(500L, sendRelayStates);
  timer_sendTemp.setInterval(15000L, sendTemp);
  timer_sendHumid.setInterval(30000L, sendHumid);
  timer_waterUsed.setInterval(10000L, sendWaterUsed);
  timer_SensorVals.setInterval(1000L, sendSensorVals);

  // Set timers to 0
  timer1 = 0;

  // Set up Flow Meter
  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;

  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

  // Set up Serial
  Serial.begin(57600);
  Serial.println("Garduino Initialising...");
  Serial.flush();

  // Set up SoftwareSerial
  SerialTxWIFI.begin(57600);

  dht.begin();

  // 1 second delay to make sure all is set up
  delay(1000);

  Serial.println("Updating Blynk!");
  Serial.flush();

  delay(250);

  Serial.print("Temp:  ");
  sendToBlynk((int)1, GetAirTemp() , 1, 0);

  delay(250);

  Serial.print("Humidity:  ");
  sendToBlynk((int)2, GetAirHumidity()  , 1, 0);

  delay(250);

  Serial.print("Relay1:  ");
  sendToBlynk((int)7, RelayOneState , 0, 1);

  delay(250);

  Serial.print("Relay2:  ");
  sendToBlynk((int)8, RelayTwoState , 0, 1);

  delay(250);

  Serial.print("Relay3:  ");
  sendToBlynk((int)9, RelayThreeState , 0, 1);

  delay(250);

  Serial.print("Relay4:  ");
  sendToBlynk((int)11, RelayFourState , 0, 1);

  // All Set Up
  Serial.println("Ready!");
  Serial.flush();
  digitalWrite(ledPin, LOW);

}


// Button Setup 


void manualButtonTS() {
  ButtonCurState = digitalRead(buttonPin);
  if (ButtonCurState == HIGH && ButtonLastState == LOW) {
    // turn LED on:
    //int LedState = digitalRead(ledPin); // Set Status Of led
    //digitalWrite(ledPin, !LedState);
    // was going to set this button to turn off the 3 garden relays and turn on the 4th relay (lawn sprinklers) for x min depending on the temp and humidity
    if (ManualMode == 0) {
      manualOverride(1);
    } else if (ManualMode == 1) {
      manualOverride(0);
    }
    delay(10);
  }
  ButtonLastState = ButtonCurState;
}

void manualOverride(int state) {

  if (state == 1) {
    digitalWrite(RELAY4, ON);
    digitalWrite(RELAY1, OFF);
    digitalWrite(RELAY2, OFF);
    digitalWrite(RELAY3, OFF);
    RelayOneState    = 0;
    RelayTwoState    = 0;
    RelayThreeState  = 0;
    RelayFourState   = 1;
    ManualMode       = 1;
  } else if (state == 0) {
    digitalWrite(RELAY4, OFF);
    RelayFourState   = 0;
    ManualMode       = 0;
  }
}


/////// Blynk Timers 


void sendRelayStates() {

  if (RelayOneStatePrev != RelayOneState) {
    Serial.print("Relay1:  ");
    sendToBlynk((int)7, RelayOneState , 0, 1);
  }
  RelayOneStatePrev = RelayOneState;

  if (RelayTwoStatePrev != RelayTwoState) {
    Serial.print("Relay2:  ");
    sendToBlynk((int)8, RelayTwoState , 0, 1);
  }
  RelayTwoStatePrev = RelayTwoState;

  if (RelayThreeStatePrev != RelayThreeState) {
    Serial.print("Relay3:  ");
    sendToBlynk((int)9, RelayThreeState , 0, 1);
  }
  RelayThreeStatePrev = RelayThreeState;

  if (RelayFourStatePrev != RelayFourState) {
    Serial.print("Relay4:  ");
    sendToBlynk((int)11, RelayFourState , 0, 1);
  }
  RelayFourStatePrev = RelayFourState;
}

void sendTemp() {

  if (AirTempPrev != GetAirTemp()) {
    if (GetAirTemp() >= -10 && GetAirTemp() <= 50) {
      Serial.print("Temp:  ");
      sendToBlynk((int)1, GetAirTemp() , 1, 0);
    }
  }
  AirTempPrev = GetAirTemp();
}

void sendHumid() {
  if (AirHumidityPrev != GetAirHumidity()) {
    if (GetAirHumidity() >= 0 && GetAirHumidity() <= 100) {
      Serial.print("Humidity:  ");
      sendToBlynk((int)2, GetAirHumidity()  , 1, 0);
    }
  }
  AirHumidityPrev = GetAirHumidity();
}

void sendWaterUsed() {
  if (RelayOneState == 1 || RelayTwoState == 1 || RelayThreeState == 1 || RelayFourState == 1) {
    Serial.print("Water Usage:  ");
    sendToBlynk((int)3, (float)totalMilliLitres / 1000 , 1, 0);
  }
}

void sendSensorVals() {

  if (SoilMoisturePercentageZone1Prev != GetSoilMoisturePercentage(1)) {
    Serial.print("Z1 %:  ");
    sendToBlynk((int)4, GetSoilMoisturePercentage(1) , 0, 0);
  }
  SoilMoisturePercentageZone1Prev = GetSoilMoisturePercentage(1);

  if (SoilMoisturePercentageZone2Prev != GetSoilMoisturePercentage(2)) {
    Serial.print("Z2 %:  ");
    sendToBlynk((int)5, GetSoilMoisturePercentage(2) , 0, 0);
  }
  SoilMoisturePercentageZone2Prev = GetSoilMoisturePercentage(2);

  if (SoilMoisturePercentageZone3Prev != GetSoilMoisturePercentage(3)) {
    Serial.print("Z3 %:  ");
    sendToBlynk((int)6, GetSoilMoisturePercentage(3) , 0, 0);
  }
  SoilMoisturePercentageZone3Prev = GetSoilMoisturePercentage(3);

}

void sendToBlynk(int pin, double data, int type, int reMap) {
  // pin = Virtual Pin (ie 20 = V20)
  // data = raw data to send
  // Expected data type (0 = int, 1 = float)
  // reMap = map 0/1 to 0/1023 (default: off)

  postData = pin;
  postData += ',';

  switch (type) {
    case 0: // int
      postData += (int)data;
      break;
    case 1: // float
      postData += (float)data;
      break;
  }
  postData += ',';
  postData += (int)reMap;

  SerialTxWIFI.println(postData);
  SerialTxWIFI.flush();

  Serial.print("Sending:  ");
  Serial.println(postData);
  Serial.flush();

}



/////// Moisture Sensor Setup /////////////////////////////////////////////////////////////

int GetSoilMoistureRaw(int Zone) {
  if (Zone == 1) {
    return analogRead(MS1);
  } else if (Zone == 2) {
    return analogRead(MS2);
  } else if (Zone == 3) {
    return analogRead(MS3);
  }
}
/////////////
int GetSoilMoisturePercentage(int Zone) {
  int SoilMoisturePercentage = map( GetSoilMoistureRaw(Zone), 0, 1023, 0, 100 );
  return SoilMoisturePercentage;
}


/////// LOOP


void loop() { // run over and over

  // serial comms
  while (SerialTxWIFI.available() > 0) {
    char recieved = SerialTxWIFI.read();
    inData += recieved;
    if (recieved == '\n') {
      parseData(inData);
      inData = ""; // Clear recieved buffer
    }
  }
  while (Serial.available() > 0) {
    SerialTxWIFI.write(Serial.read());
  }




  manualButtonTS(); // Override everything with a manual button
  GetFlowSensorData(); // get flow data

  if (ManualMode == 0) {
    if (analogRead(MS1) < moistureThreshold) {
      digitalWrite(RELAY1, ON);
      RelayOneState = 1;
    } else {
      digitalWrite(RELAY1, OFF);
      RelayOneState = 0;
    }
    if (analogRead(MS2) < moistureThreshold) {
      digitalWrite(RELAY2, ON);
      RelayTwoState = 1;
    } else {
      digitalWrite(RELAY2, OFF);
      RelayTwoState = 0;
    }
    if (analogRead(MS3) < moistureThreshold) {
      digitalWrite(RELAY3, ON);
      RelayThreeState = 1;
    } else {
      digitalWrite(RELAY3, OFF);
      RelayThreeState = 0;
    }
  }

  timer_relayStates.run();
  timer_sendTemp.run();
  timer_sendHumid.run();
  timer_waterUsed.run();
  timer_SensorVals.run();
}


/////// Digital Humidity/Tempreture


float GetAirTemp() {
  return dht.readTemperature();
}
/////////////
float GetAirHumidity() {
  return dht.readHumidity();
}


/////// Flow Sensor 

void GetFlowSensorData() {
  if (timer1 > 1000) {
    timer1 -= 1000;

    detachInterrupt(sensorInterrupt);
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    oldTime = millis();
    flowMilliLitres = (flowRate / 60) * 1000;
    totalMilliLitres += flowMilliLitres;
    unsigned int frac;
    frac = (flowRate - int(flowRate)) * 10;
    pulseCount = 0;
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  }
}

void pulseCounter() {
  pulseCount++;
}


String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {
    0, -1
  };
  int maxIndex = data.length() - 1;
  for (int i = 0; i <= maxIndex && found <= index; i++) {
    if (data.charAt(i) == separator || i == maxIndex) {
      found++;
      strIndex[0] = strIndex[1] + 1;
      strIndex[1] = (i == maxIndex) ? i + 1 : i;
    }
  }
  return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

void parseData(String inData) {
  // expecting data format as String: 1,23
  String cmd     = getValue(inData, ',', 0);
  String data    = getValue(inData, ',', 1);
  String reMap   = getValue(inData, ',', 2);

  if (reMap.toInt() == 1) {
    data = map(data.toInt(), 0, 100, 0, 1023);
  }

  switch (cmd.toInt()) {
    case 1:
      // dooo ManualMode
      if (ManualMode == 0) {
        manualOverride(1);
        Serial.println("Cmd Rx: Manual Mode Enabled");
      } else if (ManualMode == 1) {
        manualOverride(0);
        Serial.println("Cmd Rx: Manual Mode Disabled");
      }
      break;
    case 2:
      moistureThreshold = data.toInt();
      Serial.print("Cmd Rx: Moisture Threshold");
      Serial.println(" | Data: " + data);
      break;
  }

  Serial.flush();

}


Simply remove all mentions of sendToBlynk() + the function and replace it with a Blynk.virtualWrite() function.

1 Like

I did not ask for doing it instead of me. I just put all my concerns into one post and that’s why it looks a lot. I would appreciate if anyone could give me answer to any single line or guestion :innocent:

You have much more logic and variables behind. I need a few commands with some quite simple automated tasks behind without any specific logic (if you read my “problems”). I really like your project, but it does not help me much…

What im saying is that my code has all the info you will need to sort out your problems.
Use it as a reference.

Move away from digital IO pins to Virtual Pins. Create a function for each specific Vpin using BLYNK_WRITE()
Use SimpleTimer and create a function in the loop that checks for a var (ie varButtonState). that can be set to 1 or HIGH when you press the button widget.
Then one you have set this gobal var, the loop will see it, and trigger a timer which will run for 45 seconds, turn off the timer, then trigger D7.
The code would look something like this (might have incorrect syntax as im on my phone)

BLYNK_WRITE(V6){
   varButtonStateCurr = param.asInt();
   if (varButtonStateCurr == 1 && varButtonStatePrev == 0) {
      // this will trigger only once using a push widget button
      varButtonState = 1;
      delay(10);
   } 
   varNextColourPrev = varNextColour;
   // to ensure it happens only once we reverse the button function and reset the Prev state var
   if (varButtonStateCurr == 0 && varButtonStatePrev == 1) {
      varNextColourPrev = 0;
   }
}

void loop(){
   checkButtonState();
}

int checkButtonState(){
   if(varButtonState == 1){
      int timer = 0; // reset your timer
      if(timer > 45000){
         digitalWrite(7, HIGH); // trigger D7
         varButtonState = 0;
      }
   }
}


Use RTC widget. The example is VERY easy to follow if you just merge if with your code. You can then use day() week() etc to determine which Friday of the month it is.

Just use Blynk.virtualWrite(V1,‘1’); during the button press once you have your code above working.

System is currently working with digital pins. But have only widgets for direct controlls for each pin (default blynk app and they will stay). Thats why I was thinking about using them in advance too. Should I keep there digital IO pins or should I also move to digital? (if so…why?)

Thanks for code. This is great for timer. What about for sending signal after desired value of analog is reached?

RTC did you mean that ? https://github.com/blynkkk/blynk-library/blob/master/examples/Widgets/RTC/RTC.ino

I need loging for any state change, not only within blynk (I will still keep existing rf remote controll), thats I intend to use signals from end position sensors.

@monaco yes that is Blynk’s sample sketch for the RTC widget.

I have thought about weekday() a few times since I made the post a couple of weeks ago.

I’m just going to knock up a “project” to check it works as expected.

Hi @monaco,
why would you like to implement #3 with blynk?
I tend to think that blynk gives me a nice interface to drive my hardware, but I see business logic usually implemented in the controller itself (in your case the arduino) and not in the nice interface (in this case blynk).

This is why I suggest you implement #3 in the code of your arduino :slight_smile:
From blynk you would change the day and the time, but the execution at set date/time belongs to the program in the arduino in my opinion.

Good luck with your nice project!
Gustavo.

@monaco I checked weekday() and despite what it states on the Time GitHub it looks like Sunday is day 1 (not day 0).

Bit long winded but using weekday() the slider in this project shows different weekdays in the Value Display.
The Menu widget is simply to select the current day of the week and doesn’t use weekday() but affects the calculation in the slider.

// weekday.ino by Costas 8/8/16

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

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

String currentTime;
unsigned long adjtime;
unsigned int currentweekdaynumber;
String DayOfWeekStr;

SimpleTimer timer;

WidgetRTC rtc;

BLYNK_ATTACH_WIDGET(rtc, V5);

// Utility function for digital clock display: prints preceding colon and leading 0
void printDigits(int digits)
{
  Serial.print(":");
  currentTime = currentTime + ":";
  if (digits < 10) {
    Serial.print('0');
    currentTime = currentTime + "0";
  }
  Serial.print(digits);
}

void showWeekday(){
  
}

void clockDisplay()  // Digital clock display of the time
{
  // You can call hour(), minute(), ... at any time, please see Time library examples for details

  String currentDate = String(day()) + "/" + month() + "/" + year();
  currentTime = String(hour());
  Serial.print("Current time: ");
  Serial.print(hour());
  printDigits(minute());
  currentTime = currentTime + String(minute());
  printDigits(second());  
  currentTime = currentTime + String(second());
  Serial.print("  Date: ");
  Serial.print(currentDate);
  Serial.println();
  Serial.print("Time String: ");
  Serial.println(currentTime);
  Blynk.virtualWrite(V1, currentTime);  // Send time to the App
  Blynk.virtualWrite(V2, currentDate);  // Send date to the App
}

void setup()
{
  Serial.begin(115200); // See the connection status in Serial Monitor
  Blynk.begin(auth, "yourssid", "yourpwd");

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  // Begin synchronizing time
  rtc.begin();

  // Other Time library functions can be used, like:
  //   timeStatus(), setSyncInterval(interval)...
  // Read more: http://www.pjrc.com/teensy/td_libs_Time.html
 
  timer.setInterval(1000L, clockDisplay); // Display digital clock every 1 second
  timer.setInterval(1000L, showWeekday);
  Blynk.syncVirtual(V4);  // Menu update weekday number
}

// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin 0
BLYNK_WRITE(V0)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V0 to a variable
  Serial.print("V0 Slider value is: ");
  Serial.println(pinValue);
  currentweekdaynumber = 2;
  adjtime = (pinValue * (60 * 60 * 24)) + (7 * (60 * 60 * 24)) - (currentweekdaynumber * (60 * 60 * 24)) ;
  adjustTime(adjtime); // advance by 1 to 7 days
  switch (pinValue)
  {
    case 1: { 
      DayOfWeekStr = "Sunday";
      break;
    }
    case 2: {
      DayOfWeekStr = "Monday";     
      break;
    }
    case 3: { 
      DayOfWeekStr = "Tuesday";   
      break;
    }
    case 4: { 
      DayOfWeekStr = "Wednesday";
      currentweekdaynumber = 4;
      break;
    }
    case 5: { 
      DayOfWeekStr = "Thursday";
      break;
    }
    case 6: { 
      DayOfWeekStr = "Friday";
      break;
    } 
    case 7: { 
      DayOfWeekStr = "Saturday";
      break;
    }          
  } 
  Blynk.virtualWrite(V3, DayOfWeekStr); 
  Blynk.virtualWrite(V6, weekday());
  adjustTime(-adjtime);  // reset back to today
}

BLYNK_WRITE(V4) {
  switch (param.asInt())
  {
    case 1: { // Item 1
      Serial.println("Sunday selected");
      currentweekdaynumber = 1;
      break;
    }
    case 2: { // Item 2
      Serial.println("Monday selected");
      currentweekdaynumber = 2;      
      break;
    }
    case 3: { // Item 3
      Serial.println("Tuesday selected");
      currentweekdaynumber = 3;      
      break;
    }
    case 4: { // Item 4
      Serial.println("Wednesday selected");
      currentweekdaynumber = 4;
      break;
    }
    case 5: { // Item 5
      Serial.println("Thursday selected");
      currentweekdaynumber = 5;
      break;
    }
    case 6: { // Item 6
      Serial.println("Friday selected");
      currentweekdaynumber = 6;
      break;
    } 
    case 7: { // Item 7
      Serial.println("Saturday selected");
      currentweekdaynumber = 7;
      break;
    }          
    default: {
      Serial.println("Sunday by default");
      currentweekdaynumber = 1;
    }
  }
  Serial.print("Weekday number is now ");
  Serial.println(currentweekdaynumber);
}

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

@gusgonnet I would, but I have no idea how to do it :frowning:

oh ok.
I never did such a thing, but I would look into doing something like the following.

this code goes outside loop():

bool executedForTheWeek = false;

this code goes in loop():

//execute the action you want (at 7am on fridays?)
if ((weekday() == 5) and hour()==7 and minute()==00) and not executedForTheWeek) {
executedForTheWeek = true;
executeTheActionIWant();
}
if ((weekday() == 5) and hour()==7 and minute()==01) and executedForTheWeek) {
executedForTheWeek = false;
}

I added a variable executedForTheWeek to avoid repeatedly triggering the action I want, since the microprocessor is executing the loop() function as fast as it can, all the time.

what do you think?
Gustavo.

I think you should move everything out of the loop and keep it running with SimpleTimer for one :slight_smile:

This would probably work, but you could also get the week/month number and see if it’s divisable by two without a leftover. That way you could tell even/odd weeks/months apart. It is how it’s usually done.

hey, what does that mean? thanks