Controlling a stepper motor with buttons

Hi all, the objective of my project is to make the motor do a 90 degrees spin everytime I press either “left” or “right” button.
At the moment, everything is wired up and working with this code from letsarduino . com
but it does only move it as long as I keep the button pressed. How can I “delay” it to 90 degrees? Is it possible?

#include <CustomStepper.h>

CustomStepper stepper(2, 3, 4, 5, (byte){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4075.7728395, 12, CW);

int buttonPin = 8, // start button
buttonState = 0, // for holding state of button
stateMachine = 0, // to transition between different states fo
start = 0; // to start the state machine

boolean rotate1 = false;
boolean rotatedeg = false;
boolean crotate = false;

int button_1 = 2;
int button_2 = 3;
int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;

int motor_Speed = 4;
int motor_Step;
int val1 = 0;
int val2 = 0;

void setup() {
pinMode(button_1, INPUT);
pinMode(button_2, INPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);

pinMode(buttonPin, INPUT);

stepper.setRPM(12);
stepper.setSPR(4075.7728395);

}

void loop() {

{

if (stepper.isDone() && rotate1 == false)
{

stepper.setDirection(CW);
stepper.rotateDegrees(90);
rotate1 = true;
}

stepper.run();
}

val1 = digitalRead(button_1);
if (val1 == HIGH)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(motor_Speed);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(motor_Speed);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(motor_Speed);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(motor_Speed);

}

val2 = digitalRead(button_2);
if (val2 == HIGH)

{
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delay(motor_Speed);
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delay(motor_Speed);
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, LOW);
delay(motor_Speed);
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, HIGH);
delay(motor_Speed);
}
}

Why are you using the CustomStepper library as well as all those digitalWrite()s - isn't the library there to avoid all that tedious stuff?

You need code something like this

void loop() {
 leftBtnVal = digitalRead(leftBtnPin);
 rightBtnVal = digitalRead(rightBtnPin);


 if (leftBtnVal == LOW) {  // assumes LOW = pressed
 stepper.rotate( 90.0);
 }
 else if (rightBtnVal == LOW) {
 stepper.rotate(-90.0);
 }

 stepper.run();
}

I'm not familiar with that library so there may be errors in this. But it should point you in the right direction.

...R Stepper Motor Basics Simple Stepper Code

Hi c0rsa1r,
What are you driving your motors with? I have just build a basic robot using steppers, as I want it to DRAW! yes with a pen and it works… I used 2 cheap (about £1.50) modules based on the A3967 a few input controls but the ones used mostly are STEP and DIR so you don’t need all that code…

But surely you can test for a button press then just jump to a function to do the 90 Deg turn, your code seems to check for it continously!! I still need to add a couple of buttons and IR for remote control.

Here’s my code might give you an idea.

Hope it helps, regards.

Mel.

*
 * New Stepper Driver code
 * MS 31/07/2015
 * for new robot using 2 x 200 step
 * stepper motors
 * Updated: 27/08/2015
 * Add microStepping 04/08/2015
 * Now running both motors and drivers. 05/08/2015
 * Build PCB and populate, TESTING..  19/08/2015
 * Fit PCB and batteries, etc. 20/08/2015
 * Added Pen/servo control 21/08/2015
 * Added Graph, etc 30/08/2015
 */
#include <Servo.h>

const int DIRA=2,STEPA=3,DIRB=4,STEPB=5,sound=13;  // Pins to control steps and Direction.
const int MS=6,SLP=7;      // Control pins for MicroStepping & Sleep.
int speed=1500,x;         // Speed control, time between pulses, Minimum=350 uSecs (microstepping)  at 7 volts Min.
unsigned int NOS=1600,SFT=5875,Spin=2933;  //Constants, NOS=number of steps (Per rev). SFT=steps for full 360 turn.
Servo PenServo;

/* DIR=1=forward, DIR=0=Reverse
 *  
 * Turning! 2 types, turning around a still wheel, a turning circle of 330mm.
 * steps for full 360Deg turn is SFT=5875
 * 
 * Type 2 turning, that is one wheel forward, the other in reverse, a turning circle of 165mm.
 * Steps for full 360 wheel spin=2933
 * Step for 90 Deg turn=733?
 */ 

//***Start of Startup***
void setup()
{
    PenServo.attach(12);
    pinMode(DIRA, OUTPUT);      //Set up motor control pins.
    pinMode(STEPA, OUTPUT);
    pinMode(DIRB, OUTPUT);
    pinMode(STEPB, OUTPUT);
    pinMode(MS, OUTPUT);
    pinMode(SLP, OUTPUT);
    pinMode(sound, OUTPUT);
    digitalWrite(MS,HIGH);       //Enter micro-stepping mode
    digitalWrite(SLP,HIGH);      //Sleep mode
    PenUp();        //Make sure pen is UP!
    }  
 //End of startup***  

 //***Start of main program***

void loop()     //start main program.
{
    randomSeed(millis());  
    
    delay(1000);    // Wait for system/power to sabilize?

    for(int i=0; i<=5; i++) // Sound BEEP 5 times.
        {
          tone(sound,1600,10);
          delay(100);
        }
    
    digitalWrite(MS,HIGH);      //Microstepping
    
    PenDown();
    RTurn(SFT/2,speed);    //Do half Right turn
    //PenUp();
    //Forward(100);
    //PenDown();
    LTurn(SFT,speed);     //Do FULL left turn
    PenUp();
    Forward(100);
    //RT90(Spin/4);        //Do 90 Deg right turn
    PenDown();
    digitalWrite(MS,LOW);   //Revert to 200 steps, Full FAST stepping
    LTurn(SFT,speed);
    digitalWrite(MS,HIGH);  //Change to 1600 steps, Micro-stepping
    //PenUp();
    //RT90(Spin/4);
    Graph();
    Squares(500);
    digitalWrite(SLP,LOW);
    
}
//-------------------------------------------
void RTurn(int RX, int speed)
{
    digitalWrite(DIRA,HIGH);
    for(int i=0; i<=RX; i++)
        {
            digitalWrite(STEPA,LOW);
            delayMicroseconds(speed);
            digitalWrite(STEPA,HIGH);
            delayMicroseconds(speed);
        }
    delay(500);
}
//-------------------------------------------
void LTurn(int LX, int speed)
{
    digitalWrite(DIRB,HIGH);
    for(int i=0; i<=LX; i++)
        {
            digitalWrite(STEPB,LOW);
            delayMicroseconds(speed);
            digitalWrite(STEPB,HIGH);
            delayMicroseconds(speed);

        }
}
//-------------------------------------------
void PenUp()
{
    PenServo.write(90);
    delay(200);
}
//-------------------------------------------
void PenDown()
{
    PenServo.write(165);
    delay(200);
}
//------------------------------------------
void Forward(int x)
{
    digitalWrite(DIRA,HIGH);
    digitalWrite(DIRB,HIGH);

    for(int i=0; i <= x; i++)
        {
            digitalWrite(STEPA,0);
            digitalWrite(STEPB,0);
            delayMicroseconds(speed);
            digitalWrite(STEPA,1);
            digitalWrite(STEPB,1);
            delayMicroseconds(speed);
        }
}
//----------------------------------------
void Reverse(int x)
{
    digitalWrite(DIRA,LOW);
    digitalWrite(DIRB,LOW);

    for(int i=0; i <= NOS; i++)
        {
            digitalWrite(STEPB,0);
            digitalWrite(STEPA,0);
            delayMicroseconds(speed);
            digitalWrite(STEPB,1);
            digitalWrite(STEPA,1);
            delayMicroseconds(speed);

        }
}
//-----------------------------------------
void LT90(int x)     //Left Twirl 90Deg
{
    digitalWrite(DIRA,LOW);
    digitalWrite(DIRB,HIGH);

    for(int i=0; i<=x; i++)
        {
            digitalWrite(STEPB,0);
            digitalWrite(STEPA,0);
            delayMicroseconds(speed);
            digitalWrite(STEPB,1);
            digitalWrite(STEPA,1);
            delayMicroseconds(speed);
        }
}
//--------------------------------------------
void RT90(int x)      //Right Twirl 90Deg
{
    digitalWrite(DIRA,1);
    digitalWrite(DIRB,0);

    for(int i=0; i<=x; i++)
        {
            digitalWrite(STEPB,0);
            digitalWrite(STEPA,0);
            delayMicroseconds(speed);
            digitalWrite(STEPB,1);
            digitalWrite(STEPA,1);
            delayMicroseconds(speed);
        }
}
//-----------------------------------------------
void Square()
{
    PenDown();
    Forward(300);
    RT90(733);
    Forward(300);
    RT90(733);
    Forward(300);
    RT90(733);
    Forward(300);
    RT90(733);
}
//------------------------------------------------
 void Squares(int x)
{
     PenDown();
     do
    {
    Forward(x);
    RT90(733);
    Forward(x);
    RT90(733);
    Forward(x);
    RT90(733);
    Forward(x);
    RT90(733);
    x=x-50; 
    }
    while (x >40);
}

//------------------------------------------------
void Graph()
{
    
    int angle,Dist;
    PenDown;
    for(int i=0; i<=8; i++)
    {
    random(millis());     
    angle = random(100,700);
    Dist = random(50,400);
    Forward(Dist);
    RT90(angle *1.25);
    Forward(Dist);
    LT90(angle * 1.25);
    }
}