|
本帖最後由 leo0983405269 於 2017-10-11 18:03 編輯
合併前
遙控車:
const int Motor_E2 = 11;
const int Motor_E1 = 10;
const int Motor_M1 = 12;
const int Motor_M2 = 13;
char val; // variable to receive data from the serial port(bluetooth)
void setup()
{
// Start serial communication at 9600
Serial.begin(9600);
pinMode(Motor_M1, OUTPUT);
pinMode(Motor_M2, OUTPUT);
}
void loop()
{
if(Serial.available()>0)
{
val = Serial.read();
switch(val)
{
case 'f': // car forward
forward(0, 255);
break;
case 'd': // car backward
backward(0, 255);
break;
case 'l': // car turn left
left(0, 255);
break;
case 'r': // car turn right
right(0, 255);
break;
case 's': // car stop
motorstop(0, 0);
break;
}
}
}
void motorstop(byte flag, byte motorspeed)
{
Serial.println("Stop!");
digitalWrite( Motor_E1, motorspeed);
digitalWrite( Motor_E2, motorspeed);
}
void forward(byte flag, byte motorspeed)
{
Serial.println("Forward!");
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 200);
analogWrite( Motor_E2, 200);
}
void backward(byte flag, byte motorspeed)
{
Serial.println("Backward!");
digitalWrite( Motor_M1, LOW);
digitalWrite( Motor_M2, LOW);
analogWrite( Motor_E1, 200);
analogWrite( Motor_E2, 200);
}
void right(byte flag, byte motorspeed)
{
Serial.println("Turn Right! ");
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 50);
analogWrite( Motor_E2, 0);
}
void left(byte flag, byte motorspeed)
{
Serial.println("Turn Left!");
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 0);
analogWrite( Motor_E2, 50);
}
超音波:
#include <SoftwareSerial.h> //Arduino I2C library
const int trig = 8; //define Arduino pin
const int echo = 7;
const int TX = 1;
const int RX = 0;
const int delay_time = 1000; //delay 1000 ms for every measurment
SoftwareSerial BT(RX, TX);
//set Bluetooth RX (-> pin TX of Bluetooth Module), TX (-> pin RX of Bluetooth Module)
void setup() {
//Serial.begin(9600); //set baud rate of Serial Monitor
BT.begin(9600); //set baud rate of Bluetooth
pinMode(trig, OUTPUT); //set trigger pin to OUTPUT (-> pin trig of Ultrasonic Sensor)
pinMode(echo, INPUT); //set echo pin to INPUT (-> pin echo of Ultrasonic Sensor)
//Serial.println("Start!!!");
}
void loop() {
float duration; //duration to record the time of every back and forth
float distance;
digitalWrite(trig, HIGH); //trigger sona for 1 ms
delayMicroseconds(1000);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
//use pulseIn() function to get the time of pin echo from HIGH to LOW, unit=microseconds
distance = (duration / 2) * 0.034;
//distance = ( half of time of back and forth )x( wave velocity(use 0.034 cm per microsecond) )
//Serial.print("Distance ="); //show result on Serial Monitor
//Serial.print(distance);
//Serial.println("cm");
delay(delay_time);
int sendData = (int) (distance * 100); //times 100 and convert disance to integer
byte packet[3];
packet[0] = 97; //key send to phone
packet[1] = sendData / 256; //divides sendData to two 1 byte packets
packet[2] = sendData % 256;
if(BT.available() > 0) //check BT is succeed
if(BT.read() == 97) //check recieve key from phone
{
Serial.println("succeed!");
for(int i = 0; i < 3; i++)
BT.write(packet); //send packet to phone
}
}
上次新手大大提供把serial//掉我就直接沒在合併上面放進去了
合併後
#include <SoftwareSerial.h> //Arduino I2C library
const int trig = 8; //define Arduino pin
const int echo = 7;
const int TX = 1;
const int RX = 0;
const int delay_time = 1000; //delay 1000 ms for every measurment
SoftwareSerial BT(RX, TX);
//set Bluetooth RX (-> pin TX of Bluetooth Module), TX (-> pin RX of Bluetooth Module)
const int Motor_E2 = 11;
const int Motor_E1 = 10;
const int Motor_M1 = 12;
const int Motor_M2 = 13;
char val; // variable to receive data from the serial port(bluetooth)
void setup()
{
// Start serial communication at 9600
BT.begin(9600); //set baud rate of Bluetooth
pinMode(Motor_M1, OUTPUT);
pinMode(Motor_M2, OUTPUT);
pinMode(trig, OUTPUT); //set trigger pin to OUTPUT (-> pin trig of Ultrasonic Sensor)
pinMode(echo, INPUT); //set echo pin to INPUT (-> pin echo of Ultrasonic Sensor)
}
void loop()
{
float duration; //duration to record the time of every back and forth
float distance;
digitalWrite(trig, HIGH); //trigger sona for 1 ms
delayMicroseconds(1000);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
//use pulseIn() function to get the time of pin echo from HIGH to LOW, unit=microseconds
distance = (duration / 2) * 0.034;
//distance = ( half of time of back and forth )x( wave velocity(use 0.034 cm per microsecond) )
delay(1000);
int sendData = (int) (distance * 100); //times 100 and convert disance to integer
byte packet[3];
packet[0] = 97; //key send to phone
packet[1] = sendData / 256; //divides sendData to two 1 byte packets
packet[2] = sendData % 256;
if(BT.available() > 0) //check BT is succeed
if(BT.read() == 97) //check recieve key from phone
{
for(int i = 0; i < 3; i++)
BT.write(packet); //send packet to phone
}
if(BT.available()>0)
{
val = BT.read();
switch(val)
{
case 'f': // car forward
forward(0, 255);
break;
case 'd': // car backward
backward(0, 255);
break;
case 'l': // car turn left
left(0, 255);
break;
case 'r': // car turn right
right(0, 255);
break;
case 's': // car stop
motorstop(0, 0);
break;
}
}
}
void motorstop(byte flag, byte motorspeed)
{
Serial.println("Stop!");
digitalWrite( Motor_E1, motorspeed);
digitalWrite( Motor_E2, motorspeed);
}
void forward(byte flag, byte motorspeed)
{
Serial.println("Forward!");
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 200);
analogWrite( Motor_E2, 200);
}
void backward(byte flag, byte motorspeed)
{
Serial.println("Backward!");
digitalWrite( Motor_M1, LOW);
digitalWrite( Motor_M2, LOW);
analogWrite( Motor_E1, 200);
analogWrite( Motor_E2, 200);
}
void right(byte flag, byte motorspeed)
{
Serial.println("Turn Right! ");
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 50);
analogWrite( Motor_E2, 0);
}
void left(byte flag, byte motorspeed)
{
Serial.println("Turn Left!");
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 0);
analogWrite( Motor_E2, 50);
}
請問是哪裡合錯了,兩個功能都出不來
編譯過是沒有錯誤
我現在有點擔心因為手機控制遙控車是 手機對車子 超音波的值是模組對手機
這樣會不會衝突導致沒辦法成功
還是我手機程式沒寫好,不好意思傷害幫忙的人的眼睛了。
如果有單獨遙控車或超音波的問題可以私訊我,我能力範圍內大家一起討論感謝
|
|