I just bought a new stepper motor ( 400 steps for revolution, 0.9°/step) and I would like make a turn of 180° by 200 steps, but it doesn't work as aspected.
It looks like the motor needs only 110 steps to make a turn of 180° and i dont know the cause.
The driver motors is the Keyes L298 . It is wired to my arduino UNO rev3.
#define negativeSteps -55
#define positiveSteps 55
#define Speed 7500
int motorPin1 = 11;
int motorPin2 = 10;
int motorPin3 = 9;
int motorPin4 = 8;
int steps;
unsigned int tmp2;
byte modeR[] = {B00001001, B00001010, B00000110, B00000101};
byte modeF[] = {B00000101, B00000110, B00001010, B00001001};
void setup() {
Serial.begin(115200); //Opens serial connection at 115200 baud.
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
pinMode(3,OUTPUT); // Enable pin motorA
pinMode(2,OUTPUT); // Enable pin motorB
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
}
void loop()
{
while (Serial.available())
{
char cmd = (char)Serial.read();
if (cmd == 's') while(1) Seek();
}
}
void motorR(){
int bitMask=B00001111; // bitmask to modify only last 4 bit
for (int id = 0; id <4; id++) {
PORTB = (PORTB &~bitMask) | modeR[id];
while (tmp2++<Speed);
tmp2=0;
}
}
void motorF(){
int bitMask=B00001111; // bitmask to modify only last 4 bit
for (int id = 0; id <4; id++) {
PORTB = (PORTB &~bitMask) | modeF[id];
while (tmp2++<Speed);
tmp2=0;
}
}
void Seek() {
steps = negativeSteps; //Init Steps
while (1)
{
while (steps<positiveSteps) // escursione da sinistra a destra ( da -passi a +passi)
{
if (steps<0) motorF(); // se proviene da sinistra e raggiunge lo zero , si sposta verso destra
else motorR();
Serial.println(steps);
steps++;
}
}
}
Did you try to do the steps at a very slow rate to begin with? Eg..... try 1 step every half a second using delay(500) function. This is to make sure that the motor is stepping correctly, and not skipping steps etc.
Looks like you merely provided details about the driver, but no mention of the make/model of the stepper motor.
That's an awful way to set the speed of the steps.
Just to get started, remove all of those and replace with a delay(Speed) and set Speed to be something like 10 or so. Remove the delay(500) at the bottom.
Just because C lets you write this (below) it's not a good idea:
The easiest and thousands times proven approach is to use a dedicated step/dir driver like the A4988 or DRV8825 which you can get from Pololu (original ones) or cheaper clones from your trusted Chinese source ...
In combination with the AccelStepper library, with @Robin2's simple stepper test code, or with another recommended stepper library you will get your motor turn in a minute without having to deal with timers, pulse width etc. -> ok, you might learn a lot with doing it on that basis, but why, if your main goal is to get your motor move and you can easily control it in speed and direction.
Thanks everyone
Becouse Arduino is an easy tool for fast prototyping your are right,
but sometimes I prefer to go into more depth and sometimes I don't