合併前
遙控車:
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;
}
}
}
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//掉我就直接沒在合併上面放進去了
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;
}
}
}
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
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()
{
Serial.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(Serial.available()>0)
{
val = Serial.read();
switch(val)
{
case 'q'://測距離
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;
}
}
}
如果把
if(Serial.available()>0)
{
val = Serial.read();
switch(val)
{
case 'q': //測距離
for(int i = 0; i < 3; i++)
Serial.write(packet); //send packet to phone
break;
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;
}
}
}
會出現call of overladed write(byte [3])' is ambiguous 調用write(byte [3])'是不明確的
是不應該write嗎?作者: leo0983405269 時間: 2017-10-16 18:04 本帖最後由 leo0983405269 於 2017-10-16 18:07 編輯