任何的實作都是一點一滴湊出來的,主要方向如下:
1.使用appInvetor建置藍芽控制APP。
2.Arduino及藍芽模組接線。
3.程式碼實作以上1~2的操作,測試APP定義的訊號(字元)在發送後,在Arduino上看到的是否一致。
4.如以上三個步驟完成,接著就是腳位及馬達正反轉的控制。
5.不同的L298N版子在供電方面要看規格,我自己用的版子是吃5V電,但接腳是要同時把5V及12V同時接上。起初只接L298N上的5V角位時,馬達驅動不了,讓我誤以為L298N是壞的,直到再併12V腳位,就正常了。
--------------------------------------------------------------------------------------
//L298N的版子,要把5V及12V串再一起供電
#include <SoftwareSerial.h> // 引用程式庫
// 定義連接藍牙模組的序列埠
SoftwareSerial BT(10, 11 ); // 接收腳, 傳送腳
//以下腳位實作時可再自行調整,因為要做調速,採用類比腳位
//whell 1
const int motorIn1 = 3; //Analog
const int motorIn2 = 5; //Analog
//whell 2
const int motorIn3 = 6; //Analog
const int motorIn4 = 9; //Analog
char SpeedAnDi='I'; //馬達轉速,採用數位方式I,採用類比N
//預設I,因為太低時推不動減速機構,至少都要200以上
//,此時跟用數位方式控制差不多了
char oldSpeedAnDi=' ';
//方向及速度
int carSpeed;
int oldcarSpeed;
char director='S';
char olddirector=' ';
//接收內容及處理
int i = 0; //loop
int insize=0; //bt接收到的長度
char cmd[10]; //存放個別接收的char
char c=' ';
void setup() {
carSpeed=150;
Serial.begin(9600); // 與電腦序列埠連線
//Serial.println("BT is ready! ok");
// 設定藍牙模組的連線速率
// 如果是HC-05,請改成38400 -->9600
BT.begin(9600);
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(motorIn3, OUTPUT);
pinMode(motorIn4, OUTPUT);
}
void loop() {
int cnt = 0;
insize=BT.available();
while ( BT.available())//可接受 1~3個字
{
for (i = 0; i < insize; i++)
cmd[i] = (char)BT.read();
cmd[i]='\0';
Serial.println(cmd);
if(i==1) //只有一個控制字元,也就是 cmd[0]及cmd[1]='\0'
c=cmd[0];
else
{
carSpeed=atoi(cmd);
if(carSpeed<150 || carSpeed>250)
carSpeed=oldcarSpeed;
Serial.print("Input speed:");
Serial.println(carSpeed);
} //if..else
}//while
// Serial.print("i=");
// Serial.println(i);
switch (c)
{
case 'I':
SpeedAnDi='I';
break;
case 'N':
SpeedAnDi='N';
break;
case 'U': //方向
director='U';
break;
case 'D': //方向
director='D';
break;
case 'S': //方向
director='S';
//carSpeed=0;
break;
case 'R': //方向
director='R';
break;
case 'L': //方向
director='L';
break;
case 'A':
carSpeed+=10;
if(carSpeed>250)
carSpeed=250;
break;
case 'E':
carSpeed-=10;
if(carSpeed<150)
carSpeed=150;
break;
default :
Serial.print(c);
delay(200);
break;
} //SWITCH
c=' ';
//if director or rate is change to show new info
//有異動的情況才印
if(olddirector != director || oldcarSpeed != carSpeed || oldSpeedAnDi != SpeedAnDi )
{
Serial.print("DIRECTOR:");
Serial.println(director);
Serial.print("Speed:");
Serial.println(carSpeed);
Serial.print("SpeedAn-Di:");
Serial.println(SpeedAnDi);
oldSpeedAnDi=SpeedAnDi;
olddirector=director;
oldcarSpeed=carSpeed;
//有異動的情況下才跑Control重新設定
control();
}
} //LOOP
void control()
{
Serial.print("In Control:");
Serial.println(director);
switch (director)
{
case 'U': //UP //數位控制只要有值就視為真,速度都是固定
if(SpeedAnDi=='I') //digital
{
digitalWrite(motorIn1,5);
digitalWrite(motorIn2,0);
digitalWrite(motorIn3,5);
digitalWrite(motorIn4,0);
}
else //analog
{
analogWrite(motorIn1,carSpeed);
analogWrite(motorIn2,0);
analogWrite(motorIn3,carSpeed);
analogWrite(motorIn4,0);
}
break;
case 'D': //DOWN
if(SpeedAnDi=='I') //digital
{
digitalWrite(motorIn1,0);
digitalWrite(motorIn2,5);
digitalWrite(motorIn3,0);
digitalWrite(motorIn4,5);
}
else //analog
{
analogWrite(motorIn1,0);
analogWrite(motorIn2,carSpeed);
analogWrite(motorIn3,0);
analogWrite(motorIn4,carSpeed);
}
break;
case 'R': //RIGHT
if(SpeedAnDi=='I') //digital
{
digitalWrite(motorIn1,0);
digitalWrite(motorIn2,5);
digitalWrite(motorIn3,5);
digitalWrite(motorIn4,0);
}
else //analog
{
analogWrite(motorIn1,0);
analogWrite(motorIn2,carSpeed);
analogWrite(motorIn3,carSpeed);
analogWrite(motorIn4,0);
}
break;
case 'L': //LEFT
if(SpeedAnDi=='I') //digital
{
digitalWrite(motorIn1,5);
digitalWrite(motorIn2,0);
digitalWrite(motorIn3,0);
digitalWrite(motorIn4,5);
}
else //analog
{
analogWrite(motorIn1,carSpeed);
analogWrite(motorIn2,0);
analogWrite(motorIn3,0);
analogWrite(motorIn4,carSpeed);
}
break;
case 'S': //STOP
digitalWrite(motorIn1,0);
digitalWrite(motorIn2,0);
digitalWrite(motorIn3,0);
digitalWrite(motorIn4,0);
break;
}
}
沒有留言:
張貼留言