///////////////////////////////////////////////
// 程式說明: Intel Edison 循線車
// 作者: Dennis Chen 2015
// Email: sinocgtchen@gmail.com
/////////////////////////////////////////////
///三路紅外線感測器與Motoduino的腳位對應
const int SLeftLeft = 2; //左感測器輸入腳
const int SMiddle = 6; //中間感測器輸入腳
const int SRightRight = 7; //右感測器輸入腳
// 馬達與motoduino的腳位對應
const int Motor_E1 = 10; // 控制馬達1轉速 digital pin 10 of Arduino (PWM)
const int Motor_E2 = 11; // 控制馬達2轉速 digital pin 11 of Arduino (PWM)
const int Motor_M1 = 12; // 控制馬達1正反轉 digital pin 12 of Arduino
const int Motor_M2 = 13; // 控制馬達2正反轉 digital pin 13 of Arduino
// 感測器狀態值
byte byteSensorStatus=0;
int nforwardSpeed=160;
int nturnSpeed=240;
void setup() {
//set up serial communications
Serial.begin(9600);
setPwmSwizzler(3, 5, 10, 11); //設定Edison Arduino D3, D5, D10, D11為PWM輸出
// 輸出入接腳初始設定
pinMode(SLeftLeft, INPUT);
pinMode(SMiddle, INPUT);
pinMode(SRightRight, INPUT);
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(){
int nIRStatus;
//清除感測器狀態值
byteSensorStatus = 0;
// 讀取左感測器狀態值
nIRStatus = digitalRead(SLeftLeft);
if(nIRStatus == 1)
byteSensorStatus = (byteSensorStatus | (0x01 << 2));
// 讀取中間感測器狀態值
nIRStatus = digitalRead(SMiddle);
if(nIRStatus == 1)
byteSensorStatus = (byteSensorStatus | (0x01 << 1));
// 讀取右邊感測器狀態值
nIRStatus = digitalRead(SRightRight);
if(nIRStatus == 1)
byteSensorStatus = (byteSensorStatus | 0x01);
drivemotor(byteSensorStatus);
}
///////////////////////////
void drivemotor(byte nStatus)
{
switch(nStatus)
{ // 感測器 黑色:1 白色:0
case 7: // SL:1 SM:1 SR:1 //黑黑黑
motorstop(0, 0);
break;
case 6: // SL:1 SM:1 SR:0 //黑黑白
turnleft(0, nturnSpeed);
break;
case 5: // SL:1 SM:0 SR:1 //黑白黑
motorstop(0, 0);
break;
case 4: // SL:1 SM:0 SR:0 //黑白白
turnleft(0, nturnSpeed);
break;
case 3: // SL:0 SM:1 SR:1 // 白黑黑
turnright(0, nturnSpeed);
break;
case 2: // SL:0 SM:1 SR:0 // 白黑白
forward(0, nforwardSpeed);
break;
case 1: // SL:0 SM:0 SR:1 // 白白黑
turnright(0, nturnSpeed);
break;
case 0: // SL:0 SM:0 SR:0 //白白白
forward(0, nforwardSpeed);
}
}
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 turnright(byte flag, byte motorspeed)
{
Serial.println("right!");
digitalWrite( Motor_M1, LOW);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 50);
analogWrite( Motor_E2, motorspeed);
}
void turnleft(byte flag, byte motorspeed)
{
Serial.println("Left!");
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, motorspeed);
analogWrite( Motor_E2, 50);
}
///////////////////