I’m trying to control a stepper motor (42shdc3025-24b) NEMA17, but it seems to vibrate back and forth. Im trying to use my own class/library system so that i understand its functions better but havn’t managed to get anywhere with it.
Blockquote
#include “mbed.h”
Serial pc(USBTX, USBRX);
class MyStepper
{
public:
MyStepper(PinName APin, PinName BPin, PinName CPin, PinName DPin);
void write(int A, int B, int C, int D);
private:
DigitalOut _APin;
DigitalOut _BPin;
DigitalOut _CPin;
DigitalOut _DPin;
};
MyStepper::MyStepper(PinName APin, PinName BPin, PinName CPin, PinName DPin)
: _APin(APin), _BPin(BPin), _CPin(CPin), _DPin(DPin)
{
}
void MyStepper::write(int A, int B, int C, int D)
{
_APin = A;
_BPin = B;
_CPin = C;
_DPin = D;
}
MyStepper stepper1(p21,p22,p23,p24);
int LoopState = 1;
int StepTime = 0.5;
int main()
{
pc.printf(“connected!\n”);
stepper1.write(0,0,0,0);
while(true) {
switch(LoopState) {
case 1:
pc.printf("Case 1");
stepper1.write(1,0,0,0);
wait(StepTime);
LoopState+=1;
break;
case 2:
pc.printf("Case 2");
stepper1.write(0,1,0,0);
wait(StepTime);
LoopState+=1;
break;
case 3:
pc.printf("Case 3");
stepper1.write(0,0,1,0);
wait(StepTime);
LoopState +=1;
break;
case 4:
pc.printf("Case 4");
stepper1.write(0,0,0,1);
wait(StepTime);
LoopState=1;
break;
}
}
}