本帖最後由 as09651 於 2017-12-17 14:26 編輯
大家好 小的第一次接觸arduino
這邊有幾組程式碼分別是1.原本藍芽遙控車的程式碼2.欲裝上的紅外線模組的程式碼3.結合過的程式碼
1.原本藍芽遙控車的程式碼
#include <AFMotor.h>
#include <SoftwareSerial.h>
#define MOTOR_GO_FORWARD { motor1.run(FORWARD);motor2.run(FORWARD);motor3.run(FORWARD);motor4.run(FORWARD); }
#define MOTOR_GO_BACK { motor1.run(BACKWARD);motor2.run(BACKWARD);motor3.run(BACKWARD);motor4.run(BACKWARD); }
#define MOTOR_GO_LEFT { motor1.run(FORWARD);motor2.run(FORWARD);motor3.run(BACKWARD);motor4.run(BACKWARD); }
#define MOTOR_GO_RIGHT { motor1.run(BACKWARD);motor2.run(BACKWARD);motor3.run(FORWARD);motor4.run(FORWARD); }
#define MOTOR_GO_STOP { motor1.run(RELEASE);motor2.run(RELEASE);motor3.run(RELEASE);motor4.run(RELEASE); }
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
AF_DCMotor motor3(3, MOTOR12_64KHZ);
AF_DCMotor motor4(4, MOTOR12_64KHZ);
SoftwareSerial BTSerial(9, 10); //RX | TX 接 bt 的 RX
void setup()
{
Serial.begin(9600); //串口波特率设置为9600 bps
BTSerial.begin(9600); //鮑率要對,不然不會動
motor1.setSpeed(220);
motor2.setSpeed(220);
motor3.setSpeed(220);
motor4.setSpeed(220);
MOTOR_GO_STOP;
}
void loop() {
if (BTSerial.available())
{
if (BTSerial.available())
{
char command = (char)BTSerial.read();
Serial.println(command);
if (command == 'F')
{
MOTOR_GO_FORWARD;
}
else if (command == 'B')
{
MOTOR_GO_BACK;
}
else if (command == 'S')
{
MOTOR_GO_STOP;
}
else if (command == 'L')
{
MOTOR_GO_LEFT;
}
else if (command == 'R')
{
MOTOR_GO_RIGHT;
}
else
{
MOTOR_GO_STOP;
}
}
}
}
2.欲裝上的紅外線模組的程式碼
#include "math.h"
char GP2D12;
void setup()
{
Serial.begin(19200);
}
void loop()
{
GP2D12=read_gp2d12_range(0);
if(GP2D12<80 && GP2D12>10)
{
Serial.print(GP2D12,DEC);
Serial.println("cm");
}
delay(50);
}
float read_gp2d12_range(byte pin)
{
int tmp;
tmp = analogRead(pin);
if (tmp < 3)return -1;
return (6787.0 /((float)tmp - 3.0)) - 4.0;
}
3.結合過的程式碼
#include <AFMotor.h>
#include <SoftwareSerial.h>
#include <math.h>
#define MOTOR_GO_FORWARD { motor1.run(FORWARD);motor2.run(FORWARD);motor3.run(FORWARD);motor4.run(FORWARD); }
#define MOTOR_GO_BACK { motor1.run(BACKWARD);motor2.run(BACKWARD);motor3.run(BACKWARD);motor4.run(BACKWARD); }
#define MOTOR_GO_LEFT { motor1.run(FORWARD);motor2.run(FORWARD);motor3.run(BACKWARD);motor4.run(BACKWARD); }
#define MOTOR_GO_RIGHT { motor1.run(BACKWARD);motor2.run(BACKWARD);motor3.run(FORWARD);motor4.run(FORWARD); }
#define MOTOR_GO_STOP { motor1.run(RELEASE);motor2.run(RELEASE);motor3.run(RELEASE);motor4.run(RELEASE); }
char GP2D12;
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
AF_DCMotor motor3(3, MOTOR12_64KHZ);
AF_DCMotor motor4(4, MOTOR12_64KHZ);
SoftwareSerial BTSerial(9, 10); //RX | TX 接 bt 的 RX
float read_gp2d12_range(byte pin)
{
int tmp;
tmp = analogRead(pin);
if (tmp < 3)return -1;
return (6787.0 /((float)tmp - 3.0)) - 4.0;
}
void setup()
{
Serial.begin(9600); //串口波特率设置为9600 bps
BTSerial.begin(9600); //鮑率要對,不然不會動
motor1.setSpeed(220);
motor2.setSpeed(220);
motor3.setSpeed(220);
motor4.setSpeed(220);
MOTOR_GO_STOP;
}
void loop() {
if (BTSerial.available())
{
if (BTSerial.available())
{
char command = (char)BTSerial.read();
GP2D12=read_gp2d12_range(0);
if(GP2D12<80 && GP2D12>10) //區間?
{
Serial.print(GP2D12,DEC);
Serial.println("cm");
return;
}
switch(command){
case 'F':
MOTOR_GO_FORWARD;
break;
case 'B':
MOTOR_GO_BACK;
break;
case 'S':
MOTOR_GO_STOP;
break;
case 'L':
MOTOR_GO_LEFT;
break;
case 'R':
MOTOR_GO_RIGHT;
break;
default:
Serial.println("Invalid Command: "+command);
MOTOR_GO_STOP ;
}
}
}
}
現在遇到的問題就是,紅外線模組感測到東西後會切斷藍芽連線,導致遇到障礙物後車子反而會不受控制繼續執行前面的動作
而不是預期中的MOTOR_GO_STOP全部馬達停止的指令
請問是結合後的程式哪裡有問題呢? 望各位大俠指教 |