Stepper motor not rotating counterclockwise with blynk app

Board Arduino UNO
Connection Mode; USB

I have an example as you can see below that I am trying to make the steeper motor rotate anti-clockwise but it always rotates clockwise. With a sample without blink it works just fine.

I thought this line would do the trick
myStepper.step(-stepsPerRevolution);

Could you please help me and point me to the right direction

Regards
Rodrigo

    #include <Stepper.h>
    #include <SoftwareSerial.h>
    SoftwareSerial SwSerial(10, 11); // RX, TX
        
    #include <BlynkSimpleStream.h>
    const int stepsPerRevolution = 100;  // change this to fit the number of steps per revolution
    char auth[] = "859fa750c00b4c2e895de9922d7a82a0";

    Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

    BLYNK_WRITE(V2)
    {
    //  int myValue = param.asInt();
      myStepper.step(-stepsPerRevolution); // the value is 100 right now defined above
    }

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

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

hi there,

this is because stepsPerRevolution value didn’t get overwritten by value from Blynk.
you need the uncomment the line // int myValue = param.asInt() as this line is key in order to receive the value from blink.
so you would need something like this in BLYNK_WRITE
stepsPerRevolution = param.asInt();
myStepper.step(…

regs
Gabor.

aside from all other potential issues: shouldn’t it be this way?
(stepsPerRevolution * -1)
Is the compiler “wise enough” to handle that “minus” sign before declared variable??
Just asking…

Thanks guys for the responses.

I did comment out this line int myValue = param.asInt() just to make sure I knew I was passing a negative value and the rotor would rotate count clockwise.

I am posting below my original code that I change the values from blynk slider and I decide in which direction I want the motor to turn and at the end of the post I will post the original code from steeper example without blink that works, but it is done inside the loop and it takes a negative value without having to multiply the value from the slider by -1

#include <Stepper.h>
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
    
#include <BlynkSimpleStream.h>
const int stepsPerRevolution = 100;  // change this to fit the number of steps per revolution
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "859fa750c00b4c2e895de9922d7a82a0";

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

BLYNK_WRITE(V2)
{
  int myValue = param.asInt();
  if(myValue >= 0 && myValue <=50){
    myStepper.step(stepsPerRevolution);
  } else{
    myStepper.step(-stepsPerRevolution);
  }
}

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

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

and here is the original Arduino code that works just fine but without integrating with Blynk

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

Thanks again for the help

Rodrigo

@loviesbolies Search this forum for key words like stepper

There are a few other topics that should give you ideas on merging stepper code with Blynk control.

Thanks gunner . Believe me I am looking as much as I can but haven’t found any issues like mine so far. I will keep looking and researching

The key is not to look for same “issues” as you will rarely find them since everyone’s needs, code and process is different… but looking through whatever code they show does help give ideas :wink: