///////////////////////////////////////////////
// 程式說明: Intel Edison 馬達測試
// 作者: Dennis Chen 2015
// Email: sinocgtchen@gmail.com
/////////////////////////////////////////////
// 馬達與motoduino的腳位對應
const int Motor_E2 = 11; // 控制馬達2轉速 digital pin 5 of Arduino (PWM)
const int Motor_E1 = 10; // 控制馬達1轉速 digital pin 6 of Arduino (PWM)
const int Motor_M1 = 12; // 控制馬達1正反轉 digital pin 7 of Arduino
const int Motor_M2 = 13; // 控制馬達2正反轉 digital pin 8 of Arduino
void setup() {
setPwmSwizzler(3, 5, 10, 11);
//set up serial communications
Serial.begin(57600);
// 輸出入接腳初始設定
pinMode(Motor_E1, OUTPUT); //設定 Motor_E1為輸出腳位
pinMode(Motor_E2, OUTPUT); //設定 Motor_E2為輸出腳位
pinMode(Motor_M1, OUTPUT); //設定 Motor_M1為輸出腳位
pinMode(Motor_M2, OUTPUT); //設定 Motor_M2為輸出腳位
}
//////////// 主程式 ////////
void loop()
{
forward(0, 255); // 前進
delay(3000); // 延遲 3 秒
motorstop(0, 0); // 停止
delay(3000);
backward(0, 255); // 後退
delay(3000);
motorstop(0, 0);
delay(3000);
right(0, 255); // 右轉
delay(3000);
motorstop(0, 0);
delay(3000);
left(0, 255); // 左轉
delay(3000);
motorstop(0, 0);
delay(5000);
}
void motorstop(byte flag, byte motorspeed)
{
Serial.println("stop!");
digitalWrite( Motor_M1, LOW);
digitalWrite( Motor_M2, LOW);
analogWrite( Motor_E1, 0);
analogWrite( Motor_E2, 0);
}
void forward(byte flag, byte motorspeed)
{
Serial.println("forward!");
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, motorspeed);
analogWrite( Motor_E2, motorspeed);
}
void backward(byte flag, byte motorspeed)
{
Serial.println("back!");
digitalWrite( Motor_M1, LOW);
digitalWrite( Motor_M2, LOW);
analogWrite( Motor_E1, motorspeed);
analogWrite( Motor_E2, motorspeed);
}
void right(byte flag, byte motorspeed)
{
Serial.println("right!");
digitalWrite( Motor_M1, LOW);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 0);
analogWrite( Motor_E2, motorspeed);
}
void left(byte flag, byte motorspeed)
{
Serial.println("left!");
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, motorspeed);
analogWrite( Motor_E2, 0);
}