【Arduino】Arduino寻迹小车和推球对决
暑假去了访学,搞了个寻迹小车,装载了两方面的内容,沿着黑线走,和三对三的推球对战,双方场地都有一样的球,小车自动将球推到对方场地,看谁最后场地中球最少了~有两个难点,一个是小车寻迹的第一过程,一个是进入比赛场地后小车要按新的程序进行来往推球,不能再一直寻迹,用的Arduino写的代码,分享给大家看一下,小车真的很好玩haha//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);
}
}
} 听说国外中小学生就开始玩这个,我们大学才搞这个,这差距贼大 joyjaya 发表于 2019-8-27 09:48
老哥,可以再求一下M课网的Java攻城狮的资料吗!
哎呀,你等一下,我给你找找,我前两天清理了一波,怕被删除了 人工智能??
追逐太阳 发表于 2019-8-26 20:36
人工智能??
就普通的单片机编程 DIY了个4WD的寻迹,程序不知道有没问题,但是也不保证硬件出没出问题,一点也不走。愁死 现在我们这边初中学生已经开始研究四驱车编程巡线,行进中检测障碍物之类的了,现在科技太发达了,感觉我都赶不上时代了... 千年的承诺 发表于 2019-8-26 20:55
听说国外中小学生就开始玩这个,我们大学才搞这个,这差距贼大
所以这方面跟人家差远了啊…… 老哥,可以再求一下M课网的Java攻城狮的资料吗! 不需要驱动板吗? 52P 发表于 2019-8-27 09:58
不需要驱动板吗?
你指的那个?
页:
[1]
2