Robofun 機器人論壇

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

新手求助

[複製鏈接]
跳轉到指定樓層
1#
發表於 2016-12-5 22:54:52 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最後由 sam21408 於 2016-12-5 23:00 編輯

我看著書的解說,設計一個超音波避障車,結果車子只會前進幾秒,後退幾秒,右後轉,一直重複,請問我哪裡打錯,我是新手海請各位高手幫幫忙。
#include <Servo.h>
Servo servo;
const int ECHOPIN=2;
const int TRIGPIN=4;
const int negR=7;
const int posR=8;
const int negL=12;
const int posL=13;
const int pwmR=5;
const int pwmL=6;
const int Rspeed=150;
const int Lspeed=150;
const int rotSpeed=180;
unsigned long Rdistance;
unsigned long Ldistance;
unsigned long Cdistance;
void setup()
{
  pinMode(posR,OUTPUT);
  pinMode(negR,OUTPUT);
  pinMode(posL,OUTPUT);
  pinMode(negL,OUTPUT);
  pinMode(TRIGPIN,OUTPUT);
  pinMode(ECHOPIN, INPUT);

  servo.attach(3);
  servo.write(90);
}

void loop()
{
  servo.write(90);
  delay(500);
  Cdistance=ping(TRIGPIN );  
  if(Cdistance<8)
  {
    pause(0,0);   

    servo.write(45);
    delay(500);
    Rdistance=ping(TRIGPIN);  
    servo.write(135);
    delay(500);
    Ldistance=ping(TRIGPIN);         
    servo.write(90);
    if(Rdistance<8 && Ldistance<8)
    {
      back(Rspeed,Lspeed);

      delay(2000);
      right(rotSpeed,rotSpeed);

      delay(500);
      forward(Rspeed,Lspeed);
    }      
    else if(Rdistance>Ldistance)
    {
      right(rotSpeed,rotSpeed);

      delay(500);
    }
    else if(Ldistance>Rdistance)
    {
      left(rotSpeed,rotSpeed);

      delay(500);  
    }   
  }
  else

    forward(Rspeed,Lspeed);  
   }  
   int ping(int)
{
   unsigned long cm,duration;

   digitalWrite(TRIGPIN,HIGH);
   delayMicroseconds(2);
   digitalWrite(TRIGPIN,HIGH);
   delayMicroseconds(10);
   digitalWrite(TRIGPIN,LOW);
   duration=pulseIn(ECHOPIN,HIGH);
   cm=duration/29/2;
   return cm;

}  
void forward(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,HIGH);
    digitalWrite(negR,LOW);         
    digitalWrite(posL,LOW);
    digitalWrite(negL,HIGH);   
}  
void back(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,LOW);
    digitalWrite(negR,HIGH);         
    digitalWrite(posL,HIGH);
    digitalWrite(negL,LOW);   
}
void pause(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,LOW);
    digitalWrite(negR,LOW);         
    digitalWrite(posL,LOW);
    digitalWrite(negL,LOW);   
}
void right(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,LOW);
    digitalWrite(negR,LOW);         
    digitalWrite(posL,LOW);
    digitalWrite(negL,HIGH);        
}
void left(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,HIGH);
    digitalWrite(negR,LOW);         
    digitalWrite(posL,LOW);
    digitalWrite(negL,LOW);        
}
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

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

GMT+8, 2024-11-23 17:30 , Processed in 0.071773 second(s), 9 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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