?
void Robot_Traction() ????????????????????//机器人循迹子程序
{
??//有信号为LOW ?没有信号为HIGH
??SR = digitalRead(SensorRight);//有信号表明在白色区域,车子底板上L1亮;没信号表明压在黑线上,车子底板上L1灭
??SL = digitalRead(SensorLeft);//有信号表明在白色区域,车子底板上L2亮;没信号表明压在黑线上,车子底板上L2灭
??if (SL == LOW && SR == LOW)
????run(); ??//调用前进函数
??else if (SL == HIGH & SR == LOW)// 左循迹红外传感器,检测到信号,车子向右偏离轨道,向左转
????left();
??else if (SR == HIGH & SL == LOW) // 右循迹红外传感器,检测到信号,车子向左偏离轨道,向右转
????right();
??else // 都是白色, 停止
????brake();
}
void bz()//障碍程序
{
??front_detection();//测量前方距离
??if (Front_Distance < 20) //当遇到障碍物时
??{
????brake3(2);//先刹车
????back3(3);//后退减速
????brake3(2);//停下来做测距
????left_detection();//测量左边距障碍物距离
????right_detection();//测量右边距障碍物距离
????if ((Left_Distance < 20 ) && ( Right_Distance < 20 )) //当左右两侧均有障碍物靠得比较近
??????spin_left3(0.7);//旋转掉头
????else if (Left_Distance > Right_Distance) //左边比右边空旷
????{
??????left3(4);//左转
??????brake3(1);//刹车,稳定方向
????}
????else//右边比左边空旷
????{
??????right3(4);//右转
??????brake3(1);//刹车,稳定方向
????}
??}
??else
??{
????run(); //无障碍物,直行
??}
}
void gs() //跟随函数
{
??front_detection();//测量前方距离
??R = digitalRead(SensorR);//有信号表明在白色区域,红外传感器LED1亮
??L = digitalRead(SensorL);//有信号表明在白色区域,红外传感器LED2亮
?if (Front_Distance >9&&Front_Distance <= 30&&L == LOW&&R==LOW)
????run(); ??//调用前进函数
??else if (L == HIGH & R == LOW)// 左跟随红外传感器,检测到信号,车子向右偏离轨道,向左转
????left();
??else if (R == HIGH & L == LOW) // 右跟随红外传感器,检测到信号,车子向左偏离轨道,向右转 ?
????right();
??else if(Front_Distance > 4 && Front_Distance <= 9&&L == LOW&&R==LOW)
????brake();//先刹车
????else if(Front_Distance > 0 && Front_Distance <= 4&&L == LOW&&R==LOW)
??back();
????else // 都是黑色, 停止
??brake(); ?}
void ceju() //测距程序
{
?front_detection();//测量前方距离
?if (Front_Distance > 0 && Front_Distance <= 99 )
?{
?Serial.print("distance= ");
?Serial.print(Front_Distance);
?Serial.println("cm");
?delay(700);
?}
?if(Front_Distance >99 )
?Serial.println("Out of range");
?delay(700);
??}
??
void jxbcs()
{
??int robotIniPosArray[4][2] = {
??{servopin3, 90},
??{servopin2, 90}, ?
??{servopin4, 90},
??{servopin1, 90}
??};
???for (int i = 0; i < 4; i++){
????servopulse(robotIniPosArray[i][0], robotIniPosArray[i][1]);
??}
}
void jxb(char val1)
{
??val = Serial.read();
??switch (val1) {
????case 'W': ?a += 10;if (a > 140) a = 140;servopulse(servopin1, a);
????Serial.print("a="); Serial.println(a);break;
????case 'S': a -= 10;if (a < 70) a = 70;servopulse(servopin1, a);
?????Serial.print("a="); Serial.println(a);break;
????case 'A': b += 10;if (b > 180) b = 180; ?
????Serial.print("b="); Serial.println(b);break;
????case 'D': b -= 10;if (b < 0) b = 0;
????Serial.print("b="); Serial.println(b);break;
????case '8': ?c += 10;if (c > 180) c = 180;
????Serial.print("c="); Serial.println(c);break;
????case '5': c -= 10;if (c < 0) c = 0;
????Serial.print("c="); Serial.println(c);break;
????case '4': d += 10;if (d > 180) d = 180;servopulse(servopin4, d);
????Serial.print("d="); Serial.println(d);break;
????case '6': d -= 10;if (d < 0) d = 0; servopulse(servopin4, d);
????Serial.print("d="); Serial.println(d);break;
??????break;
????default:
??????break;
??}
}
void dump(decode_results *results)
{
??int count = results->rawlen;
??if (results->decode_type == UNKNOWN)
??{
????brake();
??}
}
void IR_IN() ????????????????????????????//机器人遥控子程序
{
??if (irrecv.decode(&results)) //调用库函数:解码
??{
????if (millis() - last > 250) //确定接收到信号
????{
??????on = !on;//标志位置反
??????dump(&results);//解码红外信号
????}
????if (results.value == CH0 ) ???{ run2();delay(100);brake2();}//前进
????if (results.value == CH1 ) ???{ back2();delay(100);brake2();}//后退
????if (results.value == PREV ) ??{ left2();delay(80);brake2();}//左转
????if (results.value == NEXT ) ??{ right2();delay(80);brake2();}//右转
????if (results.value == CH2 ) ????brake2();//停车
????if (results.value == PLAY ) ???spin_left2();//左旋转
????if (results.value == EQ ) ?????spin_right2();//右旋转
????if (results.value == IR_200 ) { jxbcs();keyMode = KEYMODE_1;brake2();}
????if (results.value == VOL1 ) ??{ val1 = 'W';jxb(val1); }
????if (results.value == VOL2 ) ??{ val1 = 'S'; jxb(val1); }
????if (results.value == IR_0 ) ??{ val1 = 'A';jxb(val1); }
????if (results.value == IR_100 ) { val1 = 'D';jxb(val1); }
????if (results.value == IR_1 ) ??{ val1 = '8'; jxb(val1); }
????if (results.value == IR_2 ) ??{ val1 = '5';jxb(val1); ?}
????if (results.value == IR_4 ) ??{ val1 = '4';jxb(val1); }
????if (results.value == IR_5 ) ??{ val1 = '6'; jxb(val1);}
????if (results.value == IR_6 ) ??keyMode = KEYMODE_1;
????if (results.value == IR_7) ???keyMode = KEYMODE_2;
????if (results.value == IR_8) ???keyMode = KEYMODE_3;
????if (results.value == IR_9) ???keyMode = KEYMODE_4;
????last = millis();
????irrecv.resume(); // Receive the next value }}
void LEDTask()
{
??switch (keyMode)
??{
case KEYMODE_1: IR_IN();digitalWrite(PORT_LED1, HIGH); break; //调用复位程序case KEYMODE_2: Robot_Traction(); digitalWrite(PORT_LED1, LOW);break;
case KEYMODE_3: bz();digitalWrite(PORT_LED1, HIGH);break;//用超声波避障程序
case KEYMODE_4: gs();digitalWrite(PORT_LED1, LOW);break; //调用跟随程序
????case KEYMODE_5: ceju(); digitalWrite(PORT_LED1, HIGH);break;//测距
????default:
??????break;}}
void reve()
{
??if( Serial.available()>0 ){
?????????int receive=Serial.parseInt();
??if(receive>=1 && receive<=5){moveSpeed=int(receive*40+55);}
??else if(receive==0) ???????{brake();Serial.println("Speed=0,brake");}//停车
??else if(receive==100) ?????{ val1 = 'A';jxb(val1); }
??else if(receive==101) ?????{ val1 = 'W';jxb(val1); }
??else if(receive==102) ?????{ val1 = 'S';jxb(val1);}
??else if(receive==103) ?????{ val1 = 'D';jxb(val1);}
??else if(receive==104) ?????{ val1 = '5';jxb(val1);}
??else if(receive==105) ?????{ val1 = '4';jxb(val1); }
??else if(receive==106) ?????{ val1 = '6';jxb(val1);}
??else if(receive==107) ?????{ val1 = '8';jxb(val1);}
??else if(receive==117) ?????{ jxbcs(); keyMode = KEYMODE_1;Serial.println("FW");brake2();} ????
??else if(receive==108) ????{run(); Serial.println("run");}//前进
??else if(receive==109) ????{back();Serial.println("back");}//后退
??else if(receive==110) ????{brake();Serial.println("brake");}//停车
??else if(receive==111) ????{ left();Serial.println("left");}//左
??else if(receive==112) ????{right();Serial.println("right");}//右
??else if(receive==113) ????{keyMode = KEYMODE_2;Serial.println("Robot_Traction");}//寻迹
else if(receive==114) ????{keyMode = KEYMODE_3;Serial.println("bz");}//避障
else if(receive==115) ????{keyMode = KEYMODE_4;Serial.println("gs");}//跟随
else if(receive==116) ????{keyMode = KEYMODE_5;}}}
void loop()
{
??reve();//蓝牙遥控
??IR_IN();//红外遥控
??LEDTask();//模式区分
??servopulse(servopin2, b);//2舵机连续转动
??servopulse(servopin3, c);//3舵机连续转动
}