手持脫機(jī)步進(jìn)電機(jī)控制器
簡介: 不知道各位小伙伴們常不常用步進(jìn)電機(jī),因其精確的控制性很容易實(shí)現(xiàn)步進(jìn)電機(jī)的角度和速度控制,然而也因?yàn)榇颂匦詫?dǎo)致在前期對步進(jìn)電機(jī)調(diào)試時(shí)比較繁瑣;所以結(jié)合arduino nano和A4988,我們開源一款方便測試步進(jìn)電機(jī),同時(shí)又可燒寫不同程序?qū)崿F(xiàn)移植復(fù)用的手持脫機(jī)步進(jìn)電機(jī)控制器。
材料:
接線圖: |
燒錄代碼:
int Msteppin=7;
int Mdirpin=6;
int Menpin=8;
int i=200;
int BUTTON1 = A2;
int BUTTON2 = A3;
char key;
void setup()
{
pinMode(Msteppin,OUTPUT);//驅(qū)動(dòng)
pinMode(Mdirpin,OUTPUT);//方向
pinMode(Menpin,OUTPUT);//使能
digitalWrite(Menpin,LOW);
pinMode(A2,INPUT_PULLUP);//A2上拉
pinMode(A3,INPUT_PULLUP);//A3上拉
Serial.begin(9600);
}
void a()
{digitalWrite(Mdirpin,HIGH);
int i=0;
while(i<200)
{digitalWrite(Msteppin,HIGH);
delayMicroseconds(800);
digitalWrite(Msteppin,LOW);
delayMicroseconds(800);
i++;}
}
void b()
{
digitalWrite(Mdirpin,LOW);
int i=0;
while(i<200)
{digitalWrite(Msteppin,HIGH);
delayMicroseconds(800);
digitalWrite(Msteppin,LOW);
delayMicroseconds(800);
i++;}
}
void loop()
{
while (digitalRead(BUTTON2) == LOW)
{ a();Serial.println('0'); }
while (digitalRead(BUTTON1) == LOW)
{ b();Serial.println('c'); }
int key=Serial.read();
switch(key)
{ case'0':a();delay(10);break;
case'c':b();delay(10);break;
case't':a();delay(1000);b();delay(1000);break;
default:break;
}
}
視頻展示: