/*
L9110馬達控制板 搖桿控制小型直流馬達實驗
實驗目的:通過L9110馬達控制板的簡單連接控制直流馬達的 方向 與 速度
實驗設計:Joystick 充當油門與前向後方向的控制,控制杆隨著方向上將速度由小至大推進
*/
const int A1A = 10; // pin 10 connected to pin AIA
const int A1B = 11; // pin 11 connected to pin AIB
byte speed1 = 0; // change this (0-255) to control the speed of the motor
void setup() {
pinMode(A1A, OUTPUT); // set pins to output
pinMode(A1B, OUTPUT); // set pins to output
Serial.begin(9600);
}
void loop() {
int x =analogRead(A0); //讀取搖桿類比信號
speed1 = map(x,0,1024,0,255);
//搖桿中立停
if( 178>speed1 && speed1>70 ){
Serial.println("stop");
stop();
}
//搖桿動作正逆轉判斷
if( speed1>150 ){
Serial.println("Forward ");
forward(); }
if( speed1<100) {
Serial.println("Backward ");
backward(); }
//輸出Arduino序列埠(Serial port)監控視窗
Serial.println(speed1);
}
void backward()
{ analogWrite(A1A, 255-speed1);
analogWrite(A1B,0 ); }
void forward()
{ analogWrite(A1A, 0);
analogWrite(A1B, speed1); }
void stop()
{ analogWrite(A1A, 0);
analogWrite(A1B, 0); }
留言
張貼留言