[*Altar小车动起来*]5. 跟随-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

12
返回列表 发新帖
楼主: vickywong

[*Altar小车动起来*]5. 跟随

[复制链接]
发表于 2016-12-10 23:24 | 显示全部楼层
vickywong 发表于 2016-12-10 11:53
如果按你的接线,应该把前面的接线改成这样:
[mw_shl_code=cpp,true]const byte echo_pin = 12;
const b ...

const byte echo_pin = 12;
const byte trig_pin = 13;
const byte rangingLeft_pin = A0;
const byte rangingRight_pin = 8;
const byte inPin_leftB = 5;
const byte inPin_leftA = 6;
const byte inPin_rightA = 9;
const byte inPin_rightB = 10;

int distan;
byte rangingA;
byte rangingB;
  
void setup() {
  pinMode(echo_pin,INPUT);
  pinMode(trig_pin,OUTPUT);
  pinMode(rangingLeft_pin,INPUT);
  pinMode(rangingRight_pin,INPUT);
  pinMode(inPin_leftA,OUTPUT);
  pinMode(inPin_leftB,OUTPUT);
  pinMode(inPin_rightA,OUTPUT);
  pinMode(inPin_rightB,OUTPUT);
}
int getDistance(){
  int dist;
  digitalWrite(trig_pin,LOW);
  delayMicroseconds(2);
  digitalWrite(trig_pin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trig_pin,LOW);
  dist=pulseIn(echo_pin,HIGH)/58;
  return dist;
}
void goUp(int up_speed)
{
  analogWrite(inPin_leftA,0);
  analogWrite(inPin_rightA,0);
  analogWrite(inPin_leftB,up_speed);
  analogWrite(inPin_rightB,up_speed);
}
void goLeft(int left_speed)
{
  analogWrite(inPin_leftA,left_speed);
  analogWrite(inPin_rightB,left_speed);
  analogWrite(inPin_leftB,255);
  analogWrite(inPin_rightA,255);
}
void goRight(int right_speed)
{
  analogWrite(inPin_leftB,right_speed);
  analogWrite(inPin_rightA,right_speed);
  analogWrite(inPin_leftA,255);
  analogWrite(inPin_rightB,255);
}
void goBack(int back_speed)
{
  analogWrite(inPin_leftB,back_speed);
  analogWrite(inPin_rightB,back_speed);
  analogWrite(inPin_leftA,255);
  analogWrite(inPin_rightA,255);
}
void goStop()
{
  analogWrite(inPin_leftA,0);
  analogWrite(inPin_leftB,0);
  analogWrite(inPin_rightA,0);
  analogWrite(inPin_rightB,0);
}
void loop(){
  distan = getDistance();
  rangingA = digitalRead(rangingLeft_pin);
  rangingB = digitalRead(rangingRight_pin);
    if(distan >= 2 && distan <= 10 && rangingA == 0 && rangingB == 0)
    {
      goUp(100);
    }
    else if(distan >= 2 && distan <= 10 && rangingA == 0 && rangingB == 1)
    {
      goRight(100);
      delay(100);
      goStop();
    }
    else if(distan >= 2 && distan <= 10 && rangingA == 1 && rangingB == 0)
    {
      goLeft(100);
      delay(100);
      goStop();
    }
    else
    {
      goStop();
    }
}

我上傳了你給的這個,可惜沒有任何反應,你上傳是否可運行?
 楼主| 发表于 2016-12-11 08:16 | 显示全部楼层
byran620 发表于 2016-12-10 23:24
const byte echo_pin = 12;
const byte trig_pin = 13;
const byte rangingLeft_pin = A0;

9楼程序验证过的,没问题。可能你的接线不一样,自己改改就好了
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-28 02:22 , Processed in 0.075420 second(s), 14 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表