网络资源
全部免费下载

Arduino寻迹小车和推球对决

Arduino寻迹小车和推球对决
暑假去了访学,搞了个寻迹小车,装载了两方面的内容,沿着黑线走,和三对三的推球对战,双方场地都有一样的球,小车自动将球推到对方场地,看谁最后场地中球最少了~
有两个难点,一个是小车寻迹的第一过程,一个是进入比赛场地后小车要按新的程序进行来往推球,不能再一直寻迹,用的Arduino写的代码,分享给大家看一下,小车真的很好玩haha
 
//Line Tracking IO define
#define LT_R !digitalRead(10)
#define LT_M !digitalRead(4)
#define LT_L !digitalRead(2)
 
#define ENB 5
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11
#define ENA 6
#include   //servo library
Servo myservo;     // create servo object to control servo
 
void forward(){
  analogWrite(ENA, 100);
  analogWrite(ENB, 100);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("go forward!");
}
void forward2(){
  analogWrite(ENA, 100);
  analogWrite(ENB, 100);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("go forward!");
}
 
void back(){
  analogWrite(ENA, 100);
  analogWrite(ENB, 100);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("go back!");
}
 
void back2(){
  analogWrite(ENA, 100);
  analogWrite(ENB, 100);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("go back!");
}
void left(){
  analogWrite(ENA, 200);
  analogWrite(ENB, 200);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("go left!");
}
 
void right(){
  analogWrite(ENA, 200);
  analogWrite(ENB, 200);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("go right!");
}
 
void stop(){
   digitalWrite(ENA, 0);
   digitalWrite(ENB, 0);
   Serial.println("Stop!");
}
 
void setup(){
  myservo.attach(3);
  Serial.begin(9600);
}
 
void loop() {
  {
    myservo.write(70); //setservo position according to scaled value
    delay(10);
      myservo.write(-70);
      delay(10);
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
  if (LT_L) {
    left();
    while (LT_L);
  }
  else if (LT_M) {
    forward();
  }
  else if (LT_R and LT_M) {
    back();
    delay(30);
    right();
    while (LT_R);
  }
  else if (!LT_L and !LT_R and !LT_M)
  {
    back();
    delay(100);
    right();
    while (!LT_M);
  }
if (LT_R and LT_M and LT_L)
  {
    back();
    delay(50);
    left();
    if (LT_R and LT_M and LT_L){
      right();
      delay(100);
          forward();
          delay(3000);
                   while(!LT_R and LT_M and !LT_L);
              }
               }
             }

赞(3) 打赏
未经允许不得转载:福利年 » Arduino寻迹小车和推球对决

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏