arduino编程迷宫小车 在arduino上编写程序做智能小车要调用哪些程序
\u6c42\u52a9 \u5173\u4e8earduino\u8ff7\u5bab\u7b97\u6cd5http://www.yfrobot.com/forum.php?mod=viewthread&tid=2227&highlight=3pi
int outputPin = 5;
int inputPin = 4;
int cmdPin = 3;
int pinI1=8;//\u5b9a\u4e49I1\u63a5\u53e3
int pinI2=9;//\u5b9a\u4e49I2\u63a5\u53e3
int speedpin1=11;//\u5b9a\u4e49EA(PWM\u8c03\u901f)\u63a5\u53e3
int pinI3=6;//\u5b9a\u4e49I3\u63a5\u53e3
int pinI4=7;//\u5b9a\u4e49I4\u63a5\u53e3
int speedpin2=10;//\u5b9a\u4e49EB(PWM\u8c03\u901f)\u63a5\u53e3
int average_speed_l = 150;
int average_speed_r = 100;
int speed_limit_l = 80;
int speed_limit_r = 60;
int target_distance = 30;//in cm
int lost_distance = 80;
float detect_distance()
{
digitalWrite(outputPin, LOW); // \u8ba9\u8d85\u58f0\u6ce2\u53d1\u5c04\u4f4e\u7535\u538b2\u03bcs
delayMicroseconds(2);
digitalWrite(outputPin, HIGH); // \u8ba9\u8d85\u58f0\u6ce2\u53d1\u5c04\u9ad8\u7535\u538b10\u03bcs\uff0c\u8fd9\u91cc\u81f3\u5c11\u662f10\u03bcs
delayMicroseconds(10);
digitalWrite(outputPin, LOW); // \u7ef4\u6301\u8d85\u58f0\u6ce2\u53d1\u5c04\u4f4e\u7535\u538b
float Fdistance = pulseIn(inputPin, HIGH); // \u8bfb\u5dee\u76f8\u5dee\u65f6\u95f4
Fdistance= Fdistance/5.8/10; // \u5c06\u65f6\u95f4\u8f6c\u4e3a\u8ddd\u79bb\u8ddd\u79bb\uff08\u5355\u4f4d\uff1a\u516c\u5206\uff09
Serial.print("F distance:"); //\u8f93\u51fa\u8ddd\u79bb\uff08\u5355\u4f4d\uff1a\u516c\u5206\uff09
Serial.println(Fdistance); //\u663e\u793a\u8ddd\u79bb
return Fdistance;
}
void rightSpeed(int speed_r)
{
if(speed_r < speed_limit_r)
{
speed_r = 0;
}
analogWrite(speedpin1,speed_r);//\u8f93\u5165\u6a21\u62df\u503c\u8fdb\u884c\u8bbe\u5b9a\u901f\u5ea6
digitalWrite(pinI1,HIGH);//\u4f7f\u76f4\u6d41\u7535\u673a\uff08\u5de6\uff09\u987a\u65f6\u9488\u8f6c
digitalWrite(pinI2,LOW);
}
void leftSpeed(int speed_l)
{
if(speed_l < speed_limit_l)
{
speed_l = 0;
}
analogWrite(speedpin2,speed_l);//\u8f93\u5165\u6a21\u62df\u503c\u8fdb\u884c\u8bbe\u5b9a\u901f\u5ea6
digitalWrite(pinI3,LOW);//\u4f7f\u76f4\u6d41\u7535\u673a\uff08\uff09\u987a\u65f6\u9488\u8f6c
digitalWrite(pinI4,HIGH);
}
void stop_l()
{
digitalWrite(speedpin2,LOW);//\u8f93\u5165\u6a21\u62df\u503c\u8fdb\u884c\u8bbe\u5b9a\u901f\u5ea6
digitalWrite(pinI3,LOW);//\u4f7f\u76f4\u6d41\u7535\u673a\uff08\uff09\u987a\u65f6\u9488\u8f6c
digitalWrite(pinI4,LOW);
}
void stop_r()
{
digitalWrite(speedpin1,LOW);//\u8f93\u5165\u6a21\u62df\u503c\u8fdb\u884c\u8bbe\u5b9a\u901f\u5ea6
digitalWrite(pinI1,LOW);//\u4f7f\u76f4\u6d41\u7535\u673a\uff08\u5de6\uff09\u987a\u65f6\u9488\u8f6c
digitalWrite(pinI2,LOW);
}
void turn_l()
{
leftSpeed(100);
rightSpeed(150);
}
void turn_r()
{
leftSpeed(200);
rightSpeed(80);
}
void noturn()
{
leftSpeed(100);
rightSpeed(80);
}
void rotate()
{
leftSpeed(200);
rightSpeed(0);
}
void setup()
{
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(pinI1,OUTPUT);
pinMode(pinI2,OUTPUT);
pinMode(speedpin1,OUTPUT);
pinMode(pinI3,OUTPUT);
pinMode(pinI4,OUTPUT);
pinMode(speedpin2,OUTPUT);
pinMode(cmdPin, INPUT);
}
void loop()
{
if(digitalRead(cmdPin) == LOW){
float dis = detect_distance();
if(dis > lost_distance){
while(true)
{
rotate();
delay(100);
stop_l();
stop_r();
delay(100);
dis = detect_distance();
if(dis < lost_distance) break;
}
}else{
if(dis > target_distance){
if(dis - target_distance > 2){ //if the difference is small enough, it can be neglected.
turn_r();
}else{
noturn();
}
}
if(dis < target_distance){
if(target_distance - dis > 2){
turn_l();
}else{
noturn();
}
}
}
}else{
stop_l();
stop_r();
}
delay(100);
}
查看原帖>>
个学生的姓名(如丁),D单元格中即刻显示出该
绛旓細涓鐢熺殑濮撳悕锛堝涓侊級锛孌鍗曞厓鏍间腑鍗冲埢鏄剧ず鍑鸿
绛旓細鍙互蹇熶娇鐢Arduino涓嶢dobe Flash, Processing, Max/MSP, PureData, SuperCollider绛夎蒋浠剁粨鍚堬紝浣滃嚭浜掑姩浣滃搧銆 Arduino鍙互浣跨敤鐜版湁鐨勭數瀛愬厓浠朵緥濡傚紑鍏虫垨鑰呬紶鎰熷櫒鎴栬呭叾浠栨帶鍒跺櫒浠躲丩ED銆佹杩涢┈杈炬垨鍏朵粬杈撳嚭瑁呯疆銆 Arduino涔熷彲浠ョ嫭绔嬭繍琛岋紝骞朵笌杞欢杩涜浜や簰锛屼緥濡傦細Adobe Flash, Processing, Max/MSP, Pure Data,...
绛旓細濡傛灉杩欎簺閮界帺鑵讳簡锛屽彲浠ヨ瘯璇曠帺涓涓満鍣ㄤ汉椋炶鎺у埗锛屾瘮濡傚洓鏃嬬考椋炴満銆傞琛屽櫒鏄叚鑷敱搴︽帶鍒讹紝鍥犳姣灏忚溅瑕佹洿鍔犲叿鏈夋寫鎴樻э紝涔熼渶瑕佹洿绮剧‘鐨勬帶鍒剁郴缁熴備笅闈㈣繖鏄垜浠ュ墠鐨勪竴涓洓鏃嬬考DIY锛屽熀浜arduino MultiWii鐨勩備緷鏃ф窐瀹濆洓鏃嬬考椋炶鍣ㄣ侻ultiWii鏄竴涓熀浜巃rduino鐨勫紑婧愰鎺у钩鍙帮紝鎵鏈塩浠g爜閮藉彲寰楋紝涓嶅浜庝竴涓...
绛旓細鏀寔瀵Arduino Mega銆丄rduino Uno鍜孉rduino Leonardo鐨勭數璺璁′笌浠跨湡; 鏀寔Adafruit銆丅reakout Peripherals銆丟rove鍜孧otor Control绛4澶х被鍩烘湰澶栬,鍚屾椂杩樺彲浠ユ敮鎸侀氱敤鐨勫璁炬ā鍨; 鏀寔鍩轰簬娴佺▼鍥剧殑鑷姩缂栫▼; 鏀寔灏嗘祦绋嬪浘杞崲鎴愰珮绾ц瑷; 鎻愪緵Funduino銆乑umo鏅鸿兘鏈哄櫒浜灏忚溅浠跨湡妯″瀷,鍙畬鎴愬杩广侀伩闅滃拰鏈哄櫒浜杩峰绛夊涔犮 涓...
绛旓細鏀寔瀵Arduino Mega銆丄rduino Uno鍜孉rduino Leonardo鐨勭數璺璁′笌浠跨湡; 鏀寔Adafruit銆丅reakout Peripherals銆丟rove鍜孧otor Control绛4澶х被鍩烘湰澶栬,鍚屾椂杩樺彲浠ユ敮鎸侀氱敤鐨勫璁炬ā鍨; 鏀寔鍩轰簬娴佺▼鍥剧殑鑷姩缂栫▼; 鏀寔灏嗘祦绋嬪浘杞崲鎴愰珮绾ц瑷; 鎻愪緵Funduino銆乑umo鏅鸿兘鏈哄櫒浜灏忚溅浠跨湡妯″瀷,鍙畬鎴愬杩广侀伩闅滃拰鏈哄櫒浜杩峰绛夊涔犮 9...