文章目录[隐藏]
博客第一篇:Arduino 蓝牙小车代码
//电机设置
#define leftA_PIN 5
#define leftB_PIN 6
#define righA_PIN 9
#define righB_PIN10
//
void motor_pinint();//引脚初始化
void forward();//前进
void back();//后退
void turnleftorigin();//原地左转
void turnrightorigin();//原地右转
void turnleftforword();//左前转弯
void turnrightforword();//右前转弯
void _stop();//暂停
//
//蓝牙遥控
int receive;
void reve(void);
/函数/
void setup()
{
Serial.begin(9600);//串口波特率9600 motor_pinint();
}
void loop()
{
reve();
}
void reve(void)
{
receive=Serial.parseInt();
if(receive7)
{
forward();
delay(500);_stop();
}
//前进
else if(receive6)
{
back();
delay(500);_stop();
}
//后退
else if(receive8)
{
turnleftorigin(); delay(250);_stop();
}
//原地左转
else if(receive9)
{
turnrightorigin(); delay(250);_stop();
}
//原地右转
else if(receive3)
{
turnleftforword(); delay(500);_stop();
}
//左前转弯
else if(receive2)
{
turnrightforword();
delay(500);_stop();
}
//右前转弯
else if(receive==1)
{
_stop();
delay(500);_stop();
}
//暂停
}
//
//电机引脚初始化
void motor_pinint()
{
pinMode(leftA_PIN,OUTPUT);
//设置引脚为输出引脚 pinMode(leftB_PIN,OUTPUT);
//设置引脚为输出引脚 pinMode(righA_PIN,OUTPUT);
//设置引脚为输出引脚 pinMode(righB_PIN,OUTPUT);
//设置引脚为输出引脚
}
//
// 函数forward功能:控制小车前进void forward()
{
analogWrite(leftA_PIN,180); analogWrite(leftB_PIN,0);
//左轮前进 analogWrite(righA_PIN,180); analogWrite(righB_PIN,0);
//右轮前进 }
//
//函数back功能:控制小车后退void back()
{
analogWrite(leftA_PIN,0); analogWrite(leftB_PIN,180);
//左轮后退 analogWrite(righA_PIN,0); analogWrite(righB_PIN,180);
//右轮后退
}
//
//函数turnleftorigin功能:控制小车原地左转void turnleftorigin()
{
analogWrite(leftA_PIN,0); analogWrite(leftB_PIN,120);
//左前,左后两个轮子后转 analogWrite(righA_PIN,120);
analogWrite(righB_PIN,0);
//右前,右后两个轮子前转
}
//
//函数turnrightorigin功能:控制小车原地右转
void turnrightorigin()
{
analogWrite(leftA_PIN,120); analogWrite(leftB_PIN,0);
//左前,左后两个轮子前转 analogWrite(righA_PIN,0); analogWrite(righB_PIN,120);
//右前,右后两个轮子后转
}
//
//函数turnleftforword功能:控制小车左前转弯
void turnleftforword()
{
analogWrite(leftA_PIN,120); analogWrite(leftB_PIN,0);
//左轮缓慢前进 analogWrite(righA_PIN,200); analogWrite(righB_PIN,0);
//右轮快速前进
}
/*************************/
//函数turnrightforword功能:控制小车右前转弯
void turnrightforword()
{
analogWrite(leftA_PIN,200); analogWrite(leftB_PIN,0);
//左轮快速前进 analogWrite(righA_PIN,120); analogWrite(righB_PIN,0);
/ /右轮缓慢前进
}
/ *************************************/
//函数stop功能:控制小车停止
void _stop()
{
analogWrite(leftA_PIN,0); analogWrite(leftB_PIN,0);
//左轮静止不动
analogWrite(righA_PIN,0); analogWrite(righB_PIN,0);
//右轮静止不动
}
代码亲测有效😃,如果觉得有用的话,点赞,加关注哦,你的鼓励,就是我不断更新的动力
版权声明:本文为CSDN博主「小鱼爱arduino」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_63524713/article/details/122501656
暂无评论