Stepper Motor help

I just wanted to know if theirs a way to be able to make a stepper motor spin a certain amount of time and then stop spinning? I've been trying to make a code to do that but I haven't been able to succeed, so I was wondering if their is a code to do that on a Arduino Uno. I'm very new to coding so I've been struggling on this and I've only been able to just make it keep spinning. I've added a file to show how my code looks like.

StepperMotor1.ino (1000 Bytes)

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

It is much easier for people to help you if you include your code in your Post using the code button </>

See How to get the best out of the Forum

int motorpin1 = 6;
int motorpin2 = 5;
int motorpin3 = 4;
int motorpin4 = 3;

int delaytime=2;

#include <Stepper.h>

Stepper myStepper(32, 6, 4, 5, 3);

void setup() {
  // put your setup code here, to run once:
pinMode(motorpin1,OUTPUT);
pinMode(motorpin2,OUTPUT);
pinMode(motorpin3,OUTPUT);
pinMode(motorpin4,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
digitalWrite(motorpin3, LOW);
digitalWrite(motorpin4, LOW);

delay(delaytime);

digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);
digitalWrite(motorpin3, LOW);
digitalWrite(motorpin4, LOW);

delay(delaytime);

digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
digitalWrite(motorpin3, HIGH);
digitalWrite(motorpin4, LOW);

delay(delaytime);

digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
digitalWrite(motorpin3, LOW);
digitalWrite(motorpin4, HIGH);

delay(delaytime);
}

...R

You are not making any use of the Stepper library. Have you tried any of the example programs that come with it?

What stepper motor are you using which only has 32 steps for one complete revolution?

...R

Usually, you use a stepper driver but it almost looks like you're driving the coils directly. How is the stepper wired up?

Robin2:
You are not making any use of the Stepper library. Have you tried any of the example programs that come with it?

What stepper motor are you using which only has 32 steps for one complete revolution?

...R

I've looked into the stepper revolution example and also followed a video on it but i couldn't figure out a way where it would stop after 1 revolution because it would spin and stop and then it would start again and I want it to spin 1 full revolution and then stop without starting again. Also since I want it to stop it after 1 revolution i deleted the counterclockwise code in the example.

#include <Stepper.h>

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

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(32, 6, 4, 5, 3);

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

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

}

wildbill:
Usually, you use a stepper driver but it almost looks like you're driving the coils directly. How is the stepper wired up?

I have the Elegoo starter kit and I am using the stepper motor 5v dc while i connected it to the stepper motor driver module. I have connected the 4 pins to the arduino uno from the stepper motor driver and also using the 9v battery connected to the stepper driver module. So yes I am driving the coils directly using the stepper motor driver module.

Move the code in loop to the end of setup. Then it will just move once at power up.

Give this a try, it's a modification of the stepper library "oneRevolution" example. You can find others if (at the top of the IDE window) you click File --> Examples --> Stepper, and scroll down.

/*
 Stepper Motor Control - one revolution

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 3 - 6 of the Arduino.

 The motor should revolve one revolution in one direction.

 Created 11 Mar. 2007
 Modified 30 Nov. 2009
 by Tom Igoe
 */

#include <Stepper.h>

const int stepsPerRevolution = 2048;  // 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, 6, 4, 5, 3);

void setup() {
  // set the speed at 8 rpm:
  myStepper.setSpeed(8);
  // initialize the serial port:
  Serial.begin(9600);
}
void loop() {
  // step one revolution  in one direction:
  myStepper.step(stepsPerRevolution);
  delay(500);
  while(1); // press reset button to repeat.
}

JCA34F:
Give this a try, it's a modification of the stepper library "oneRevolution" example. You can find others if (at the top of the IDE window) you click File --> Examples --> Stepper, and scroll down.

/*

Stepper Motor Control - one revolution

This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 3 - 6 of the Arduino.

The motor should revolve one revolution in one direction.

Created 11 Mar. 2007
Modified 30 Nov. 2009
by Tom Igoe
*/

#include <Stepper.h>

const int stepsPerRevolution = 2048;  // 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, 6, 4, 5, 3);

void setup() {
 // set the speed at 8 rpm:
 myStepper.setSpeed(8);
 // initialize the serial port:
 Serial.begin(9600);
}
void loop() {
 // step one revolution  in one direction:
 myStepper.step(stepsPerRevolution);
 delay(500);
 while(1); // press reset button to repeat.
}

This way does work, but do you know is their another way without using the function "while"? or is this the only way?

 int E = 13;
 int D = 12;
 int C = 11;
 int DP = 10;
 int B = 9;
 int A = 8;
 int F = 7;
 int G = 6;
 
 int pushButton = 3;
 int counter = 0;
 
 int delaytime=2;




#include <Stepper.h>
 int motorpin1 = 5;
 int motorpin2 = 4;
 int motorpin3 = 2;
 int motorpin4 = 1;
 int Distance = 0;
const int stepsPerRevolution = 2048;

Stepper myStepper(32, 5, 2, 4, 1);

void StepperFunction (){
  myStepper.step(stepsPerRevolution);
  Distance = Distance + 1;
  if (Distance == 1){
  while (true);
}

}


 void one(){            //This code is  to display the Number 1
  digitalWrite(E,LOW);
  digitalWrite(D,LOW);
  digitalWrite(C,HIGH);
  digitalWrite(DP,LOW);
  digitalWrite(B,HIGH);
  digitalWrite(A,LOW);
  digitalWrite(F,LOW);
  digitalWrite(G,LOW);
}

 void Two(){            //This code is to display the Number 2
  digitalWrite(E,HIGH);
  digitalWrite(D,HIGH);
  digitalWrite(C,LOW);
  digitalWrite(DP,LOW);
  digitalWrite(B,HIGH);
  digitalWrite(A,HIGH);
  digitalWrite(F,LOW);
  digitalWrite(G,HIGH);
}

 void Three(){            //This code is to display the Number 3 
  digitalWrite(E,LOW);
  digitalWrite(D,HIGH);
  digitalWrite(C,HIGH);
  digitalWrite(DP,LOW);
  digitalWrite(B,HIGH);
  digitalWrite(A,HIGH);
  digitalWrite(F,LOW);
  digitalWrite(G,HIGH);
}

 void setup() {
  pinMode(E,OUTPUT);
  pinMode(D,OUTPUT);
  pinMode(C,OUTPUT);
  pinMode(DP,OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(A,OUTPUT);
  pinMode(F,OUTPUT);
  pinMode(G,OUTPUT);
  pinMode(pushButton,INPUT);

  pinMode(motorpin1,OUTPUT);
  pinMode(motorpin2,OUTPUT);
  pinMode(motorpin3,OUTPUT);
  pinMode(motorpin4,OUTPUT);

  myStepper.setSpeed(300);
 }

 void loop() { 
    int buttonState=digitalRead(pushButton);
        
  if (buttonState==HIGH){  
  if (counter > 3){ //Make sure the number will only be 1-3
   counter = 0;
}
  (counter++);
  delay(500);
}

  if (counter==1){       //Floor 1, display will show 1
    one(); 
}

  if (counter==2){       //Floor 2, display will show 2 
    Two();
    StepperFunction();
    }
 
   if (counter==3){       //Floor 3
     Three();
}


}

So the code above that I am doing is trying to make a basic way to have something similar like a elevator but not exactly. I'm trying to use 1 button to go to floor 1-3 by pressing the button twice it would go to floor 2 and pressing it 3 times would go to floor 3. I want to make it so that the stepper motor would only spin once so that it would go to floor 2 and then the motor would stop. I achieved that but while using the "While" function it makes my code just stop working so I want to know if their is another way to approach this problem. Also I attached a wiring diagram of how my wiring is set up. (Again, this is not suppose to be exactly like a elevator but similar, so please do not criticize me, I am trying to learn).

using the "While" function it makes my code just stop working

 while (true);

No surprise really as the code says "while true equals true do nothing"

What is it that should happen when Distance equals 1 ?

@magic_marble324, this seems to be a continuation of your other Thread. Please click Report to Moderator and ask to have the two Threads merged so we have all the info about your project in one place.

...R

there are millions of ways. The next step is your turn to describe the functionality that you want to have in the end.
A description with normal words.

And I guess you should start walking up the learning-curve of programming
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Topics merged