|
首先,需要的哪些工具:
1、 arduino UNO R3 x1
2、 L298N電機(jī)驅(qū)動(dòng)模塊 x1
3、 HC06 藍(lán)牙模塊 x1
4、 小車底盤(pán) x1
5、 12V電源(7~9V)x1
6、 導(dǎo)線 若干
7、 藍(lán)牙遙控app
第二:L298模塊介紹
第三:App的下載
http://shouji.baidu.com/software/11629425.html
第四:HC-06模塊的介紹:
說(shuō)一下具體電路連接:
第一步:12V 18650充電電池 接到 L298N 電機(jī)驅(qū)動(dòng)模塊的 +12V 以及 GND,+5V 電源輸出給Arduino UNO R3板子供電。有些人可能說(shuō)如果電機(jī)供電以及板子供電是同一個(gè)電源,可能電機(jī)轉(zhuǎn)動(dòng)會(huì)影響到板子供電。目前我測(cè)試是沒(méi)有發(fā)現(xiàn)問(wèn)題。可能原因是本來(lái)板子耗電量也不是很大,并且12V電池本身電量還是足夠,不會(huì)出現(xiàn)供電不足問(wèn)題。
第二步:左電機(jī)連接到OUT3和OUT4,右電機(jī)連接到OUT1和OUT2
第三步:就是L298N的IN控制引腳和Arduino板子的連接。IN1連接到引腳6,IN2連接到引腳7,IN3連接到引腳4,IN4連接到引腳5.
藍(lán)牙測(cè)試代碼
功能:藍(lán)牙小車測(cè)試按鍵值程序
- 藍(lán)牙測(cè)試代碼
- 功能:藍(lán)牙小車測(cè)試按鍵值程序
- 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'){
- //前進(jìn)
- Serial.println("up");
- }else if(ch == '2'){
- //后退
- Serial.println("back");
- }else if(ch == '3'){
- //左轉(zhuǎn)
- Serial.println("left");
- }else if(ch == '4'){
- //右轉(zhuǎn)
- Serial.println("right");
- }else if(ch=='0'){
- //停車
- Serial.println("stop");
- }else{
- //其他編碼
- Serial.println(ch);
- }
- }
- }
- 藍(lán)牙小車代碼:
- #define IN1 6 // 7 6 右輪
- #define IN2 7
- #define IN3 4 // 5 4 左輪
- #define IN4 5
- #define LEFT '3' //左轉(zhuǎn)編碼
- #define RIGHT '4'//右轉(zhuǎn)編碼
- #define GO '1'//前進(jìn)編碼
- #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){
- //前進(jìn)
- go();
- }else if(ch == BACK){
- //后退
- back();
- }else if(ch == LEFT){
- //左轉(zhuǎn)
- turnLeft();
- }else if(ch == RIGHT){
- //右轉(zhuǎn)
- turnRight();
- }else if(ch=='0'){
- //停車
- stopCar();
- }
- }
- }
- void initCar(){
- //默認(rèn)全是低電平 停止?fàn)顟B(tài)
- digitalWrite(IN1,LOW);
- digitalWrite(IN2,LOW);
- digitalWrite(IN3,LOW);
- digitalWrite(IN4,LOW);
- }
- /**
- * 左轉(zhuǎn)
- */
- void turnLeft(){
- digitalWrite(IN1,HIGH);
- digitalWrite(IN2,LOW); //右輪前進(jìn)
- digitalWrite(IN3,LOW);
- digitalWrite(IN4,LOW); //左輪不動(dòng)
- }
- /**
- * 右轉(zhuǎn)
- */
- void turnRight(){
- digitalWrite(IN1,LOW);
- digitalWrite(IN2,LOW); //右輪不動(dòng)
- digitalWrite(IN3,HIGH);
- digitalWrite(IN4,LOW); //左輪前進(jìn)
- }
- /**
- * 前進(jìn)
- */
- void go(){
- digitalWrite(IN1,HIGH);
- digitalWrite(IN2,LOW); //右輪前進(jìn)
- digitalWrite(IN3,HIGH);
- digitalWrite(IN4,LOW); //左輪前進(jìn)
- }
- /**
- * 倒車
- */
- void back(){
- digitalWrite(IN1,LOW);
- digitalWrite(IN2,HIGH); //右輪后退
- digitalWrite(IN3,LOW);
- digitalWrite(IN4,HIGH); //左輪后退
- }
- /**
- * 停車
- */
- void stopCar(){
- initCar();
- }
復(fù)制代碼
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'){
//前進(jìn)
Serial.println("up");
}else if(ch == '2'){
//后退
Serial.println("back");
}else if(ch == '3'){
//左轉(zhuǎn)
Serial.println("left");
}else if(ch == '4'){
//右轉(zhuǎn)
Serial.println("right");
}else if(ch=='0'){
//停車
Serial.println("stop");
}else{
//其他編碼
Serial.println(ch);
}
}
}
|
-
-
藍(lán)牙小車.rar
2018-6-14 18:30 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
1.66 MB, 下載次數(shù): 27, 下載積分: 黑幣 -5
評(píng)分
-
查看全部評(píng)分
|