Controlling a stepper motor using Stepper library using arduino

I am having a problem in programming my stepper motor. I do not know how i can make the stepper motor revolve for a certain period of time or for a certain number of revolution using the Stepper.h library. Should i use a different library for this or am i missing something. thanks

Post your code!

chrisroque24:
or am i missing something. thanks

Yes.

Your code.

And please use the code button </> so your code looks like this and is easy to copy to a text editor

…R

Here is the code

int motor1Relay = 8;
int motor1Pin = 0;    // the pin that the pushbutton is attached to
int counter = 0;

int motor1State = 0;         // current state of the button
int motor1LastState = 0;     // previous state of the button
volatile unsigned long coinsCount = 0; //counts coin pulses
volatile unsigned long credit = 0;     // counts credit


#include <Stepper.h>
const int stepsPerRevolution = 1000; 
Stepper myStepper(stepsPerRevolution, 12, 13);

void sendInfo(const __FlashStringHelper* head, char* data){
  Serial.print(head);
  Serial.println(data);
}
 
void setup()
{
  pinMode(motor1Pin, INPUT);
  pinMode(motor1Relay, OUTPUT); 
  myStepper.setSpeed(120);
  Serial.begin(9600);
  attachInterrupt(1, acceptorCount, RISING); //Digital interrupt pin 2
  sendInfo(F("PLEASE INSERT COIN"), "");
}

void checkCoins(){
  while (coinsCount>0){
    coinsCount--;
   
    credit = credit + 1;
    Serial.print("credit : ");
    Serial.println(credit);
    
    delay(10);
  
  }
}
 
void loop()
{

   checkCoins();
   
   motor1State = digitalRead(motor1Pin);
   
  if (motor1State != motor1LastState) {

    if (motor1State == LOW && credit > 0) {
      credit--;
      Serial.print("credit : ");
      Serial.println(credit);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      digitalWrite(motor1Relay, HIGH);  // turn on motor1Relay
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      digitalWrite(motor1Relay, LOW);  // turn on motor1Relay
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      myStepper.step(stepsPerRevolution);
      
    } else {

      digitalWrite(motor1Relay, LOW);   // turn off motor1Relay
     
    }
    
    delay(50);
  }
  
  motor1LastState = motor1State;    
          
}

void acceptorCount()
{
  coinsCount++;
}

Post a link to the datasheet for your stepper motor.

What stepper motor driver are you using?
If your motor driver just needs step and direction signals I suggest you use the AccelStepper library.

This Simple Stepper Code can be used for testing without any library (assuming a step/direction driver).

You have awful lot of repetition in your code. Learn about for loops - or restructure your code to use the natural repetion of the loop() function

...R
Stepper Motor Basics