Sending constant value from widget

Hi Guys,

I’ve got an problem with the Virtual Pin combinded with the Joystick widget. I want that the value from my joystick widget sends constantly an value to the virtual pin. I already tested the Joystick example for the serial output and realised that there is no constantly sending of values. The values are only sending when i move my joystick a little bit.

So my question is, is it possible that i can send constantly an value of the joystick without moving the joystick?

Best regards

@Danial hello. No. However, you can implement this within your code. You may add timer with “if no values in last seconds, do something”.

Thanks for your fast reply. I am using the joystick for my Stepper Motor project, so i can move the joystick forward and the motor should go constantly forward, but when there is no constantly sending of values from the joystick widget, then it’s is useless for me. Do you have an another suggestion about my problem?

Hello. You can do that in your code as they told you. There is no need for the widget to send the value constantly.
Maybe you will need to re-write or re-think your code, but is completely possible.

@Luciano Hi, do you have an example for that type of code ?
Because i don’t have any ideas, i have researched everywhere.
That’s the reason why i made an new topic. It would be great if you or someone else has any ideas.

Thanks for helping!

I can’t test this, but I think in something like this. I added a button to stop the motor.

#define STOP  0
#define UP    1
#define DOWN  2
#define LEFT  3
#define RIGHT 4

int moveDirection = STOP;

void setup()
{
  ....
}

void loop()
{
  Blynk.run();
  moveMotors(moveDirection); //something to move the motors
}


BLYNK_WRITE(V0) // A Button to STOP
{
  int button = param.asInt();
  if (button == 1)
    moveDirection = STOP;

}
BLYNK_WRITE(V1) //JOYSTICK
{
  int x = param[0].asInt();
  int y = param[1].asInt();
  if (y == 1023)
  {
    moveDirection = UP;
  }
  else if (y == 0)
  {
    moveDirection = DOWN;
  }
  else if (x == 1023)
  {
    moveDirection = RIGHT;
  }
  else if (x == 0)
  {
    moveDirection = LEFT;
  }
}

@Luciano I already wrote the code like that, but it doesn’t work like that with constantly sending of values. I also set the Code in a while loop in the BLYNK_WRITE(V1) but it only starts the motor but never stops again.

PS: I am using an l298n Module with the combination of Stepper Motor, Arduino Mega and an HC-06 Bluetooth Module.

It should work… maybe you have to change your program (the motor moving part)
This way if you pull the joystick UP, moveDirection will stay UP unless yo move it to other position (DOWN; LEFT or RIGHT) or press STOP button.
Then you need to use that information to move the motors, that has nothing to do with Blynk, its coding.
Maybe if you post your complete code I can help you, but I dont know much about stepper motors.

@Luciano Without Blynk, only the code and an normal hardware joystick I can move the motor. But when i implement the code in blynk and want to configure everything so that it work like i think it should. I upload the programm, but it doesn’t work. However i can send you the code tomorrow and maybe some screenshots of my Code and my Blynk programm.

Anyway thank you for your helping and time, i really appreciate that.

int xval = 0;
int yval = 0;






void setup()
{
 
    
    
}

void loop()
{
  Blynk.run();
  if (xval == 2)
  {
    //up
  } 
  else if (xval == 3)
  {
    // stop
  }
  else if (xval == 4)
  {
    // down
  }
  
      

 if (yval == 2)
  {
    // Left
  }
  else if (yval == 3)
  {
    // Step
  }
  else if (yval == 4)
  {
    // Right
  }
    
}


BLYNK_WRITE(V0) // A Button to STOP
{
  int button = param.asInt();
  if (button == 1)
    moveDirection = STOP;

}
BLYNK_WRITE(V1) //JOYSTICK
{
  int x = param[0].asInt();
  int y = param[1].asInt();
 

 if (x == 2)
  {
   xval = 2;  //up
  }
 

else if (x == 3)
  {
   xval = 3;  // stop
  }


 else if (x == 4)
  {
    xval = 4;  // down
  }
  
      

if (y == 2)
  {
    yval = 2; // Left
  }

else if (y == 3)
  {
   yval =3;  // Step
  }

  else if (y == 4)
  {
    yval = 4;  // Right
  }
}



@AbuJarrah Thanks for your reply to my problem, i will test your code.

1 Like

@AbuJarrah From the logical perspective the code should work.
But how do you control the speed of the motors?

1 Like
int xval = 0;
int yval = 0;
int Speed = 0;






void setup()
{
 
    
    
}

void loop()
{
  Blynk.run();
  if (xval == 2)
  {
         analogWrite(MotorA, Speed);    //up
  } 
  else if (xval == 3)
  {
    // stop
  }
  else if (xval == 4)
  {
    // down
  }
  
      

 if (yval == 2)
  {
    // Left
  }
  else if (yval == 3)
  {
    // Step
  }
  else if (yval == 4)
  {
    // Right
  }
    
}


BLYNK_WRITE(V0) // A Button to STOP
{
  int button = param.asInt();
  if (button == 1)
    moveDirection = STOP;

}
BLYNK_WRITE(V1) //JOYSTICK
{
  int x = param[0].asInt();
  int y = param[1].asInt();
 

 if (x == 2)
  {
   xval = 2;  // stop

  }
 

else if (x == 3)
  {
   xval = 3;  // stop
  }


 else if (x == 4)
  {
    xval = 4;  // down
  }
  
      

if (y == 2)
  {
    yval = 2; // Left
  }

else if (y == 3)
  {
   yval =3;  // Step
  }

  else if (y == 4)
  {
    yval = 4;  // Right
  }
}

BLYNK_WRITE(V?) // Slider For Speed
{
  int pinValue = param.asInt();
Speed = pinValue;
}

@AbuJarrah

… could work for a normal DC motor but not for stepper motors.

This is just an example of how you can determine the value of speed and direction

@IBK @AbuJarrah

// 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


// 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);
  
  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 Stop(){
  myStepper1.step(0);
  myStepper2.step(0);
  myStepper3.step(0);
  myStepper4.step(0);
  myStepper5.step(0);
  myStepper6.step(0);
}

BLYNK_WRITE(V0){
  
  int x = param[0].asInt();
  int y = param[1].asInt();
  
  if(y > 250){
    forward();
  }
  if(y < 35){
    backward();
  }
  if(y == 128){
    Stop();
  }
}


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

I’d suggest to try what @Dmitriy suggested: “You may add timer …”

void setup(){
   ...
  timer.setInterval(50L, driveMotors); // Value needs to be tested and adjusted!!
}

and then

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

with

...
byte forwards = 0;
byte backwards = 0;
// STEPPER MOTOR ///////
...

globally declared.

Further changes:
Looking inside the source of the library helps to understand how the library works:

void Stop(){
  myStepper1.step(0);
  ...
}

… does nothing! Can be deleted.

Calling

myStepper1.setSpeed(sp);
...

in the library this is done:

void Stepper::setSpeed(long whatSpeed)
{
  this->step_delay = 60L * 1000L * 1000L / this->number_of_steps / whatSpeed;
}

I.e. with steps only set to “1” or “-1” - as in the code above - I’d do:

myStepper1.setSpeed(60L * 1000L * 1000L);
...

… because speed - in this particular application - is only determined by timer.setInterval(50L, driveMotors);
No unnecessary additional delay.:wink:

My BLYNK_WRITE(V0) would look like this:

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;
  }
}

Did not have the time to test. But should work in principal.
Give it a try.

@IBK Thanks for your reply.

What does the 50L mean?

I think you are describing the AccelStepper Library, which has nothing to do with the Arduino Stepper.h Library i think.


What I want to use the Blynk for:
I want to use Blynk for the Stepper motor control and i want this manually not with Timer or something else.
My idea is that i can move the Stepper Motors forwards and backwards with the joystick and that constantly. I just want the joystick movement forward, backward, left and right i will add too only if the forward and backward is possible with Blynk App and Library.

It means the timer intervall is 50ms. L can also be omitted here.
Se also SimpleTimer doks.

Wrong. My quote is a 1:1 copy from the Stepper.h !

@IBK Thanks for your reply.
When i use the timer is it possible that i can move the joystick forward and the Stepper Motor is constantly moving forward or is it not possible with the timer?