Go Down

Topic: Help for 4 wire stepper + L293D and Arduino sketch code [SOLVED!] (Read 7753 times) previous topic - next topic

ozgur84

Mar 07, 2013, 10:56 am Last Edit: Mar 07, 2013, 06:26 pm by ozgur84 Reason: 1
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 on the project(and some others) below
http://www.hobbytronics.co.uk/stepper-motor-sn754410


What I want to do is controlling the stepper motor motion with two push buttons as a stand-alone application. 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)  edit: The code is not complete!!

So I am open for all ideas and modifications.

Regadrs

Code: [Select]

#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
 }
}

PaulS

Quote
So I am open for all ideas

When your Arduino arrives, load that code and see what happens.
The art of getting good answers lies in asking good questions.

ozgur84


Quote
So I am open for all ideas

When your Arduino arrives, load that code and see what happens.


this is also an idea thanks :) but if you read the code till the end, you can see it is not complete and I put a notice through the end of the code
Code: [Select]
// I SUPPOSE THE HELP IS NEEDED AFTER OR AROUND THIS POINT

I would appriciate any constructive ideas though :)

PaulS

Code: [Select]
delay(1000); // to avouid button bouncing
One second? Get rid of that POS switch and get a decent one. Even a 99 cent radio shack cheapie won't bounce for anywhere near that long.

Code: [Select]
old_val1 = val1; // val1 is now old, let's store it
old_val2 = val2; // val2 is now old, let's store it

Not outside of a function, you won't.

You are allowed ONE loop function.

You know, you are allowed to use the Verify function even without an Arduino connected.

Reading the second switch would be a good idea. Grouping all the code dealing with one switch together makes more sense than trying to mix reading one switch and acting on it with reading the other switch and acting on it.

If a switch is determined to have become pressed, it will take time to step the stepper motor. While that is happening, any bouncing that happens on that switch is likely to have died out. So, debouncing the switches is probably not necessary. Even if it is, 10 milliseconds is a more reasonable time.
The art of getting good answers lies in asking good questions.

ozgur84

#4
Mar 07, 2013, 06:26 pm Last Edit: Mar 07, 2013, 06:36 pm by ozgur84 Reason: 1



You know, you are allowed to use the Verify function even without an Arduino connected.

Reading the second switch would be a good idea. Grouping all the code dealing with one switch together makes more sense than trying to mix reading one switch and acting on it with reading the other switch and acting on it.

If a switch is determined to have become pressed, it will take time to step the stepper motor. While that is happening, any bouncing that happens on that switch is likely to have died out. So, debouncing the switches is probably not necessary. Even if it is, 10 milliseconds is a more reasonable time.


Actually as a physicst I am realy new in programing with arduino code an electronics (I am more faimiliar with MATLAB). Luckily the postman brought my arduino after I had wrote the first post and I have been playing with my new toy since this morning. :) I think after half a day work and couple of trials, I completed the code. It works!!! It is a great feeling! :)

Anyway, could you explain the verify function. I really have no clue?
Another thing, what I meen with bouncing is the fast reading speeds of arduino... it reads the buttons quick and executes the next stpes so fast! for avoiding this, I wrote the delay command with 1sec after the button related steps and I think it is not a bad idea, and I like the way it waits for a while before the motor starts rotating :)

Anyway I will share the code after a this post. It would be great, if you could look at it and comment.

Regards,
Özgür

ozgur84

Ok, here is the code and it is working after half a day of work!

This is my very first arduino project and my arduino uno r3 board was delivered this morning. Although, I somehow made it worked :)

Code: [Select]


// 4 wire bipolar stepper motor driver code for L293D which controlls the direction of the motor with two push buttons for each clockwise and counter clockwise direction. After completing the predefined steps in 'motorstep' waits for the new direction command will come from one of the push buttons

const int CW = 2;
const int CCW = 3;
const int ENA = 8;
const int ENB = 13;
const int black = 9;  // IN1 pin of L293D
const int brown = 10;  // IN2 pin of L293D
const int orange = 11;  // IN3 pin of L293D
const int yellow = 12;  // IN4 pin of L293D
int valA = 0; // counter clockwise button vallue
int stateA =0; // state of the counter clockwise button
int valB = 0;  // clockwise button value
int stateB = 0; //state of the clockwise button
int pause = 10; //delay between each step for slow rotation
int motorstep = 48; // number of steps when the buttons are once pressed

void setup(){
  pinMode(black, OUTPUT);
  pinMode(brown, OUTPUT);
  pinMode(orange, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(CW, INPUT);
  pinMode(CCW, INPUT);
}

void loop(){
  valA = digitalRead(CW); //reads the CW button value and writes to 'valA'
  if(valA ==HIGH){
    stateA = 1- stateA;
    delay(1000); // to get rid of button bouncing (arduino reads the buttons really fast!!)
    stateB = 0;
    reverse(motorstep); 
  }
 
  valB = digitalRead(CCW); //reads the CCW button value and writes to 'valB'
  if (valB == HIGH){
    stateB = 1- stateB;
    delay(1000); // to get rid of button bouncing (arduino reads the buttons really fast!!)
    stateA = 0;
    forward(motorstep);   
  }
} // end loop

void reverse(int i){
  // Pin 8 Enable A Pin 13 Enable B on
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
 
  while (1)   {
    digitalWrite(black, 0);
    digitalWrite(brown, 1);
    digitalWrite(orange, 1);
    digitalWrite(yellow, 0);
    delay(pause); 
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;

    digitalWrite(black, 0);
    digitalWrite(brown, 1);
    digitalWrite(orange, 0);
    digitalWrite(yellow, 1);
    delay(pause);
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;

    digitalWrite(black, 1);
    digitalWrite(brown, 0);
    digitalWrite(orange, 0);
    digitalWrite(yellow, 1);
    delay(pause); 
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;

    digitalWrite(black, 1);
    digitalWrite(brown, 0);
    digitalWrite(orange, 1);
    digitalWrite(yellow, 0);
    delay(pause);
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;
  }

  // all outputs to stepper off
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);

}  // end reverse()



void forward(int i){

  // Pin 8 Enable A Pin 13 Enable B on
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);

  while (1){

    digitalWrite(black, 1);
    digitalWrite(brown, 0);
    digitalWrite(orange, 1);
    digitalWrite(yellow, 0);
    delay(pause);
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;

    digitalWrite(black, 1);
    digitalWrite(brown, 0);
    digitalWrite(orange, 0);
    digitalWrite(yellow, 1);
    delay(pause); 
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;

    digitalWrite(black, 0);
    digitalWrite(brown, 1);
    digitalWrite(orange, 0);
    digitalWrite(yellow, 1);
    delay(pause);
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;

    digitalWrite(black, 0);
    digitalWrite(brown, 1);
    digitalWrite(orange, 1);
    digitalWrite(yellow, 0);
    delay(pause); 
    i--; // reduces the remaining 'motorstep' value as "motorstep-1" for each step
    if (i < 1) break;
  }

  // all outputs to stepper off
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);

}  // end forward()



Cheers!
Özgür

Go Up