- TT馬達+65mm 車輪 X 4
- L298N 直流馬達驅動板
- 紅外線發射LED X 3
- 紅外線居收LED X 3
- 200歐姆電阻 X 3
- 10K歐姆電阻 X 3
- 10K 可變電阻 X 1
- LM324運算放大器 X 1
- Arduino pro mini 5V/16M X 1
製作紅外線偵測障礙物電路板,三組LED紅外線發射接收分別偵測前、左、右障礙物。
線路圖如下
完成電路板
Arduino 使用PIN 5,6,9,10 PWM控制馬達轉向,PIN 11,12,13接收紅外線偵測信號
線路如如下
完成小車圖
實測影片
Arduino原始碼
const int PIN1A=6; const int PIN1B=5; const int PIN2A=9; const int PIN2B=10; const int PIN_R=11; const int PIN_F=12; const int PIN_L=13; int _speed = 150; int r,f,l; void setup() { pinMode(PIN1A, OUTPUT); pinMode(PIN1B, OUTPUT); pinMode(PIN2A, OUTPUT); pinMode(PIN2B, OUTPUT); pinMode(PIN_R, INPUT); pinMode(PIN_F, INPUT); pinMode(PIN_L, INPUT); digitalWrite(PIN_R, LOW); digitalWrite(PIN_F, LOW); digitalWrite(PIN_L, LOW); } void _stop() { analogWrite(PIN1A, 0); analogWrite(PIN1B, 0); analogWrite(PIN2A, 0); analogWrite(PIN2B, 0); } void forward() { analogWrite(PIN1A, _speed); analogWrite(PIN1B, 0); analogWrite(PIN2A, _speed); analogWrite(PIN2B, 0); } void backward() { analogWrite(PIN1A, 0); analogWrite(PIN1B, _speed); analogWrite(PIN2A, 0); analogWrite(PIN2B, _speed); } void right() { analogWrite(PIN1A, _speed); analogWrite(PIN1B, 0); analogWrite(PIN2A, 0); analogWrite(PIN2B, 0); } void left() { analogWrite(PIN1A, 0); analogWrite(PIN1B, 0); analogWrite(PIN2A, _speed); analogWrite(PIN2B, 0); } void loop() { f = digitalRead(PIN_F); r = digitalRead(PIN_R); l = digitalRead(PIN_L); // 判斷前、左、右有障礙物採取動作 if (!f && !r && !l) forward(); if (!f && !r && l) { _stop(); right(); delay(500); } if (!f && r && !l) { _stop(); left(); delay(500); } if (!f && r && l) { _stop(); backward(); delay(500); } // 前有障礙物,左右都無障礙物時,隨機採取右轉或左轉。 if (f && !r && !l) { _stop(); if(random(2)) { left(); } else { right(); } delay(500); } if (f && !r && l) { _stop(); right(); delay(500); } if (f && r && !l) { _stop(); left(); delay(500); } if (f && r && l) { _stop(); backward(); delay(500); } }
沒有留言:
張貼留言