智能小车项目开发(Arduino)

智能小车项目开发(Arduino)

文章目录

  • 智能小车项目开发(Arduino)
    • 循迹小车
      • 1.项目器件
      • 2.项目实现
    • 跟随小车
      • 1.项目器件
      • 2.项目实现
    • 避障小车
      • 1.项目器件
      • 2.项目实现

注意: 具体每个元器件的使用原理在此不做介绍,需要去单独学习,此章我只介绍具体的程序编写

三个功能的代码学起来很简单,,重要的是其中开发逻辑的一个认知。(每个都会用,联动起来就蒙了哈哈哈哈)

循迹小车

1.项目器件

2个红外循迹模块,1个L298N,智能小车套件,arduino mega2560开发板

2.项目实现

//1.引脚需要后期自己换
//2.其次里面涉及到了PWM调速,不会的可以先去了解一下,方便理解
//3.里面用的是控速转弯
#define leftTrail     1
#define rightTrail    2
#define leftWheel01   3
#define leftWheel02   4
#define rightWheel01  5
#define rightWheel02  6
void Trail_init()
{
    pinMode(leftTrail,INPUT);
    pinMode(rightTrail,INPUT);
}
void car_init()
{
    pinMode(leftWheel01,OUTPUT);
    pinMode(leftWheel02,OUTPUT);
    pinMode(rightWheel01,OUTPUT);
    pinMode(rightWheel02,OUTPUT);
}
//具体是前后左右,需要根据自己的接线调试
void go_forword()
{
    digitalWrite(leftWheel01,LOW);
    analogWrite(leftWheel02,200);
    digitalWrite(rightWheel01,LOW);
    analogWrite(rightWheel02,200);
}
void nogo()
{
    digitalWrite(leftWheel02,LOW);
    analogWrite(leftWheel01,0);
    digitalWrite(rightWheel02,LOW);
    analogWrite(rightWheel01,0);
}
void turn_left()
{
    digitalWrite(leftWheel01,LOW);
    analogWrite(leftWheel02,100);
    digitalWrite(rightWheel01,LOW);
    analogWrite(rightWheel02,220);
}
void turn_right()
{
    digitalWrite(leftWheel01,LOW);
    analogWrite(leftWheel02,220);
    digitalWrite(rightWheel01,LOW);
    analogWrite(rightWheel02,100 );
}
void setup()
{
    Trail_init();
    car_init();
}

void loop()
{ 
    //有黑线灯会熄灭,呈现出高电平
    if(analogRead(leftTrail)==1 && analogRead(rightTrail)==0)
    {
        turn_left();
    }
    if(analogRead(leftTrail)==0 && analogRead(rightTrail)==1)
    {
        turn_right();
    }
    if(analogRead(leftTrail)==1 && analogRead(rightTrail)==1)
    {
        nogo();
    }
    if(analogRead(leftTrail)==0 && analogRead(rightTrail)==0)
    {
        go_forword();
    }
}

跟随小车

1.项目器件

超声波模块,智能小车套件,L298N电机驱动,arduino mega2560开发板

2.项目实现

关于电机驱动都是通用的,所有直接上面的代码也可以

//超声波相关的代码
#define Echo D5	//吸收
#define Tirg D6	//发射,,给一个10us的高电平,触发
unsigned long dis;//unsigned的作用是:声明无符号的整数类型。
double dis;//也可以这样定义这个变量 
/**写一个检测高电平触发时间*/
unsigned long init_time()
{
    digitalWrite(Tirg,HIGH);
    delayMicroseconds(10);
    digitalWrite(Tirg,LOW);
    return pulseln(Echo,HIGH);
}

void setup(){
    pinMode(Echo,INPUT);
    pinMode(Tirg,OUTPUT);
    Serial.begin(115200);
}
void loop(){
    /*我感觉这个是换算起来比较麻烦的一个
    dis = init_time()/58;*/
        /还有一种计算方法就是,路程 =时间 * 速度(340m/s),,,2dis = time * 340m/s,这个时间是微秒,所以换算得0.017
    dis = init_time()*0.017;
    Serial.print(dis);
    Serial.println("cm");    //字符要加引号
    delay(1200);//写一个延迟
}
//根据超声波反馈回来的距离来判断小车的前进亦或者后退
#define leftWheel01   1
#define leftWheel02   2
#define rightWheel01  3
#define rightWheel02  4
#define Trig          5
#define Echo          6
unsigned long dis;//unsigned的作用是:声明无符号的整数类型。
unsigned long init_time()
{
    digitalWrite(Tirg,HIGH);
    delayMicroseconds(10);
    digitalWrite(Tirg,LOW);
    return pulseln(Echo,HIGH);
}
void wave_init()
{
    pinMode(Echo,INPUT);
    pinMode(Tirg,OUTPUT);
}
void car_init()
{
    pinMode(leftWheel01,OUTPUT);
    pinMode(leftWheel02,OUTPUT);
    pinMode(rightWheel01,OUTPUT);
    pinMode(rightWheel02,OUTPUT);
}
//具体是前后左右,需要根据自己的接线调试
void go_forword()
{
    digitalWrite(leftWheel01,LOW);
    analogWrite(leftWheel02,200);
    digitalWrite(rightWheel01,LOW);
    analogWrite(rightWheel02,200);
}
void stop()
{
    digitalWrite(leftWheel02,LOW);
    analogWrite(leftWheel01,0);
    digitalWrite(rightWheel02,LOW);
    analogWrite(rightWheel01,0);
}
void goback()
{
    digitalWrite(leftWheel02,LOW);
    analogWrite(leftWheel01,200);
    digitalWrite(rightWheel02,LOW);
    analogWrite(rightWheel01,200);
}
void turn_left()
{
    digitalWrite(leftWheel01,LOW);
    analogWrite(leftWheel02,100);
    digitalWrite(rightWheel01,LOW);
    analogWrite(rightWheel02,220);
}
void turn_right()
{
    digitalWrite(leftWheel01,LOW);
    analogWrite(leftWheel02,220);
    digitalWrite(rightWheel01,LOW);
    analogWrite(rightWheel02,100 );
}
void setup()
{
    car_init();
    wave_init()}

void loop()
{ 
    delay(50);每次得出来的超声波数据放缓50ms,为了使更顺畅
	dis = init_time()*0.017;//单位是cm
    if(dis >= 10 && dis <=40)
    {
        go_forword();
    }
    if(dis <= 10 && dis >= 0)
    {
        goback();
    }
     if(dis >= 40)
    {
        stop();
    }
}

避障小车

1.项目器件

超声波模块,舵机,智能小车套件,L298N电机驱动,arduino mega2560开发板

2.项目实现

//根据超声波反馈回来的距离来判断小车的前进亦或者后退
#include 		//包含舵机的头文件
#define PIN_SERVO D5	//定义一个舵机引脚
Servo myservo;			//给自己的舵机起一个名字
#define leftWheel01   1
#define leftWheel02   2
#define rightWheel01  3
#define rightWheel02  4
#define Trig          5
#define Echo          6
unsigned long dis;//unsigned的作用是:声明无符号的整数类型。
unsigned long init_time()
{
    digitalWrite(Tirg,HIGH);
    delayMicroseconds(10);
    digitalWrite(Tirg,LOW);
    return pulseln(Echo,HIGH);
}
void wave_init()
{
    pinMode(Echo,INPUT);
    pinMode(Tirg,OUTPUT);
}
void car_init()
{
    pinMode(leftWheel01,OUTPUT);
    pinMode(leftWheel02,OUTPUT);
    pinMode(rightWheel01,OUTPUT);
    pinMode(rightWheel02,OUTPUT);
}
//具体是前后左右,需要根据自己的接线调试
void go_forword()
{
    digitalWrite(leftWheel01,LOW);
    analogWrite(leftWheel02,200);
    digitalWrite(rightWheel01,LOW);
    analogWrite(rightWheel02,200);
}
void nogo()
{
    digitalWrite(leftWheel02,LOW);
    analogWrite(leftWheel01,0);
    digitalWrite(rightWheel02,LOW);
    analogWrite(rightWheel01,0);
}
void goback()
{
    digitalWrite(leftWheel02,LOW);
    analogWrite(leftWheel01,200);
    digitalWrite(rightWheel02,LOW);
    analogWrite(rightWheel01,200);
}
void turn_left()
{
    digitalWrite(leftWheel01,LOW);
    analogWrite(leftWheel02,100);
    digitalWrite(rightWheel01,LOW);
    analogWrite(rightWheel02,220);
}
void turn_right()
{
    digitalWrite(leftWheel01,LOW);
    analogWrite(leftWheel02,220);
    digitalWrite(rightWheel01,LOW);
    analogWrite(rightWheel02,100 );
}
void setup()
{
    car_init();
    wave_init();
    myservo.attach(PIN_SERVO);//连接舵机
}

void loop()
{ 
    delay(50);每次得出来的超声波数据放缓50ms,为了使更顺畅
	dis = init_time()*0.017;//单位是cm
  sssss.write(0);
  delay(1000);
  sssss.write(180);
  delay(1000);
}

你可能感兴趣的:(arduino系列,单片机,c语言,物联网,mcu)