#include <SoftwareSerial.h>
#include<Servo.h>
Servo servoRight;
Servo servoLeft;
SoftwareSerial BT(9,10); // 接收腳(RX), 傳送腳(TX);
char val;
String vals;
int count1=0;
char cd[2];
int leftOut;
int leftIn;
int center;
int rightIn;
int rightOut;
int IRStatus=0;
int count=0;
int number1=0;
void setup() {
// put your setup code here, to run once:
servoRight.attach(12);
servoLeft.attach(13);
Serial.begin(9600); //for arduino serial port mointor
BT.begin(38400);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(BT.available()){
val=BT.read();
if(val==65) vals+='A';if(val==66) vals+='B';if(val==67) vals+='C';
if(val==68) vals+='D';if(val==69) vals+='E';if(val==70) vals+='F';
if(val==71) vals+='G';if(val==72) vals+='H';if(val==73) vals+='I';
if(val==49) vals+='1';if(val==50) vals+='2';if(val==51) vals+='3';
if(val==52) vals+='4';if(val==53) vals+='5';if(val==54) vals+='6';
if(val==55) vals+='7';if(val==56) vals+='8';
count1++;
if(count1==2){
Serial.println(vals);
int x=0,y=0; //貨櫃作標
int Ax=0,Ay=0;// 車子起點作標
int Ansx=0,Ansy=0;//要拿貨的座標
int AAnsx=0,AAnsy=0;//車子要走幾步
switch(number1){//放入貨櫃作標
case 1:
x=2;y=1;
break;
case 2:
x=3;y=1;
break;
case 3:
x=4;y=1;
break;
case 4:
x=5;y=1;
break;
case 5:
x=2;y=2;
break;
case 6:
x=3;y=2;
break;
case 7:
x=4;y=2;
break;
case 8:
x=5;y=2;
break;
case 9:
x=8;y=1;
break;
case 10:
x=9;y=1;
break;
case 11:
x=10;y=1;
break;
case 12:
x=11;y=1;
break;
case 13:
x=8;y=2;
break;
case 14:
x=9;y=2;
break;
case 15:
x=10;y=2;
break;
case 16:
x=11;y=2;
break;
case 17:
x=14;y=1;
break;
case 18:
x=15;y=1;
break;
case 19:
x=16;y=1;
break;
case 20:
x=17;y=1;
break;
case 21:
x=14;y=2;
break;
case 22:
x=15;y=2;
break;
case 23:
x=16;y=2;
break;
case 24:
x=17;y=2;
break;
case 25:
x=2;y=5;
break;
case 26:
x=3;y=5;
break;
case 27:
x=4;y=5;
break;
case 28:
x=5;y=5;
break;
case 29:
x=2;y=6;
break;
case 30:
x=3;y=6;
break;
case 31:
x=4;y=6;
break;
case 32:
x=5;y=6;
break;
case 33:
x=8;y=5;
break;
case 34:
x=9;y=5;
break;
case 35:
x=10;y=5;
break;
case 36:
x=11;y=5;
break;
case 37:
x=8;y=6;
break;
case 38:
x=9;y=6;
break;
case 39:
x=10;y=6;
break;
case 40:
x=11;y=6;
break;
case 41:
x=14;y=5;
break;
case 42:
x=15;y=5;
break;
case 43:
x=16;y=5;
break;
case 44:
x=17;y=5;
break;
case 45:
x=14;y=6;
break;
case 46:
x=15;y=6;
break;
case 47:
x=16;y=6;
break;
case 48:
x=17;y=6;
break;
case 49:
x=2;y=9;
break;
case 50:
x=3;y=9;
break;
case 51:
x=4;y=9;
break;
case 52:
x=5;y=9;
break;
case 53:
x=2;y=10;
break;
case 54:
x=3;y=10;
break;
case 55:
x=4;y=10;
break;
case 56:
x=5;y=10;
break;
case 57:
x=8;y=9;
break;
case 58:
x=9;y=9;
break;
case 59:
x=10;y=9;
break;
case 60:
x=11;y=9;
break;
case 61:
x=8;y=10;
break;
case 62:
x=9;y=10;
break;
case 63:
x=10;y=10;
break;
case 64:
x=11;y=10;
break;
case 65:
x=14;y=9;
break;
case 66:
x=15;y=9;
break;
case 67:
x=16;y=9;
break;
case 68:
x=17;y=9;
break;
case 69:
x=14;y=10;
break;
case 70:
x=15;y=10;
break;
case 71:
x=16;y=10;
break;
case 72:
x=17;y=10;
break;
}
Serial.println(x);
Serial.println(y);
if(vals.charAt(1)=='1'||vals.charAt(1)=='2'||vals.charAt(1)=='3'||vals.charAt(1)=='4'){
//下面拿貨 ex 貨櫃A1(2,1)
Ansx=x-Ax;//拿貨x Ax起點(0) x貨櫃(2) Ansx拿貨(2)
Ansy=y-Ay-1;//拿貨y Ay起點(0) y貨櫃(1) Ansy拿貨(0)
}
else{//上面拿貨 ex 貨櫃A5(2,2)
Ansx=x-Ax;//拿貨x Ax起點(0) x貨櫃(2) Ansx拿貨(2)
Ansy=y-Ay+2;//拿貨y Ay起點(0) y貨櫃(2) Ansy拿貨(4)
}
//要去(2,4)座標
AAnsx=Ansx-(Ax); //Ansx拿貨座標(2) Ax起點(0) AAnsx步數(2)
AAnsy=Ansy-(Ay); //Ansy拿貨座標(4) Ay終點(0) AAnsy步數(4)
Serial.println(AAnsx);
Serial.println(AAnsy);
for(int q=0;q<=AAnsy;q++) {//直走
if(q!=AAnsy) {
forward(450);
turnLeft(27);//車子會右偏,所以往左校正一些
}
else {//右轉
forward(450);
turnRight(650);
turnLeft(27);
}
}
for(int w=1;w<=AAnsx;w++) {//右轉後直走
if(w!=AAnsx) {
forward(500);
//turnRight(50);
}
else {//停下
stopward(3000);
}
}
count1=0;
vals="";
stopward(100);
}
}
}
↓目前使用3燈,不知是否使用5燈會好點?
void getSensorData(){
leftIn=digitalRead(A1);
center=digitalRead(A2);
rightIn=digitalRead(A3);
}
switch(IRStatus){
case 0:
stopward(100);
break;
case 1:
turnLeft(250);
break;
case 2:
stopward(100);
break;
case 3:
turnLeft(300);
break;
case 4:
turnRight(250);
break;
case 5:
forward(450);
break;
case 6:
turnRight(300);
break;
case 7:
stopward(100);
break;
}
}
int backward(int delaytime){
servoRight.writeMicroseconds(1300);
servoLeft.writeMicroseconds(1700);
delay(delaytime);
}
int turnLeft(int delaytime){
servoRight.writeMicroseconds(1300);
servoLeft.writeMicroseconds(1300);
delay(delaytime);
}
int turnRight(int delaytime){
servoRight.writeMicroseconds(1700);
servoLeft.writeMicroseconds(1700);
delay(delaytime);
}
int forward(int delaytime){
servoRight.writeMicroseconds(1700);
servoLeft.writeMicroseconds(1300);
delay(delaytime);
}
}
int stopward(int delaytime){
servoRight.writeMicroseconds(1500);
servoLeft.writeMicroseconds(1500);
delay(delaytime);
}作者: 超新手 時間: 2018-6-27 20:17
要循跡, 只要把
1) void driveMotor(int IRStatus)
的 case 寫對即可
2) 在 void driveMotor(int IRStatus)
中增加 case 2
3) 拿掉其他 半自走 的部份