若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。
Arduino UNO 電路圖如下:
1. Arduino UNO R3 板子 x 1
2. 公對母杜邦線 x 4
3. 公對公杜邦線 x 11
4. 5V 直流馬達 x 2
5. 藍芽模組HC-05 x 1
6. L298N 馬達驅動板 x 1
7. 小車底盤 x 1
8. 小車輪子 x 2
9. 萬向輪 x 1
10. 緊固件 x 4
11. 長螺絲 x 4
12. 短螺絲 (長8根、短2根)
13. 螺帽 x 10
14. 行動電源 x 1
15. USB Cable線 x 1
16. 橡皮筋多條
接線對應腳位:
L298N馬達驅動板
- IN1 接 Arduino D5
- IN2 接 Arduino D6
- IN3 接 Arduino D9
- IN4 接 Arduino D10
- 12V 接 Arduino 5V
- GND 接 Arduino GND
- 5V 接 Arduino Vin
HC-05 藍芽模組
- Vcc 接 Arduino 3.3V
- TXD 接 Arduino D11
- GND 接 Arduino GND
- RXD 接 Arduino D12
Arduino 程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include <Wire.h> | |
SoftwareSerial CAR_BT(11,12); //定義 PIN11 及 PIN12 分別為 RX 及 TX 腳位 | |
/* Input for leftMotor: | |
IN1 IN2 Action | |
LOW LOW Stop | |
HIGH LOW Forward | |
LOW HIGH Backward | |
HIGH HIGH Stop | |
*/ | |
const int leftMotorIn1 = 5; | |
const int leftMotorIn2 = 6; | |
/* Input for rightMotor: | |
IN3 IN4 Action | |
LOW LOW Stop | |
HIGH LOW Forward | |
LOW HIGH Backward | |
HIGH HIGH Stop | |
*/ | |
const int rightMotorIn3 = 9; | |
const int rightMotorIn4 = 10; | |
bool isForward = true; | |
void setup() | |
{ | |
pinMode(leftMotorIn1, OUTPUT); | |
pinMode(leftMotorIn2, OUTPUT); | |
pinMode(rightMotorIn3, OUTPUT); | |
pinMode(rightMotorIn4, OUTPUT); | |
Serial.begin(9600); //Arduino起始鮑率:9600 | |
CAR_BT.begin(9600); //藍牙鮑率:9600 注意!每個藍牙晶片的鮑率都不太一樣,請務必確認 | |
} | |
void loop() | |
{ | |
int inSize; | |
char input; | |
if( (inSize = (CAR_BT.available())) > 0) { //讀取藍牙訊息 | |
Serial.print("size = "); | |
Serial.println(inSize); | |
Serial.print("Input = "); | |
Serial.println(input=(char)CAR_BT.read()); | |
if( input == 'f' ) { | |
forward(); | |
isForward = true; | |
} | |
if( input == 'b' ) { | |
backward(); | |
isForward = false; | |
} | |
if( input == 'l' ) { | |
left(); | |
} | |
if( input == 'r' ) { | |
right(); | |
} | |
if( input == 's' ) { | |
motorstop(); | |
} | |
} | |
} | |
void motorstop() | |
{ | |
digitalWrite(leftMotorIn1, LOW); | |
digitalWrite(leftMotorIn2, LOW); | |
digitalWrite(rightMotorIn3, LOW); | |
digitalWrite(rightMotorIn4, LOW); | |
} | |
void forward() | |
{ | |
digitalWrite(leftMotorIn1, HIGH); | |
digitalWrite(leftMotorIn2, LOW); | |
digitalWrite(rightMotorIn3, HIGH); | |
digitalWrite(rightMotorIn4, LOW); | |
} | |
void backward() | |
{ | |
digitalWrite(leftMotorIn1, LOW); | |
digitalWrite(leftMotorIn2, HIGH); | |
digitalWrite(rightMotorIn3, LOW); | |
digitalWrite(rightMotorIn4, HIGH); | |
} | |
void right() | |
{ | |
if( isForward ) { | |
digitalWrite(leftMotorIn1, HIGH); | |
digitalWrite(leftMotorIn2, LOW); | |
digitalWrite(rightMotorIn3, LOW); | |
digitalWrite(rightMotorIn4, LOW); | |
} else { | |
digitalWrite(leftMotorIn1, LOW); | |
digitalWrite(leftMotorIn2, LOW); | |
digitalWrite(rightMotorIn3, LOW); | |
digitalWrite(rightMotorIn4, HIGH); | |
} | |
} | |
void left() | |
{ | |
if( isForward ) { | |
digitalWrite(leftMotorIn1, LOW); | |
digitalWrite(leftMotorIn2, LOW); | |
digitalWrite(rightMotorIn3, HIGH); | |
digitalWrite(rightMotorIn4, LOW); | |
} else { | |
digitalWrite(leftMotorIn1, LOW); | |
digitalWrite(leftMotorIn2, HIGH); | |
digitalWrite(rightMotorIn3, LOW); | |
digitalWrite(rightMotorIn4, LOW); | |
} | |
} |
示範影片:
沒有留言:
張貼留言