- 最後登錄
- 2023-5-26
- 在線時間
- 3783 小時
- 註冊時間
- 2011-9-22
- 閱讀權限
- 50
- 精華
- 0
- UID
- 10339933
- 帖子
- 7565
- 積分
- 4321 點
- 潛水值
- 34960 米
| 本帖最後由 chevylin0802 於 2017-5-4 10:30 AM 編輯
- #include <SoftwareSerial.h>
- #include <Wire.h>
- #include <stdio.h>
- int enablePin = 6;
- int motorPin1 = 3;
- int motorPin2 = 5;
- int trig = 9 , echo = 8 ;
- int beep = 12;
- char x;
- byte dis[6]; //distance package
- SoftwareSerial BT(10, 11); // RX, TX
- float distance = 0;
- char dischar[4];
- int speed = 0; // speed control value
- int i=0;
- float Length()
- {
- float duration, distancetmp;
- digitalWrite(trig, HIGH);
- delayMicroseconds(1000);
- digitalWrite(trig, LOW);
- duration = pulseIn (echo, HIGH);
- distancetmp = (duration / 2) / 29;
- return distancetmp;
- }
- void setup()
- {
- // put your setup code here, to run once:
- Serial.begin(9600);
- BT.begin(9600);
- pinMode(motorPin1, OUTPUT);
- pinMode(motorPin2, OUTPUT);
- pinMode(enablePin, OUTPUT);
- pinMode(trig, OUTPUT);
- pinMode(echo, INPUT);
- pinMode(beep, OUTPUT);
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, LOW);
- digitalWrite(enablePin, LOW);
- delay(1000);
- }
- void loop()
- {
-
- distance = Length();
- if (distance < 20) {
- Serial.print("WARNING!");
- Serial.print("Obstacle!!");
- digitalWrite(beep,HIGH);
- // delay(500);
- // cancelled because too many delay code
- digitalWrite(beep,LOW);
- speed = 0;
- analogWrite(enablePin, speed);
- } else {
- Serial.print("OK!");
- digitalWrite(beep,LOW);
- // delay(500);
- // cancelled because too many delay code
- if( speed == 0 ) {
- speed = 191; // 191 or 127 ?
- analogWrite(enablePin, speed);
- }
- }
- Serial.print("distance = ");
- Serial.print(distance);
- Serial.println("cm");
- delay(500);
-
- int sendData = (int) (distance * 100); //times 100 and convert disance to integer
- byte packet[3];
- packet[0] = 97;
- packet[1] = sendData / 256; //divides sendData to two 1 byte packets
- packet[2] = sendData % 256;
- if ( BT.available() > 0 ) {
- if (BT.read() == 97) {
- for(int i = 0; i < 3; i++)
- BT.write(packet);
- }
- val = BT.read();
-
- switch(x) {
- case 'F': // car forward
- forward();
- break;
- case 'B': // car back
- back();
- break;
- case 'S': // car stop
- motorstop();
- break;
- case 'H': // speed high
- motorhigh();
- break;
- case 'M': // speed mid
- motormedian();
- break;
- case 'L': // speed low
- motorlow();
- break;
- }
- delay(500); // if bluetooth command transmitted, the delay is need
- }
- }
- void motorstop()
- {
- speed = 0;
- Serial.println("Stop!");
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, LOW);
- analogWrite(enablePin, speed);
- }
- void forward()
- {
- Serial.println("Forward!");
- digitalWrite(motorPin1, HIGH);
- digitalWrite(motorPin2, LOW);
- if(speed == 0) {
- speed = 127;
- analogWrite(enablePin, speed);
- }
- }
- void back()
- {
- Serial.println("Back!");
- digitalWrite(motorPin1, LOW);
- digitalWrite(motorPin2, HIGH);
- if(speed == 0) {
- speed = 127;
- analogWrite(enablePin, speed);
- }
- }
- void motorhigh()
- {
- Serial.println("High!");
- if( speed != 255 ) { // if is 255 then no more need repeat the output
- speed = 255;
- analogWrite(enablePin, speed);
- }
- }
- void motormedian()
- {
- speed = 127;
- Serial.println("Median!");
- if( speed != 127 ) { // if is 127 then no more need repeat the output
- speed =127;
- analogWrite(enablePin, speed);
- }
- }
- void motorlow()
- {
- speed = 63;
- Serial.println("Low!");
- if( speed != 63 ) { // if is 63 then no more need repeat the output
- speed = 63;
- analogWrite(enablePin, speed);
- }
- }
複製代碼 這是修改後的代碼
你可以試試... |
|