Sending constant value from widget

@Danial Yes, that was the intention, wasn’t it?
Make the changes according to the suggestions above and give it a try.

@IBK Thanks for your fast replys.
'timer' was not declared in this scope
I can’t use the timer, is there a library i need to include so that it works

BlynkTimer timer;

nedds to be added before you can use it …

@IBK Thanks for your fast reply. :slight_smile:

/*************************************************************
  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 with HC-06/HC-05
  Bluetooth 2.0 Serial Port Profile (SPP) module
  to connect your project to Blynk.

  Note: This only works on Android! iOS does not support SPP :(
        You may need to pair the module with your smartphone
        via Bluetooth settings. Default pairing password is 1234

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

  NOTE: Bluetooth support is in beta!

 *************************************************************/


// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
#include <BlynkSimpleSerialBLE.h>
#include <Stepper.h>

#define BLYNK_PRINT DebugSerial
#define BLYNK_USE_DIRECT_CONNECT

SoftwareSerial DebugSerial(0, 1); // RX, TX

BlynkTimer timer;
byte forwards = 0;
byte backwards = 0;

// STEPPER MOTOR //////////////////////////////////////////

const int stepsPerRevolution = 200;


Stepper myStepper1(stepsPerRevolution, 47, 49, 51, 53);   // 1 Stepper Motor
Stepper myStepper2(stepsPerRevolution, 2, 3, 4, 5);       // 2 Stepper Motor
Stepper myStepper3(stepsPerRevolution, 39, 41, 43, 45);   // 3 Stepper Motor

Stepper myStepper4(stepsPerRevolution, 10, 11, 12, 13);   // 4 Stepper Motor
Stepper myStepper5(stepsPerRevolution, 6, 7, 8, 9);       // 5 Stepper Motor
Stepper myStepper6(stepsPerRevolution, 23, 25, 27, 29);   // 6 Stepper Motor

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


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

void setup() {

  // Debug console
  DebugSerial.begin(9600);
  DebugSerial.println("Waiting for connections...");

  // Blynk will work through Serial
  // 9600 is for HC-06. For HC-05 default speed is 38400
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  timer.setInterval(50, driveMotors);

  int sp = 300;       //Motorspeed

  myStepper1.setSpeed(sp);  // setSpeed setzt die Maximale Geschwindigkeit der Schrittmotoren
  myStepper2.setSpeed(sp);  // "
  myStepper3.setSpeed(sp);  // "
  myStepper4.setSpeed(sp);  // "
  myStepper5.setSpeed(sp);  // "
  myStepper6.setSpeed(sp);  // "
}

void driveMotors()
{
  if (forwards == 1) forward();
  if (backwards == 1) backward();
}

void forward() {
  myStepper1.step(-1);
  myStepper2.step(-1);
  myStepper3.step(-1);
  myStepper4.step(-1);
  myStepper5.step(1);
  myStepper6.step(1);
}

void backward() {
  myStepper1.step(1);
  myStepper2.step(1);
  myStepper3.step(1);
  myStepper4.step(1);
  myStepper5.step(-1);
  myStepper6.step(-1);
}

void driveMotors()
{
  if (forwards == 1) forward();
  if (backwards == 1) backward();
}

BLYNK_WRITE(V0) {
  int x = param[0].asInt();
  int y = param[1].asInt();

  if (y > 250) {
    forwards = 1;
    backwards = 0;
  }
  else if (y < 35) {
    forwards = 0;
    backwards = 1;
  }
  else {
    forwards = 0;
    backwards = 0;
  }
}


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

When i run this code i get this error code message.

redefinition of 'void driveMotors()'

Not to much of a surprise if I look at the code: :wink:

@IBK My Fault :smiley::smiley::smiley::smiley::smiley:

@IBK
The Stepper motor doesn’t run… :frowning:

/*************************************************************
  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 with HC-06/HC-05
  Bluetooth 2.0 Serial Port Profile (SPP) module
  to connect your project to Blynk.

  Note: This only works on Android! iOS does not support SPP :(
        You may need to pair the module with your smartphone
        via Bluetooth settings. Default pairing password is 1234

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

  NOTE: Bluetooth support is in beta!

 *************************************************************/


// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
#include <BlynkSimpleSerialBLE.h>
#include <Stepper.h>

#define BLYNK_PRINT DebugSerial
#define BLYNK_USE_DIRECT_CONNECT

SoftwareSerial DebugSerial(0, 1); // RX, TX

BlynkTimer timer;
byte forwards = 0;
byte backwards = 0;

// STEPPER MOTOR //////////////////////////////////////////

const int stepsPerRevolution = 200;


Stepper myStepper1(stepsPerRevolution, 47, 49, 51, 53);   // 1 Stepper Motor
Stepper myStepper2(stepsPerRevolution, 2, 3, 4, 5);       // 2 Stepper Motor
Stepper myStepper3(stepsPerRevolution, 39, 41, 43, 45);   // 3 Stepper Motor

Stepper myStepper4(stepsPerRevolution, 10, 11, 12, 13);   // 4 Stepper Motor
Stepper myStepper5(stepsPerRevolution, 6, 7, 8, 9);       // 5 Stepper Motor
Stepper myStepper6(stepsPerRevolution, 23, 25, 27, 29);   // 6 Stepper Motor

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


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

void setup() {

  // Debug console
  DebugSerial.begin(9600);
  DebugSerial.println("Waiting for connections...");

  // Blynk will work through Serial
  // 9600 is for HC-06. For HC-05 default speed is 38400
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  timer.setInterval(50, driveMotors);

  int sp = 300;       //Motorspeed

  myStepper1.setSpeed(sp);  // setSpeed setzt die Maximale Geschwindigkeit der Schrittmotoren
  myStepper2.setSpeed(sp);  // "
  myStepper3.setSpeed(sp);  // "
  myStepper4.setSpeed(sp);  // "
  myStepper5.setSpeed(sp);  // "
  myStepper6.setSpeed(sp);  // "
}


void forward() {
  myStepper1.step(-1);
  myStepper2.step(-1);
  myStepper3.step(-1);
  myStepper4.step(-1);
  myStepper5.step(1);
  myStepper6.step(1);
}

void backward() {
  myStepper1.step(1);
  myStepper2.step(1);
  myStepper3.step(1);
  myStepper4.step(1);
  myStepper5.step(-1);
  myStepper6.step(-1);
}

void driveMotors()
{
  if (forwards == 1){
    forward();
  }
  if (backwards == 1){ 
    backward();
  }
}

BLYNK_WRITE(V0) {
  int x = param[0].asInt();
  int y = param[1].asInt();

  if (y > 250) {
    forwards = 1;
    backwards = 0;
  }
  else if (y < 35) {
    forwards = 0;
    backwards = 1;
  }
  else {
    forwards = 0;
    backwards = 0;
  }
}


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

Here are my settings on my Blynk App:

Try this change …

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

Just did a quick test with one stepper motor. It work’s fine :wink:

// Nano conncted via Serial
// Joystick on V30 
// Stepper motor on pin 2,3,4,5

#include <BlynkSimpleStream.h>
#include <Stepper.h>
BlynkTimer timer;

byte forwards = 0;
byte backwards = 0;

Stepper myStepper1(10, 2, 3, 4, 5);

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

char auth[] = "xxx";

void setup()
{
  Serial.begin(38400);
  Blynk.begin(Serial, auth);
  
  timer.setInterval(10L, driveMotors);
}

void forward()
{
  myStepper1.step(-1);
}

void backward()
{
  myStepper1.step(1);
}

void driveMotors()
{
  if(forwards == 1) forward();
  if(backwards == 1) backward();
}

BLYNK_WRITE(V30)
{ 
  int x = param[0].asInt();
  int y = param[1].asInt();
  
  if(y > 250)
  {
    forwards = 1;
    backwards = 0;
  }
  else if(y < 35)
  {
    forwards = 0;
    backwards = 1;
  }
  else
  {
    forwards = 0;
    backwards = 0;
  }
}

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

@IBK I will test this code soon. Thank you for your help.

There may be an easier way to do this. I’m also using AccelStepper and a joystick. Rather than try to continuously send the step() command, I have the joystick set to the fastest write interval (100ms). On BLYNK_WRITE, I tell the motor to moveTo(motor.currentPosition() + 1000), where in my setup, 1000 steps takes just over 100ms to reach. That way, in between updates from the joystick’ BLYNK_WRITE call, the motor is in constant motion. When i release the joystick, Blynk sends a 0 to that call, and I tell the motor to stop().

Of course, I send a negative value if the joystick was slid down instead of up. You can use the value itself to scale the motor’s speed. In my case, I dont really care about the jog speed.

Point is, if you use moveTo() rather than step() you can keep the motor running smoothly.

Hope this helps!

Hi @IBK,
Thanks for your help, luckly it works now.
Do you know how i can implement the virtualslider for the motor speed ?

@Danial: A slider for speed control is possible,of course. But as you already have the joystick in use I’d suggest to base the speed control on that as well. Currently it only works as a kind of switch.

If you e.g. give the joystick y position a range from -10 to +10 you can:

  • decide whether the motor shall “stop” ( y == 0 ), move direction “forwards” ( y > 0) or direction “backwards” ( y < 0)
  • base the speed of the motor on the absolute value of y ( abs(y) ) by changing the timer interval value in ten steps.

@IBK Thanks for your fast reply. Is there any example for slider widgets, that would be really helpfull :smiley:

@Danial: As I don’t do the programming for you :wink: I’d suggest you have a look at one of the examples from the Blynk library (Stroboscope.ino).
It shows how to adjust a timer using a slider - and this is what you need to do in your project as well.
Give it a try … :keyboard:

1 Like

@IBK Thank you very much for your help. I will try it.

Hi @IBK I am coming back here again because i have no clue how to fix this problem with the slider and the speed.
I hope you can help me.

Best regards.

@Danial: There are several ways of changing the speed. At the end it’s changing the timer value depending on the slider position.
One possible solution could look like this (changes marked with “// ++)”:

// Nano conncted via Serial
// Joystick on V30 
// Stepper motor on pin 2,3,4,5

#include <BlynkSimpleStream.h>
#include <Stepper.h>
BlynkTimer timer;

int timerID; // ++

byte forwards = 0;
byte backwards = 0;

Stepper myStepper1(10, 2, 3, 4, 5);

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

char auth[] = "xxx";

BLYNK_CONNECTED()   // ++
{                   // ++
  Blynk.syncAll();  // ++
}                   // ++

BLYNK_WRITE(V20) // Slider, range 1..50, send on release   // ++
{                                                          // ++
  int timerValue = param[0].asInt();                       // ++
  timerValue = constrain(timerValue, 1,50);                // ++
                                                           // ++
  timer.deleteTimer(timerID);                              // ++
  timerID = timer.setInterval(timerValue, driveMotors);    // ++
}

BLYNK_WRITE(V30)
{ 
  int x = param[0].asInt();
  int y = param[1].asInt();
  
  if(y > 250)
  {
    forwards = 1;
    backwards = 0;
  }
  else if(y < 35)
  {
    forwards = 0;
    backwards = 1;
  }
  else
  {
    forwards = 0;
    backwards = 0;
  }
}

void setup()
{
  Serial.begin(38400);
  Blynk.begin(Serial, auth);
  
  timerID = timer.setInterval(10L, driveMotors); // ++
}

void forward()
{
  myStepper1.step(-1);
}

void backward()
{
  myStepper1.step(1);
}

void driveMotors()
{
  if(forwards == 1) forward();
  if(backwards == 1) backward();
}

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