之前忘記POST這篇DIY的過程給大家分享,現在利用一點時間說明一下, 這次使用 Motoduino 來做循線車是很容易的一件事, 只要加一個 Tracker Sensor(IR Sensor) 及修改Arduino程式即完成.
DIY材料:
1.Motoduino 或 (Arduino Duemilanove + Motor shield)
2.Tracker Sensor (IR Sensor)
3.小車本體
4. 9V battery
Sketch(Arduino程式):
// Sensor sequence: SLeftLeft SMiddle SRightRight
const int SLeftLeft = 9; //左感測器輸入腳
const int SMiddle = 10; //中間感測器輸入腳
const int SRightRight = 11; //右感測器輸入腳
// variables will change:
int SLL; //左感測器狀態
int SM; //中感測器狀態
int SRR; //右感測器狀態
const int Motor_M1 = 7;
const int Motor_M2 = 8;
const int Motor_E1 = 5;
const int Motor_E2 = 6;
byte byteSensorStatus=0;
#define SENSOR_L 4;
#define SENSOR_M 2;
#define SENSOR_R 1;
void setup() {
//set up serial communications
Serial.begin(9600);
// 輸出入接腳初始設定
pinMode(SLeftLeft, INPUT);
pinMode(SMiddle, INPUT);
pinMode(SRightRight, INPUT);
pinMode(Motor_M1, OUTPUT);
pinMode(Motor_M2, OUTPUT);
}
void loop(){
byteSensorStatus = 0;
// 讀取感測器狀態值
SLL = digitalRead(SLeftLeft);
if(SLL == 1)
byteSensorStatus = (byteSensorStatus | (0x01 << 2));
SM = digitalRead(SMiddle);
if(SM == 1)
byteSensorStatus = (byteSensorStatus | (0x01 << 1));
SRR = digitalRead(SRightRight);
if(SRR == 1)
byteSensorStatus = (byteSensorStatus | 0x01);
// Serial.println(byteSensorStatus, HEX);
switch(byteSensorStatus)
{ // 0:
case 0: // SL:0 SM:0 SR:0
motorstop(0,0);
break;
case 1: // SL:0 SM:0 SR:1
left(0,0);
break;
case 2: // SL:0 SM:1 SR:0
motorstop(0,0);
break;
case 3: // SL:0 SM:1 SR:1
left(0,0);
break;
case 4: // SL:1 SM:0 SR:0
right(0,0);
break;
case 5: // SL:1 SM:0 SR:1
forward(0,0);
break;
case 6: // SL:1 SM:1 SR:0
right(0,0);
break;
case 7: // SL:1 SM:1 SR:1
forward(0,0);
}
}
void motorstop(byte flag, byte numOfValues)
{
digitalWrite( Motor_E1, 0);
digitalWrite( Motor_E2, 0);
Serial.println("stop : ");
}
void forward(byte flag, byte numOfValues)
{
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 255);
analogWrite( Motor_E2, 255);
}
void back(byte flag, byte numOfValues)
{
digitalWrite( Motor_M1, LOW);
digitalWrite( Motor_M2, LOW);
analogWrite( Motor_E1, 255);
analogWrite( Motor_E2, 255);
}
void right(byte flag, byte numOfValues)
{
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 255);
analogWrite( Motor_E2, 0);
}
void left(byte flag, byte numOfValues)
{
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 0);
analogWrite( Motor_E2, 255);
}
The video demonstration: http://www.youtube.com/watch?v=NkF-v1hor-U&list=UU6aKpj71jLKYABYLr3Wbpuw&index=2&feature=plcp
Motoduino information: http://motoduino.com
My Email: sinocgtchen@gmail.com
沒有留言:
張貼留言