好友
阅读权限10
听众
最后登录1970-1-1
|
暑假去了访学,搞了个寻迹小车,装载了两方面的内容,沿着黑线走,和三对三的推球对战,双方场地都有一样的球,小车自动将球推到对方场地,看谁最后场地中球最少了~
有两个难点,一个是小车寻迹的第一过程,一个是进入比赛场地后小车要按新的程序进行来往推球,不能再一直寻迹,用的Arduino写的代码,分享给大家看一下,小车真的很好玩haha[Asm] 纯文本查看 复制代码 //www.elegoo.com
//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.h> //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);
}
}
} |
-
差不多就是这样的场地
免费评分
-
参与人数 1 | 吾爱币 +3 |
热心值 +1 |
收起
理由
|
苏紫方璇
| + 3 |
+ 1 |
欢迎分析讨论交流,吾爱破解论坛有你更精彩! |
查看全部评分
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|