Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 4231|回復: 0
打印 上一主題 下一主題

超音波解救

[複製鏈接]
跳轉到指定樓層
1#
發表於 2012-3-29 22:10:54 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
unsigned char analogPin = 0;
float val = 0;
int pwm_a = 3;
int pwm_b = 11;
int dir_a = 12;
int dir_b = 13;

void setup() {

Serial.begin(9600);
pinMode(pwm_a, OUTPUT);
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);

}

void loop()
{
  val = analogRead(analogPin)*1.38;    // read the value from the
  Serial.print("EZ1 distance = ");
  Serial.print(val);
  Serial.println(" cm");
  delay(100);

digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, HIGH);
delay(100);
analogWrite(pwm_a,250);
analogWrite(pwm_b,250);
delay(100);

if (val < 100) // obstacle detected
{
//Stop both motors...
digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, HIGH);
analogWrite(pwm_a, 100);
analogWrite(pwm_b, 100);

delay(1000); // ...and pause for a second
}
else if  ( val < 50 );
{
digitalWrite(dir_a, LOW);
digitalWrite(dir_b, HIGH);
analogWrite(pwm_a, 150);
analogWrite(pwm_b, 50);
delay (1000);
}

}


無法照程式計畫走
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-12-4 16:04 , Processed in 0.181419 second(s), 8 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表