我們需要 easy driver 的模組來驅動步進馬達
然後去拆一個光碟機的步進馬達
我們用 serial 輸入 1 或者 2 來讓載台移動
因為導螺桿的長度限制, 步進的計數大約在 3000 左右
easy driver 的模組需額外供電 6V ~ 24V, 一組 18650 * 2 的電池盒就夠了
int Distance = 0; // Record the number of steps we've taken
boolean up = false;
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
Serial.begin(9600);
}
void doJobUp(){
if(up){
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
Distance = Distance + 1; // record this step
// Check to see if we are at the end of our move
if (Distance == 3000){
Distance = 0;
up=false;
// Now pause for half a second
delay(500);
return;
}else{
doJobUp();
}
}else{
return;
}
}
void doJobDown(){
if(!up){
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
Distance = Distance + 1; // record this step
// Check to see if we are at the end of our move
if (Distance == 3000){
Distance = 0;
up=true;
// Now pause for half a second
delay(500);
return;
}else{
doJobDown();
}
}else{
return;
}
}
void loop() {
if (Serial.available() > 0) {
int inByte = Serial.read();
switch (inByte) {
case '1':
doJobUp();
break;
case '2':
doJobDown();
break;
}
}
}