Help for 4 wire stepper + L293D and Arduino code

ozgur84

Member
Hello eveyrbody,

I am new in Arduino world and actually I have just ordered my arduino uno r3 yesterday and waiting for the shipment. I also started reading "Massimo Banzi's Getting started with Arduino" and related to the LED project at chapter 4 I found some clues. After reading some projects which I found on internet I have some ideas however as a newbee I have bunch of questions to ask

I want to control a 4 wire stepper motor (mitsumi M42SP-6NK) with L293D. I have build my motor shiled based pn the project(picture) below with L293D instead.



What I want to do is controlling the stepper motor motion with two push buttons. This is how the whole thing should work...

4 digital pins of arduino will be assigned as output pins and will be connected to the input pins of L293D(pins 2,7,10,15),

2 digitalpins of arduino will be connected to the 2 push buttons.

1.when I press one button the motor should turn 8 steps in forward (or clockwise) direction and it should stop
2.when I press the other button the motor should turn 8 steps in counter clockwise direction and stop.

as the code I thought something like this (at least for the first steps)



Code:
#include <Stepper.h>
 
int in1Pin = 12; // to the pin15 of L293D
int in2Pin = 11; // to the pin10 of L293D
int in3Pin = 10; // to the pin7 of L293D
int in4Pin = 9; // to the pin2 of L293D
const int BUTTONCW =7; // button for clockwise rotation
const int BUTTONCCW = 8; // botton for counter clockwise rotation
 
int val1 = 0; // val will be used to store the state
// of the input pin clockwise 
int old_val1 = 0; // this variable stores the previous
// value of "val1" clockwise
int val2 = 0; // val will be used to store the state
// of the input pin counter clockwise 
int old_val2 = 0; // this variable stores the previous
// value of "val2" counter clockwise

int state1 = 0; // 0 = motor stands still and 1 = motor rotates clockwise 
int state2 = 0; // 0 = motor stands still and 1 = motor rotates counter clockwise 

Stepper motor(48, in1Pin, in2Pin, in3Pin, in4Pin);  
 
void setup()
{
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);
  pinMode(BUTTONCW, INPUT);
  pinMode(BUTTONCCW, INPUT);
  motor.setSpeed(8);
}
 
void loop ()
{
  // check if there was a transition 
  val1 = digitalRead(BUTTONCW); // read input value and store it for clockwise
if ((val1 == HIGH) && (old_val1 == LOW)){ 
state1 = 1 - state1;
delay(1000); // to avouid button bouncing
if ((val2 == HIGH) && (old_val2 == LOW)){ 
state2 = 1 - state2;
delay(1000); // to avouid button bouncing

} 
 old_val1 = val1; // val1 is now old, let's store it
 old_val2 = val2; // val2 is now old, let's store it

void loop()
{
  
  if (state1 == HIGH)
  {
    motor.step(steps ,forward);
    
    // I SUPPOSE THE HELP IS NEEDED AFTER OR AROUND THIS POINT
  }
}

So I am open for all ideas and modifications.

Regadrs
Özgür
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…