Reading existing slider value and use it to the input

Hi all,

May I know whether it is possible to read the value of the slider on our apps on the board?
I would like to use that value to be a variable for a motor movement.
I have the value of the slider from -3 to 3.
For example the value of the slider right now is -1 and I slide it to 3, my motor should rotate clockwise 4 times.
If the value right now is 0 and I slide it to -3, my motor should rotate counter clockwise 3 times.
Right now, I have my code just to rotate the motor with the inserted value (not the difference between the existing value and the new value)
I am using L298N and NEMA 17.


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Stepper.h>

const int stepsPerRevolution = 100;
int x;
int y;
int z;
Stepper myStepper(stepsPerRevolution, 16, 5, 4, 0 );

char auth[] = "xxxx";
char ssid[] = "xxxx";
char pass[] = "xxxx";

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  myStepper.setSpeed(60);
}

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

BLYNK_WRITE(V1)
{
  Blynk.syncVirtual(V1);
  Serial.println(param.asInt());
  y = param.asInt();
  if (y > 0)
  {
    for (int x = 0; x <= y; x++)
    {
      Serial.println(x);
      myStepper.step(stepsPerRevolution);
      delay(10);
    }
  }

  if (y < 0)
  {
    z = -y;
    for (int x = 0; x <= z; x++)
    {
      Serial.println(x);
      myStepper.step(-stepsPerRevolution);
      delay(10);
    }
  }
}

Thanks in advance!

I’d say that the way to handle this is to save the last slider value in a variable, then when the BLYNK_WRITE(vPin) is triggered get the new value and compare it to the old one; and move your motor accordingly.
You’d then overwrite the old value with the new one, so that it can be used next time around.

I think you’d need the slider widget set to ‘send on release’ for this process to work smoothly, but that something you’d need to experiment with.

Pete.

Hi Pete,

Thanks for the solution on that! I got the idea on that and I have edited my code as below. The problem now is my motor and motor driver gets really hot even if the motor stops running. Is there anyway to turn the motor off until I operate the slider?


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Stepper.h>

const int stepsPerRevolution = 100;
int x;
int y;
int z;
int yprev;
int a;
Stepper myStepper(stepsPerRevolution, 16, 5, 4, 0 );

char auth[] = "xxxx";
char ssid[] = "xxxx";
char pass[] = "xxxx";

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  myStepper.setSpeed(60);
}

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

BLYNK_WRITE(V1)
{
  Serial.println(param.asInt());
  a = param.asInt();
  y = a - yprev;
  if (y > 0)
  {
    for (int x = 1; x <= y; x++)
    {
      Serial.println(x);
      Serial.println("clockwise");
      myStepper.step(stepsPerRevolution);
      delay(10);
    }
  }

  if (y < 0)
  {
    z = -y;
    for (int x = 1; x <= z; x++)
    {
      Serial.println(x);
      Serial.println("counterclockwise");
      myStepper.step(-stepsPerRevolution);
      delay(10);
    }
  }
  yprev = a;
}

You can stop the motor after reaching the desired postion mystepper.setSpeed(0);
But i dont know whether the library you are using have this function. This is shoud basically turn off the output of the driver.

Or if you have a good driver then it will have a ENABLE pin to start or stop the motor.

But if you want to hold the position of the stepper motor then you should not stop it. Because turning off the motor will loose its magnetic field and loose its position. To hold its positions it will draw current and that will result in heating.