首先要先感謝 http://engyfun.blogspot.tw/2015/02/here-is-source-code-for-our-28byj48.html 所提供的ULN2003 driver library source code
Arduino的driver header and Cpp 檔案可以放到如下之位置
路徑是在你安裝Arduino的資料夾裡的\libraries,資料夾名稱可以自己命名,完成之後便可以在Arduino裡看到我們剛剛丟進去的driver並且匯入使用了
這時候我們就可以在我們的Arduino code裡面使用#include <StepperMotor.h>
來使用我們剛剛的driver了
Arduino code 如下
主要功能為可以由console輸入需要旋轉的角度以及輸出旋轉角度,完成之後回復原狀
==================================================================
#include <StepperMotor.h>
// ULN2003 Motor Driver
StepperMotor motor(8,9,10,11);// IN1, IN2, IN3, IN4
float Speed = 2.0; // motor gets slower as this number increases
float Steps = 4076.0; // 4076 is approx 360 degrees
// maximum speed about 15RPM at 5 volts with an Arduino
float Delay = 1000.0; // delay in ms for next round
float n ; //dgree of motor
String inString = ""; // string to hold input
#define led 13 // onboard led
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
digitalWrite(led, LOW); // turn off LED
motor.setStepDuration(Speed);
}
void loop(){
// motor turn clockwise
digitalWrite(led, HIGH);
inString = "";// clean forword input
while (Serial.available() > 0) {
int inChar = Serial.read();
if (inChar != '\n') {
// As long as the incoming byte
// is not a newline,
// convert the incoming byte to a char
// and add it to the string
inString += (char)inChar;
}
n = inString.toFloat();
}
Serial.print("turn clockwise degree:");
Serial.println(n);
motor.step(Steps*n/360.0);
delay(Delay);
// reverse motor
digitalWrite(led, LOW); // shows reverse rotation
Serial.print("turn clockwise degree:");
Serial.println(-n);
motor.step(-Steps*n/360.0);
delay(Delay);
}
==================================================================
沒有留言:
張貼留言