Need help with coding problem

I am trying to make my code run so that when the phoneValue is positive, the stepper motor turns but as it is when the code is run the motor makes a sound as though it is turning but no movement is seen. When i comment out the Blynk code and just run the motorOn() function it runs smoothly and the motor turns…

//Blynk set up code
#include <Blynk.h>
#include <SPI.h>
#include <BlynkSimpleEthernet.h>
#include <Ethernet.h>
#define BLYNK_PRINT Serial
#define W5100_CS  10
char auth[] = "xxx";


//motor set up code
#include <Stepper.h>
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

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

BLYNK_WRITE(V1) {
  int phoneValue = param.asInt();
  Serial.println(phoneValue);
  if (phoneValue){
    Serial.println("motor on");
    motorOn();
  }
}

void motorOn() {
  int ClockwiseRev = 0;
  while (ClockwiseRev < 3) {
    Serial.print("turning clockwise. this is revolution number (of 3 from 0): ");
    Serial.println(ClockwiseRev);
    myStepper.step(5 * stepsPerRevolution);
    delay(500);
    ClockwiseRev = ClockwiseRev + 1;
  }
  
  int antiClockwiseRev = 0;
  while (antiClockwiseRev < 3) {
    Serial.print("turning anti-clockwise. This is revolution number (of 3 from 0): ");
    Serial.println(antiClockwiseRev);
    myStepper.step(5*-stepsPerRevolution);
    delay(500);
    antiClockwiseRev = antiClockwiseRev + 1;
  }
}

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

Please edit your post and add triple backticks before and after your code so that it displays correctly. Triple backticks look like this:
```

The semicolons after curly brackets like this:

aren’t correct C++ syntax, so are probably contributing to your issues.

Pete.

Can you post what you see in the Serial Monitor, when the code runs? :slight_smile:

Also your motorOn() function has a blocking delay of at least 4 seconds. You need to rethink how to trigger/control your motor.

1 Like

Looking at your code you don’t seem to have a timer object associated with motorOn().

motorOn() is called in BLYNK_WRITE(V1) if phoneValue is true, thus no timer is needed as it’s called each time V1 changes to true from the app. I think the problem is more likely caused by the blocking delay, pointed out by JustBertC.

1 Like