Hi guys,
I’ve just bought a Arduino Motor Shield and I’d like to connect a bipolar stepper motor (with 4 cables) to that board.
I want to stopand restart stepper motor with the keys of the computer keyboard ( es. a=go, b=stop).
I’ve got just the program, but I can’t continue it.
Thank you
The names you are using for your motor pins are not really appropriate because the specific pins do not control direction and neither do they use PWM. They are just 4 connections to the motor and would probably be less confusing if they were simply called motorPin1, motorPin2, motorPin3 and motorPin4.
Assuming your code works to make the motor move you haven’t got any code for detecting a button press.
In principal all you need is some code that sets a variable to true when one button is pressed and sets it to false when the other button is pressed. Then the code to run the steppers only runs if the value of the variable is true. And instead of running for myStep steps for every iteration of loop your motor should just move 1 step. That way the buttons get tested between each step.
long time,now;
/* stepmotore sequence
"Gray-kode"
0-1000 =8
1-1100 =12
2-0100 =4
3-0110 =6
4-0010 =2
5-0011 =3
6-0001 =1
7-1001 =9
*/
byte sekv[8]={8,12,4,6,2,3,1,9};
byte pointer = 0; // remember current position
char key;
#define m1 2 // m1 and m2 => coil A m3, m4 to coil B !! CHANGE pins to match
#define m2 3
#define m3 8
#define m4 9
//**************************************************************************
void setup()
{
pinMode(m1,OUTPUT);
pinMode(m2,OUTPUT);
pinMode(m3,OUTPUT);
pinMode(m4,OUTPUT);
Serial.begin(115200); // need to read command f=forware, r=reverse, any other key =stop
//
motor(0);
now=millis();
}
//**************************************************************************
void loop()
{
// get a command
if (Serial.available()) key=Serial.read(); // read 'new' commamd
switch (key)
{
case 'f': // take one step forware
{
stepforw();
break;
}
case 'r': // take a step backwards
{
stepreverse();
break;
}
}
// wait just a moment..
while((millis()-now)<30) {};
now=millis();
}
//**************************************************************************
int motor(int posisjon)
{
// this part can be written with less code
byte mm=sekv[posisjon];
// så sjekker vi enkelt de 4 i sekvens..:
if ((mm & 8)>0) digitalWrite(m1,HIGH);
else digitalWrite(m1,LOW);
if ((mm & 4)>0) digitalWrite(m2,HIGH);
else digitalWrite(m2,LOW);
if ((mm & 2)>0) digitalWrite(m3,HIGH);
else digitalWrite(m3,LOW);
if ((mm & 1)>0) digitalWrite(m4,HIGH);
else digitalWrite(m4,LOW);
}
//**************************************************************************
void stepforw() //
{
pointer++;
if (pointer>7) pointer=0;
motor(pointer);
}
//**************************************************************************
void stepreverse()
{
pointer--;
if (pointer>7) pointer=7;
motor(pointer);
}
//**************************************************************************
You’ve got the wrong controller for that motor, it needs a constant-current chopper
drive capable of 3.5A output really. The L298 can’t deliver the current needed and
that motor will only go very slowly driven from an H-bridge. The windings are
probably something like 0.5 to 1 ohm, so you’ll be wasting most of your power in
the L298 darlington stages…
Low impedance stepper motors need current-control drive, high impedance motors
can be voltage driven.
The advantage with this kind of controller, is that they allow you to run motor if smaller steps (microstepping)
and You get full control on the current flow.
I have the code changed a little…
Set jumpers to match picture.
Code may be changed to add a ‘relax’ key where enable-lines are set low. (to stop current flowing)
Your motor will get hot. Abort before reaching 70 degs.
long time,now;
/* stepmotore sequence
"Gray-kode"
0-00 =0
1-01 =1
2-11 =3
3-10 =2
*/
byte sekv[4]={0,1,3,2};
byte pointer = 0; // remember current position
char key;
#define drA 2
#define enA 3
#define drB 8
#define enB 9
//**************************************************************************
void setup()
{
pinMode(drA,OUTPUT);
pinMode(drB,OUTPUT);
pinMode(enA,OUTPUT);
pinMode(enB,OUTPUT);
// use theese
digitalWrite(enA,HIGH); //enable coil A and B
digitalWrite(enB,HIGH);
/* or this alternative for less current
analogWrite(enA,150); //enable coil A and B
analogWrite(enB,150);
*/
Serial.begin(115200); // need to read command f=forware, r=reverse, any other key =stop
//
motor(0);
now=millis();
}
//**************************************************************************
void loop()
{
// get a command
if (Serial.available()) key=Serial.read(); // read 'new' commamd
switch (key)
{
case 'f': // take one step forware
{
stepforw();
break;
}
case 'r': // take a step backwards
{
stepreverse();
break;
}
}
// wait just a moment..
while((millis()-now)<30) {};
now=millis();
}
//**************************************************************************
int motor(int posisjon)
{
// this part can be written with less code
byte mm=sekv[posisjon];
// så sjekker vi enkelt de 4 i sekvens..:
if ((mm & 2)>0) digitalWrite(drA,HIGH);
else digitalWrite(drA,LOW);
if ((mm & 1)>0) digitalWrite(drB,HIGH);
else digitalWrite(drB,LOW);
}
//**************************************************************************
void stepforw() //
{
pointer++;
if (pointer>3) pointer=0;
motor(pointer);
}
//**************************************************************************
void stepreverse()
{
pointer--;
if (pointer>3) pointer=3;
motor(pointer);
}
//**************************************************************************