// 本範例控制了 3 顆六線步進馬達
int i = 2; // 速度 2 ms 切換
// 馬達 A 腳位
int a1 = 22;
int a2 = 23;
int a3 = 24;
int a4 = 25;
// 馬達 B 腳位
int b1 = 2;
int b2 = 3;
int b3 = 4;
int b4 = 5;
// 馬達 C 腳位
int c1 = 8;
int c2 = 9;
int c3 = 10;
int c4 = 11;
void setup(){
// 初始化馬達 A
pinMode(a1, OUTPUT);
pinMode(a2, OUTPUT);
pinMode(a3, OUTPUT);
pinMode(a4, OUTPUT);
// 初始化馬達 B
pinMode(b1, OUTPUT);
pinMode(b2, OUTPUT);
pinMode(b3, OUTPUT);
pinMode(b4, OUTPUT);
// 初始化馬達 C
pinMode(c1, OUTPUT);
pinMode(c2, OUTPUT);
pinMode(c3, OUTPUT);
pinMode(c4, OUTPUT);
}
void loop(){
A(); // 控制馬達 A
B(); // 控制馬達 B
C(); // 控制馬達 C
}
void A(){
digitalWrite(a1,LOW);
digitalWrite(a2,LOW);
digitalWrite(a3,HIGH);
digitalWrite(a4,HIGH);
delay(i);
digitalWrite(a1,HIGH);
digitalWrite(a2,LOW);
digitalWrite(a3,LOW);
digitalWrite(a4,HIGH);
delay(i);
digitalWrite(a1,HIGH);
digitalWrite(a2,HIGH);
digitalWrite(a3,LOW);
digitalWrite(a4,LOW);
delay(i);
digitalWrite(a1,LOW);
digitalWrite(a2,HIGH);
digitalWrite(a3,HIGH);
digitalWrite(a4,LOW);
delay(i);
}
void B(){
digitalWrite(b1,LOW);
digitalWrite(b2,LOW);
digitalWrite(b3,HIGH);
digitalWrite(b4,HIGH);
delay(i);
digitalWrite(b1,HIGH);
digitalWrite(b2,LOW);
digitalWrite(b3,LOW);
digitalWrite(b4,HIGH);
delay(i);
digitalWrite(b1,HIGH);
digitalWrite(b2,HIGH);
digitalWrite(b3,LOW);
digitalWrite(b4,LOW);
delay(i);
digitalWrite(b1,LOW);
digitalWrite(b2,HIGH);
digitalWrite(b3,HIGH);
digitalWrite(b4,LOW);
delay(i);
}
void C(){
digitalWrite(c1,LOW);
digitalWrite(c2,LOW);
digitalWrite(c3,HIGH);
digitalWrite(c4,HIGH);
delay(i);
digitalWrite(c1,HIGH);
digitalWrite(c2,LOW);
digitalWrite(c3,LOW);
digitalWrite(c4,HIGH);
delay(i);
digitalWrite(c1,HIGH);
digitalWrite(c2,HIGH);
digitalWrite(c3,LOW);
digitalWrite(c4,LOW);
delay(i);
digitalWrite(c1,LOW);
digitalWrite(c2,HIGH);
digitalWrite(c3,HIGH);
digitalWrite(c4,LOW);
delay(i);
}
留言列表