您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 其它行业文档 > 基于Arduino的超声波壁障小车源程序
基于Arduino的超声波壁障小车源程序河南大学王艺//L=左//R=右//F=前//B=後#includeServo.hintpinLF=14;//定义6脚位左后intpinLB=15;//定义9脚位左前intpinRF=16;//定义10脚位右前intpinRB=17;//定义11脚位右后intinputPin=9;//定义超音波接受脚位intoutputPin=8;//定义超音波发送脚位intFspeedd=0;//前速intRspeedd=0;//右速intLspeedd=0;//左速intdirectionn=0;Servomyservo;//myservointdelay_time=250;//舵机的稳定时间intFgo=8;//前进intRgo=6;//右转intLgo=4;//左转intBgo=2;//倒车voidsetup(){Serial.begin(9600);pinMode(pinLB,OUTPUT);pinMode(pinLF,OUTPUT);pinMode(pinRB,OUTPUT);pinMode(pinRF,OUTPUT);pinMode(inputPin,INPUT);pinMode(outputPin,OUTPUT);myservo.attach(10);//定义舵机第5脚位(PWM)}voidadvance(inta)//前进{digitalWrite(pinRF,HIGH);digitalWrite(pinRB,LOW);//analogWrite(pinRF,100);//analogWrite(pinRB,0);digitalWrite(pinLF,HIGH);digitalWrite(pinLB,LOW);//analogWrite(pinLF,100);//analogWrite(pinLB,0);delay(a*100);}voidright(intb){digitalWrite(pinRF,LOW);digitalWrite(pinRB,LOW);//analogWrite(pinRF,0);//analogWrite(pinRB,0);digitalWrite(pinLF,HIGH);digitalWrite(pinLB,LOW);//analogWrite(pinLF,100);//analogWrite(pinLB,0);delay(b*100);}voidleft(intc){digitalWrite(pinRF,HIGH);digitalWrite(pinRB,LOW);//analogWrite(pinRF,100);//analogWrite(pinRB,0);digitalWrite(pinLF,LOW);digitalWrite(pinLB,LOW);//analogWrite(pinLF,0);//analogWrite(pinLB,0);delay(c*100);}voidturnR(intd){digitalWrite(pinRF,LOW);digitalWrite(pinRB,HIGH);analogWrite(pinRF,0);analogWrite(pinRB,100);digitalWrite(pinLF,HIGH);digitalWrite(pinLB,LOW);analogWrite(pinLF,100);analogWrite(pinLB,0);delay(d*100);}voidturnL(inte){digitalWrite(pinRF,HIGH);digitalWrite(pinRB,LOW);analogWrite(pinRF,100);analogWrite(pinRB,0);digitalWrite(pinLF,LOW);digitalWrite(pinLB,HIGH);analogWrite(pinLF,0);analogWrite(pinLB,100);delay(e*100);}voidstopp(intf)//停止{digitalWrite(pinRB,LOW);digitalWrite(pinRF,LOW);digitalWrite(pinLB,LOW);digitalWrite(pinLF,LOW);delay(f*100);}voidback(intg){digitalWrite(pinRF,LOW);digitalWrite(pinRB,HIGH);//analogWrite(pinRF,0);//analogWrite(pinRB,150);digitalWrite(pinLF,LOW);digitalWrite(pinLB,HIGH);//analogWrite(pinLF,0);//analogWrite(pinLB,150);delay(g*100);}voiddetection()//测量三个角度(0.90.179){intdelay_time=250;//舵机稳定时间ask_pin_F();if(Fspeedd10){stopp(1);back(2);}if(Fspeedd25){stopp(1);ask_pin_L();delay(delay_time);ask_pin_R();delay(delay_time);if(LspeeddRspeedd){directionn=Rgo;//向右走}if(Lspeedd=Rspeedd){directionn=Lgo;//向左走}if(Lspeedd10&&Rspeedd10){directionn=Bgo;}}else{directionn=Fgo;//向前走}}voidask_pin_F()//量出前方距离{myservo.write(90);digitalWrite(outputPin,LOW);delayMicroseconds(2);digitalWrite(outputPin,HIGH);delayMicroseconds(10);digitalWrite(outputPin,LOW);floatFdistance=pulseIn(inputPin,HIGH);Fdistance=Fdistance/5.8/10;Serial.print(Fdistance:);//输出距离单位公分Serial.println(Fdistance);Fspeedd=Fdistance;}voidask_pin_L()//测出左边距离{myservo.write(5);delay(delay_time);digitalWrite(outputPin,LOW);delayMicroseconds(2);digitalWrite(outputPin,HIGH);delayMicroseconds(10);digitalWrite(outputPin,LOW);floatLdistance=pulseIn(inputPin,HIGH);Ldistance=Ldistance/5.8/10;//将时间转化为距离Serial.print(Ldistance:);Serial.println(Ldistance);Lspeedd=Ldistance;}voidask_pin_R()//量出右边距离{myservo.write(177);delay(delay_time);digitalWrite(outputPin,LOW);delayMicroseconds(2);digitalWrite(outputPin,HIGH);delayMicroseconds(10);digitalWrite(outputPin,LOW);floatRdistance=pulseIn(inputPin,HIGH);Rdistance=Rdistance/5.8/10;Serial.print(Rdistance:);Serial.println(Rdistance);Rspeedd=Rdistance;}voidloop(){myservo.write(90);//舵机初次位置detection();//测量角度,判断往哪边转动if(directionn==2)//假如directionn(方向)=2(倒车){back(8);//倒退turnL(2);//Serial.print(Reverse);}if(directionn==6)//假如directionn(方向)=6(右){back(1);turnR(6);//右Serial.print(Right);}if(directionn==4)//假如directionn(方向)=4(左){back(1);turnL(6);//左Serial.print(Left);}if(directionn==8)//假如directionn(方向)=8(前){advance(1);//正常前Serial.print(Advance);Serial.print();}}
本文标题:基于Arduino的超声波壁障小车源程序
链接地址:https://www.777doc.com/doc-5172188 .html