Using Blynk + Hard coded routine

Hello all,

After a lot of Googling, I still can’t figure this out. I am working on a project where I want to have an automatic routine run while allowing for manual control through the Blynk app. I want to turn off the auto routine when manual mode is turned on through the app. Please take a look and let me know if you have a suggestion.

Thanks!

Hardware model + communication type: Arduino UNO with Ethernet 2 Shield
Android)

Blynk server


/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  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 Arduino Ethernet Shield 2 (W5500)
  to connect your project to Blynk.

  Note: This requires Ethernet2 library
    from http://librarymanager/all#Ethernet2

        Pins 10, 11, 12 and 13 are reserved for Ethernet module.
        DON'T use them in your sketch directly!

  Feel free to apply it to any other example. It's simple!
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
  

#include <SimpleTimer.h>
#include <SPI.h>
#include <Ethernet2.h>
#include <BlynkSimpleEthernet2.h>
BlynkTimer timer;


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

int Blynk_controlU = 7; //Digital Pin 5 is for Blynk manual Mode "UP"
int Blynk_controlD = 8; //Digital Pin 8 is for Blynk manual Mode "Down"
int Blynk_controlMANUAL = 5; // Digital Pin 7 is for Blynk Manual Mode 
int trigger = 3; //Digital Pin 3 is Trigger for manual mode
int Auto_ControlU = 6; //Digital Pin 6 is for Auto Control "UP"
int Auto_ControlD = 9; //Digital Pin 9 is for Auto Control "DOWN"
//////////////////////////////////////////////////////
void routine_manual()
{digitalWrite(Auto_ControlU, LOW);
     
    digitalWrite(Auto_ControlD, LOW);}
    
//////////////////////////////////////////////////////  
void routine_auto()
{digitalWrite(Auto_ControlU, HIGH);
  delay(1000);
    digitalWrite(Auto_ControlU, LOW);
     delay(1000);
    digitalWrite(Auto_ControlD, HIGH);
  delay(1000);
    digitalWrite(Auto_ControlD, LOW);
     delay(1000);}

//////////////////////////////////////////////////////

//////////////////////////////////////////////////////

void setup()
{
 pinMode(Blynk_controlU,OUTPUT);
  pinMode(Blynk_controlD,OUTPUT);
  pinMode(Blynk_controlMANUAL,OUTPUT);
  pinMode(trigger,INPUT);
  pinMode(Auto_ControlU,OUTPUT);
  pinMode(Auto_ControlD,OUTPUT); 
  timer.setInterval(1000L,TimerEvent);
  
 
 
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth);
  // You can also specify server:
  //Blynk.begin(auth, "blynk-cloud.com", 80);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8080);
  // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example
 
}

void TimerEvent(){
  if (digitalRead(trigger == 1)){
     routine_manual();
  }else{
    routine_auto();
  }
  Serial.println(digitalRead(trigger));
}


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


//////////////////////////////////////////////////////




A few comments…

  1. I wouldn’t use digital pins for this, I’d use virtual pins instead.

  2. I’d use an auto_mode flag (boolean variable) to indicate if I’m in auto mode or not. I’d call the auto mode routine using a timer, and start by checking if auto_mode ==true before executing the rest of teh routine.

  3. the blocking delays you are using here:

aren’t a good idea with Blynk. They will lead to disconnections and lack of responsiveness. Use non-blocking timeout timers in a lambda function if these delays are necessary.

Pete.

Pete,

Thank you for your reply. I am not sure what using a flag means, can you explain or is there a good resource for this? I tried looking it up and I do not understand…

Thanks,

Andy

A flag is simply a variable. In this case, used to indicate whether or not the system is in auto mode or not.

Pete.