Stepper motor code doesn't work

im a newbie with arduino can any body help
code:

#include <AccelStepper.h>

// motor pins
Preformatted text#define motorPin1 4 // Blue - 28BYJ-48 pin 1
#define motorPin2 5 // Pink - 28BYJ-48 pin 2
#define motorPin3 6 // Yellow - 28BYJ-48 pin 3
#define motorPin4 7 // Orange - 28BYJ-48 pin 4
// Red - 28BYJ-48 pin 5 (VCC)
// Blue - 28BYJ-48 pin GND
#define STEPS_PER_TURN 2048 // number of steps in 360deg;

int motorSpeed = 500; // High speeds (800 and above) may cause erratic behavior in 28BYJ-48
int motorAccel = 400; // As above: better avoiding extreme accelerations
int myPos = 0; // will be used to define a starting point for 360deg; rotations
int LeftTurnUp = 0; // Couple of flags to determine rotation direction
int RightTurnDown = 0; // Couple of flags to determine rotation direction
int Continuous = 0; // used below to discriminate single rotation commands
int incomingByte = 0; // for incoming serial data

/-----( Objects for stepper control )-----/
// Set up the stepper as 4 wire bipolar on pin 4,5,6,7
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper(4,motorPin1,motorPin3,motorPin2,motorPin4);

void setup()
{
Serial.begin(9600);
stepper.setMinPulseWidth(20); // Advisable setting to avoid that pulses from Arduino
// are too quick to be decoded
stepper.setMaxSpeed(motorSpeed);
stepper.setSpeed(motorSpeed);
stepper.setAcceleration(motorAccel);
// the following two lines reset "step zero" to the current position
stepper.setCurrentPosition(stepper.currentPosition());
stepper.runToPosition();
Serial.println("Available commands:");
Serial.println("R = continuous clockwise rotation");
Serial.println("r = 360deg; clockwise rotation");
Serial.println("L = continuous counter-clockwise rotation");
Serial.println("l = 360deg; counter-clockwise rotation");
Serial.println("S = stop rotation");
}

void loop()
{

if (Serial.available() > 0)
{
incomingByte = Serial.read();
{
if (incomingByte == 'R')
{
Serial.println("received «R» - activating continuous clockwise rotation");
// The two lines that follow allow to send commands in any sequence:
// before execution, a quick stop is performed
stepper.stop(); // Stop as fast as possible: sets new target
stepper.runToPosition(); // Now stopped after quickstop
// The following couple of flags determines rotation direction
LeftTurnUp = 1;
RightTurnDown = 0;
Continuous = 1; // used below to discriminate single rotation commands
stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here"
stepper.setSpeed(motorSpeed); // Previous commands have reset the speed
}

  if (incomingByte == 'L')
  {
    Serial.println("received «L» - activating continuous counter-clockwise rotation");
    // The two lines that follow allow to send commands in any sequence:
    // before execution, a quick stop is performed
    stepper.stop(); // Stop as fast as possible: sets new target
    stepper.runToPosition(); // Now stopped after quickstop
    // The following couple of flags determines rotation direction
    RightTurnDown = 1;
    LeftTurnUp = 0;
    Continuous = 1; // used below to discriminate single rotation commands
    stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here"
    stepper.setSpeed(motorSpeed); // Previous commands have reset the speed
  }

  if (incomingByte == 'r')
  {
    Serial.println("received «r» - activating single clockwise rotation");
    // The two lines that follow allow to send commands in any sequence:
    // before execution, a quick stop is performed
    stepper.stop(); // Stop as fast as possible: sets new target
    stepper.runToPosition(); // Now stopped after quickstop
    // The following couple of flags determines rotation direction
    LeftTurnUp = 1;
    RightTurnDown = 0;
    Continuous = 0; // used below to discriminate single rotation commands
    stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here"
    stepper.setSpeed(motorSpeed); // Previous commands have reset the speed
    // Since I will have to step forward 2047 steps, I store my current 
    // position as starting point of the rotation
    myPos=stepper.currentPosition();
  } 
  
  if (incomingByte == 'l')
  {
    Serial.println("received «l» - activating single counter-clockwise rotation");
    // The two lines that follow allow to send commands in any sequence:
    // before execution, a quick stop is performed
    stepper.stop(); // Stop as fast as possible: sets new target
    stepper.runToPosition(); // Now stopped after quickstop
    // The following couple of flags determines rotation direction
    RightTurnDown = 1;
    LeftTurnUp = 0;
    Continuous = 0; // used below to discriminate single rotation commands
    stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here"
    stepper.setSpeed(motorSpeed); // Previous commands have reset the speed
    // Since I will have to step backwards 2047 steps, I store my current 
    // position as starting point of the rotation
    myPos=stepper.currentPosition();
  }
  if (incomingByte == 'S')
  {
    Serial.println("received «S» - stopping rotation");
    // Reset the flags that determine rotation direction
    LeftTurnUp = 0;
    RightTurnDown = 0;
    stepper.stop(); // Stop as fast as possible: sets new target
    stepper.runToPosition(); // Now stopped after quickstop
  } 
}

}

if (Continuous == 1) // continuous rotation
{
if (LeftTurnUp == 1) //left turn
{
stepper.moveTo(10000); //move many steps - more then mechanically needed
}

if (RightTurnDown == 1)  //right turn
  {
   stepper.moveTo(-10000); //move many steps - more then mechanically needed
}
stepper.run();

}
if (Continuous == 0) // continuous rotation
{
if (LeftTurnUp == 1) //left turn
{
stepper.moveTo(myPos+STEPS_PER_TURN); // 1 turn = 2048 step
}

if (RightTurnDown == 1)  //right turn
  {
   stepper.moveTo(myPos-STEPS_PER_TURN); // 1 turn = 2048 step
}

}
stepper.run();
}

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

What should the code do ?
What does the code actually do ?

You should post sketches like this:

#include <AccelStepper.h>

// motor pins
#define motorPin1 4  // Blue - 28BYJ-48 pin 1
#define motorPin2 5  // Pink - 28BYJ-48 pin 2
#define motorPin3 6  // Yellow - 28BYJ-48 pin 3
#define motorPin4 7  // Orange - 28BYJ-48 pin 4
// Red - 28BYJ-48 pin 5 (VCC)
// Blue - 28BYJ-48 pin GND
#define STEPS_PER_TURN 2048  // number of steps in 360deg;

int motorSpeed = 500;   // High speeds (800 and above) may cause erratic behavior in 28BYJ-48
int motorAccel = 400;   // As above: better avoiding extreme accelerations
int myPos = 0;          // will be used to define a starting point for 360deg; rotations
int LeftTurnUp = 0;     // Couple of flags to determine rotation direction
int RightTurnDown = 0;  // Couple of flags to determine rotation direction
int Continuous = 0;     // used below to discriminate single rotation commands
int incomingByte = 0;   // for incoming serial data

/*-----( Objects for stepper control )-----*/
// Set up the stepper as 4 wire bipolar on pin 4,5,6,7
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper(4, motorPin1, motorPin3, motorPin2, motorPin4);

void setup() {
  Serial.begin(9600);
  stepper.setMinPulseWidth(20);  // Advisable setting to avoid that pulses from Arduino
  // are too quick to be decoded
  stepper.setMaxSpeed(motorSpeed);
  stepper.setSpeed(motorSpeed);
  stepper.setAcceleration(motorAccel);
  // the following two lines reset "step zero" to the current position
  stepper.setCurrentPosition(stepper.currentPosition());
  stepper.runToPosition();
  Serial.println("Available commands:");
  Serial.println("R = continuous clockwise rotation");
  Serial.println("r = 360deg; clockwise rotation");
  Serial.println("L = continuous counter-clockwise rotation");
  Serial.println("l = 360deg; counter-clockwise rotation");
  Serial.println("S = stop rotation");
}

void loop() {

  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    {
      if (incomingByte == 'R') {
        Serial.println("received «R» - activating continuous clockwise rotation");
        // The two lines that follow allow to send commands in any sequence:
        // before execution, a quick stop is performed
        stepper.stop();           // Stop as fast as possible: sets new target
        stepper.runToPosition();  // Now stopped after quickstop
        // The following couple of flags determines rotation direction
        LeftTurnUp = 1;
        RightTurnDown = 0;
        Continuous = 1;                                         // used below to discriminate single rotation commands
        stepper.setCurrentPosition(stepper.currentPosition());  // Set step 0 "here"
        stepper.setSpeed(motorSpeed);                           // Previous commands have reset the speed
      }

      if (incomingByte == 'L') {
        Serial.println("received «L» - activating continuous counter-clockwise rotation");
        // The two lines that follow allow to send commands in any sequence:
        // before execution, a quick stop is performed
        stepper.stop();           // Stop as fast as possible: sets new target
        stepper.runToPosition();  // Now stopped after quickstop
        // The following couple of flags determines rotation direction
        RightTurnDown = 1;
        LeftTurnUp = 0;
        Continuous = 1;                                         // used below to discriminate single rotation commands
        stepper.setCurrentPosition(stepper.currentPosition());  // Set step 0 "here"
        stepper.setSpeed(motorSpeed);                           // Previous commands have reset the speed
      }

      if (incomingByte == 'r') {
        Serial.println("received «r» - activating single clockwise rotation");
        // The two lines that follow allow to send commands in any sequence:
        // before execution, a quick stop is performed
        stepper.stop();           // Stop as fast as possible: sets new target
        stepper.runToPosition();  // Now stopped after quickstop
        // The following couple of flags determines rotation direction
        LeftTurnUp = 1;
        RightTurnDown = 0;
        Continuous = 0;                                         // used below to discriminate single rotation commands
        stepper.setCurrentPosition(stepper.currentPosition());  // Set step 0 "here"
        stepper.setSpeed(motorSpeed);                           // Previous commands have reset the speed
        // Since I will have to step forward 2047 steps, I store my current
        // position as starting point of the rotation
        myPos = stepper.currentPosition();
      }

      if (incomingByte == 'l') {
        Serial.println("received «l» - activating single counter-clockwise rotation");
        // The two lines that follow allow to send commands in any sequence:
        // before execution, a quick stop is performed
        stepper.stop();           // Stop as fast as possible: sets new target
        stepper.runToPosition();  // Now stopped after quickstop
        // The following couple of flags determines rotation direction
        RightTurnDown = 1;
        LeftTurnUp = 0;
        Continuous = 0;                                         // used below to discriminate single rotation commands
        stepper.setCurrentPosition(stepper.currentPosition());  // Set step 0 "here"
        stepper.setSpeed(motorSpeed);                           // Previous commands have reset the speed
        // Since I will have to step backwards 2047 steps, I store my current
        // position as starting point of the rotation
        myPos = stepper.currentPosition();
      }
      if (incomingByte == 'S') {
        Serial.println("received «S» - stopping rotation");
        // Reset the flags that determine rotation direction
        LeftTurnUp = 0;
        RightTurnDown = 0;
        stepper.stop();           // Stop as fast as possible: sets new target
        stepper.runToPosition();  // Now stopped after quickstop
      }
    }
  }

  if (Continuous == 1)  // continuous rotation
  {
    if (LeftTurnUp == 1)  //left turn
    {
      stepper.moveTo(10000);  //move many steps - more then mechanically needed
    }

    if (RightTurnDown == 1)  //right turn
    {
      stepper.moveTo(-10000);  //move many steps - more then mechanically needed
    }
    stepper.run();
  }
  if (Continuous == 0)  // continuous rotation
  {
    if (LeftTurnUp == 1)  //left turn
    {
      stepper.moveTo(myPos + STEPS_PER_TURN);  // 1 turn = 2048 step
    }

    if (RightTurnDown == 1)  //right turn
    {
      stepper.moveTo(myPos - STEPS_PER_TURN);  // 1 turn = 2048 step
    }
  }
  
  stepper.run();
}

Wiring diagram?

Kindly share us the circuit image

Code works perfectly for me (@johnwasser's version). Which driver are you using?

Hi,
Have you been able to get the stepper moving at all?
Have you developed your code in stages and have some code that JUST runs the stepper?

If not, then STOP, and write some code JUST to prove your hardware and that you understand how to drive it.

Tom.. :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.