Integrating Blynk into my code

Hello everyone,
In a project, I am using 2 buttons to control a stepper motor, one button for each direction. The code works with the physical buttons, however, I tried to include aspects from the BlynkBlink code to make it wireless, and the code no longer works.
My code is (with the BlynkBlink pieces included):


#define BLYNK_PRINT Serial // Enables Serial Monitor

// Following includes are for Arduino Ethernet Shield (W5100)
// If you're using another shield, see Boards_* examples
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
 char auth[] = "4b86e5a75b2c437ca4cfe27ad721c1dd";

   
#include <BlynkSimpleStream.h>
#include "Stepper.h"


#define STEPS  32   // Number of steps per revolution of Internal shaft
int  Steps2Take;  // 2048 = 1 Revolution
int button = 6; 
int button2 = 7;
int buttonState = 0;
int buttonState2 = 0;

Stepper small_stepper(STEPS, 8, 10, 9, 11);

void setup()
{
 pinMode(button, INPUT);
 pinMode(button2, INPUT);
 Serial.begin(9600);
  Blynk.begin(auth);
}

void loop()
{
   Blynk.run();
  buttonState = digitalRead(button);
  buttonState2 = digitalRead(button2);
if(buttonState == HIGH){
  
 small_stepper.setSpeed(500); //Max seems to be 700
                      Steps2Take  =  2048;  // Rotate CW
                      small_stepper.step(Steps2Take);
                      delay(200); 

}
if(buttonState2 == HIGH){
   small_stepper.setSpeed(500); //Max seems to be 700
                      Steps2Take  =  -2048;  // Rotate CW
                      small_stepper.step(Steps2Take);
                      delay(200); 
}

}

I can only assume that putting in the BlynkBlink code pieces caused the error. Any advice helps.
Thanks

I forgot to take out the delay() commands, and when I did it made no difference. Just FYI

Does it work with physical buttons after you Blynkified your code?

no it does not, neither the physical button work or the buttons on the app. We have been using the classic BlynkBlink with the app.
Here is the code we are using to use the Servo with only the physical buttons:

#include "Stepper.h"

#define STEPS  32   // Number of steps per revolution of Internal shaft
int  Steps2Take;  // 2048 = 1 Revolution
int button = 6; 
int button2 = 7;
int buttonState = 0;
int buttonState2 = 0;

Stepper small_stepper(STEPS, 8, 10, 9, 11);

void setup()
{
 pinMode(button, INPUT);
 pinMode(button2, INPUT);
 Serial.begin(9600);
}

void loop()
{
  buttonState = digitalRead(button);
  buttonState2 = digitalRead(button2);
if(buttonState == HIGH){
  
 small_stepper.setSpeed(500); //Max seems to be 700
                      Steps2Take  =  512;  // Rotate CW
                      small_stepper.step(Steps2Take);
                      delay(200); 

}
if(buttonState2 == HIGH){
   small_stepper.setSpeed(500); //Max seems to be 700
                      Steps2Take  =  -512;  // Rotate CW
                      small_stepper.step(Steps2Take);
                      delay(200); 
}

Serial.println(buttonState);
}

Here is our Blynkified version:

#include <Stepper.h>
#define STEPS 32
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleStream.h>

char auth[] = "4b86e5a75b2c437ca4cfe27ad721c1dd";

int Steps2Take;
int button = 6;
int button2 = 7;
int buttonState = 0;
int buttonState2 = 0;

Stepper small_stepper(STEPS, 8, 10, 9, 11);
void setup()
{

  DebugSerial.begin(9600);  
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  pinMode(button, INPUT);
  pinMode(button2, INPUT);
}

void loop()
{
  Blynk.run();
  if(buttonState == HIGH){
  
 small_stepper.setSpeed(500); //Max seems to be 700
                      Steps2Take  =  512;  // Rotate CW
                      small_stepper.step(Steps2Take);
                     
}

if(buttonState2 == HIGH){
   small_stepper.setSpeed(500); //Max seems to be 700
                      Steps2Take  =  -512;  // Rotate CW
                      small_stepper.step(Steps2Take);
                   
}
}

@dgol did you try any of the Blynk examples before trying to set up your project?

Very few people can Blynkify a sketch without having learnt the basics first.

3 Likes

Tried a few.

@Costas :sweat_smile: sorry )

6 Likes

Here is a more updated code. The motor doesnt turns on when the code is blynkafied

#include <Blynk.h>
#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
#include <BlynkSimpleStream.h>
#include "Stepper.h"
#define STEPS 32
Stepper small_stepper(32, 8, 10, 9, 11);
char auth[] = "4b86e5a75b2c437ca4cfe27ad721c1dd";
SoftwareSerial SwSerial(2, 3); // RX, TX

int Steps2Take;
int button = 6;
int button2 = 7;
int buttonState = 0;
int buttonState2 = 0;


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

  pinMode(button, INPUT);
  pinMode(button2, INPUT);
}

void loop()
{
  buttonState = digitalRead(button);
  buttonState2 = digitalRead(button2);
 Blynk.run();

  if((buttonState == HIGH)||(digitalRead(13) == HIGH))
  {
  small_stepper.setSpeed(500); //Max seems to be 700
                      Steps2Take  =  512;  // Rotate CW
                      small_stepper.step(Steps2Take);
  }
  if ((buttonState2 == HIGH) || (digitalRead(12) == HIGH))
  {
    small_stepper.setSpeed(500); //Max seems to be 700
                      Steps2Take  = -512;  // Rotate CW
                      small_stepper.step(Steps2Take);
  }
}

I don’t see any code for widgets.

What I’m beginning to learn is that buttons need some calls above the setup to tell the MCU to look at the virtual pins for a change in value.

BLYNK_WRITE(V1) {
Button1 = param.asInt();
}

BLYNK_WRITE(V2) {
Button2 = param.asInt();
}

Now Button1 is tied to V1. And Button2 to V2 respectively.

Is there a way to turna digital button widget from an output to an input, or will I need to use virtual pins for that?

To my knowledge, widgets don’t need to be declared as input or output as they don’t physically connect to any MCU pins. That aside though, the functionality remains the same. If button == 1, do thing A, else if button == 0, do thing B.

@dgol I think you need to read up about Value Displays… http://docs.blynk.cc/#widgets-displays-value-display (if you want a simple input on the App)

Heck, read the entire documents page for further Blynk Jedi insight :wink:

1 Like