|
首先,需要的哪些工具:
1、 arduino UNO R3 x1
2、 L298N電機驅動模塊 x1
3、 HC06 藍牙模塊 x1
4、 小車底盤 x1
5、 12V電源(7~9V)x1
6、 導線 若干
7、 藍牙遙控app
第二:L298模塊介紹
第三:App的下載
http://shouji.baidu.com/software/11629425.html
第四:HC-06模塊的介紹:
說一下具體電路連接:
第一步:12V 18650充電電池 接到 L298N 電機驅動模塊的 +12V 以及 GND,+5V 電源輸出給Arduino UNO R3板子供電。有些人可能說如果電機供電以及板子供電是同一個電源,可能電機轉動會影響到板子供電。目前我測試是沒有發現問題。可能原因是本來板子耗電量也不是很大,并且12V電池本身電量還是足夠,不會出現供電不足問題。
第二步:左電機連接到OUT3和OUT4,右電機連接到OUT1和OUT2
第三步:就是L298N的IN控制引腳和Arduino板子的連接。IN1連接到引腳6,IN2連接到引腳7,IN3連接到引腳4,IN4連接到引腳5.
藍牙測試代碼
功能:藍牙小車測試按鍵值程序
- 藍牙測試代碼
- 功能:藍牙小車測試按鍵值程序
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- if(Serial.available()>0){
- char ch = Serial.read();
- if(ch == '1'){
- //前進
- Serial.println("up");
- }else if(ch == '2'){
- //后退
- Serial.println("back");
- }else if(ch == '3'){
- //左轉
- Serial.println("left");
- }else if(ch == '4'){
- //右轉
- Serial.println("right");
- }else if(ch=='0'){
- //停車
- Serial.println("stop");
- }else{
- //其他編碼
- Serial.println(ch);
- }
- }
- }
- 藍牙小車代碼:
- #define IN1 6 // 7 6 右輪
- #define IN2 7
- #define IN3 4 // 5 4 左輪
- #define IN4 5
- #define LEFT '3' //左轉編碼
- #define RIGHT '4'//右轉編碼
- #define GO '1'//前進編碼
- #define BACK '2'//后退編碼
- #define STOP '0'//停止編碼
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- pinMode(IN1,OUTPUT);
- pinMode(IN2,OUTPUT);
- pinMode(IN3,OUTPUT);
- pinMode(IN4,OUTPUT);
- initCar();
- }
- void loop() {
- // put your main code here, to run repeatedly:
- if(Serial.available()>0){
- char ch = Serial.read();
- if(ch == GO){
- //前進
- go();
- }else if(ch == BACK){
- //后退
- back();
- }else if(ch == LEFT){
- //左轉
- turnLeft();
- }else if(ch == RIGHT){
- //右轉
- turnRight();
- }else if(ch=='0'){
- //停車
- stopCar();
- }
- }
- }
- void initCar(){
- //默認全是低電平 停止狀態
- digitalWrite(IN1,LOW);
- digitalWrite(IN2,LOW);
- digitalWrite(IN3,LOW);
- digitalWrite(IN4,LOW);
- }
- /**
- * 左轉
- */
- void turnLeft(){
- digitalWrite(IN1,HIGH);
- digitalWrite(IN2,LOW); //右輪前進
- digitalWrite(IN3,LOW);
- digitalWrite(IN4,LOW); //左輪不動
- }
- /**
- * 右轉
- */
- void turnRight(){
- digitalWrite(IN1,LOW);
- digitalWrite(IN2,LOW); //右輪不動
- digitalWrite(IN3,HIGH);
- digitalWrite(IN4,LOW); //左輪前進
- }
- /**
- * 前進
- */
- void go(){
- digitalWrite(IN1,HIGH);
- digitalWrite(IN2,LOW); //右輪前進
- digitalWrite(IN3,HIGH);
- digitalWrite(IN4,LOW); //左輪前進
- }
- /**
- * 倒車
- */
- void back(){
- digitalWrite(IN1,LOW);
- digitalWrite(IN2,HIGH); //右輪后退
- digitalWrite(IN3,LOW);
- digitalWrite(IN4,HIGH); //左輪后退
- }
- /**
- * 停車
- */
- void stopCar(){
- initCar();
- }
復制代碼
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0){
char ch = Serial.read();
if(ch == '1'){
//前進
Serial.println("up");
}else if(ch == '2'){
//后退
Serial.println("back");
}else if(ch == '3'){
//左轉
Serial.println("left");
}else if(ch == '4'){
//右轉
Serial.println("right");
}else if(ch=='0'){
//停車
Serial.println("stop");
}else{
//其他編碼
Serial.println(ch);
}
}
}
|
-
-
藍牙小車.rar
2018-6-14 18:30 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
1.66 MB, 下載次數: 27, 下載積分: 黑幣 -5
評分
-
查看全部評分
|