Hallo,
ich habe einen Schrittmotor der ausgezeichnet functioniert mit das L298N und dieses Program:
#include <Stepper.h>
const int stepsPerRevolution = 48; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int long pos;
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
//myStepper.step(1);
}
void loop() {
pos = random(12, 20);
Serial.println(pos*2);
while (pos>0)
{
myStepper.step(pos*2);
pos = pos - 1 ;
}
delay(5000);
}
Aber jetzt habe ich bei Conrad das Motorshield von Velleman VMA03 gekauft. Ich habe fast alle Beispiele aus dem internet probiert, der Motor schuttelt nur oder summt.
Zum Beispiel, bei dieses Programm summt es ganz schlimm:
// Initializing pins
const int pwr_a = 3;
const int pwr_b = 9;
const int dir_a = 2;
const int dir_b = 8;
const int STEPS = 2*200; //total count of halfsteps per full rotation (*2 for half steps!)
// e.g. 1.8deg stepper => 200 steps => 400 halfsteps
int currStep = 0; //current step, corresponds to angular position, limits: 0...STEPS
int sub = 0; //current halfstep within repeating sequence (8 halfsteps), limits: 0...7
int stepdelay_fast = 1000; //Wait time between steps (microseconds) - fast drive
int stepdelay_slow = 4000; //Wait time between steps (microseconds) - slow drive
void setup()
{
// Initialize the pins, in the correct type of mode.
pinMode(pwr_a,OUTPUT);
pinMode(pwr_b,OUTPUT);
pinMode(dir_a,OUTPUT);
pinMode(dir_b,OUTPUT);
}
void loop()
{
subStep(2, stepdelay_fast); //50 halfsteps forward, slow
/* delay(1000); //wait 1 second
subStep(-50, stepdelay_slow); //50 halfsteps backward, slow
delay(1000); //wait 1 second
subStep(400, stepdelay_fast); //400 halfsteps forward, fast
delay(1000); //wait 1 second
subStep(-400, stepdelay_fast); //400 halfsteps backward, slow
TurnOfMotors(); //turn off motor current
delay(10000); //wait 10 seconds
*/}
// This method is called in order to make the stepper motor make a number of sub steps (depending on your wiring).
// Variable steps is for number of steps (forwards = positive, backwards = negative)
// stepDelay is for waiting time between steps in microseconds => bigger means lower speed, smaller means higher speed
void subStep(long steps, int stepDelay){
// The function will run for the amount of times called in the method.
// This is accomplished by a while loop, where it will subtract 1 from the amount after every run (forwards).
// In case of backward rotation it will add 1 to the negative number of steps until 0 is reached.
while(steps!=0)
{
if(steps>0){currStep++;} //increment current halfstep (forward)
if(steps<0){currStep--;} //decrement current halfstep (backward)
if(currStep>STEPS){currStep= currStep-STEPS;} //position >360deg is reached => set position one turn back
if(currStep<0){currStep= currStep+STEPS;} //position <0deg is reached => set position one turn forward
sub = currStep%8; //determine the next halfstep
switch(sub)
{
case 0:
// Starting position (if repeated, ful step (4))
// EXPLINATION: in this case, both our power are high.
// Therefore both coils are activated, with their standard polarities for their magnetic fields.
digitalWrite(pwr_a,HIGH);
digitalWrite(pwr_b,HIGH);
digitalWrite(dir_a,HIGH);
digitalWrite(dir_b,HIGH);
break;
case 1:
//Half step (½)
// EXPLINATION: In this case, only out b-coil is active, still with it's stand polarity.
digitalWrite(pwr_a,HIGH);
digitalWrite(pwr_b,LOW);
digitalWrite(dir_a,HIGH);
digitalWrite(dir_b,LOW);
break;
case 2:
//Full step (1)
// EXPLINATION: In this case, the b-coil is activated as in previous cases.
// But the a-coil now has it's direction turned on. So now it's active, but with the reversered polarity.
// By continuing this pattern (for reference: http://www.8051projects.net/stepper-motor-interfacing/full-step.gif) , you'll get the axis to turn.
digitalWrite(pwr_a,HIGH);
digitalWrite(pwr_b,HIGH);
digitalWrite(dir_a,HIGH);
digitalWrite(dir_b,LOW);
break;
case 3:
// Half step (1½)
digitalWrite(pwr_a,LOW);
digitalWrite(pwr_b,HIGH);
digitalWrite(dir_a,LOW);
digitalWrite(dir_b,LOW);
break;
case 4:
// Full step (2)
digitalWrite(pwr_a,HIGH);
digitalWrite(pwr_b,HIGH);
digitalWrite(dir_a,LOW);
digitalWrite(dir_b,LOW);
break;
case 5:
// Half step (2½)
digitalWrite(pwr_a,HIGH);
digitalWrite(pwr_b,LOW);
digitalWrite(dir_a,LOW);
digitalWrite(dir_b,LOW);
break;
case 6:
// Full step (3)
digitalWrite(pwr_a,HIGH);
digitalWrite(pwr_b,HIGH);
digitalWrite(dir_a,LOW);
digitalWrite(dir_b,HIGH);
break;
case 7:
// Half step (3½)
digitalWrite(pwr_a,LOW);
digitalWrite(pwr_b,HIGH);
digitalWrite(dir_a,LOW);
digitalWrite(dir_b,HIGH);
break;
}
delayMicroseconds(stepDelay); //Waiting time to next halfstep
if(steps>0){steps--;} //decrement of remaining halfsteps of forward rotation
if(steps<0){steps++;} //increment of remaining halfsteps of backward rotation
}
}
// This method simply just turn of the motors, called when ever we don't need the motors anymore.
// In this way, we won't fray the circuit or coils.
// Note 1: Motor has no torque anymore
// Note 2: If current position is between full steps, motor will move for a half step!
void TurnOfMotors(){
digitalWrite(pwr_a,LOW);
digitalWrite(pwr_b,LOW);
digitalWrite(dir_a,LOW);
digitalWrite(dir_b,LOW);
}
[\code]
Was mache ich falsh?
Bert