espbt_car
可能需要看一些esp的pwm通道配置的文档
#include "Arduino.h" //蓝牙 #include "BluetoothSerial.h" #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it #endif BluetoothSerial SerialBT; const int Left_a = 27; const int Left_b = 14; const int Right_a = 12; const int Right_b = 13; const int freq = 5000; // 设置PWM的频率 const int La_Channel = 0; // 设置PWM的通道 const int Lb_Channel = 1; const int Ra_Channel = 2; const int Rb_Channel = 3; const int resolution = 8; // 设置PWM 占空比的分辨率 最大占空比为 2^8 = 256 //车 void PWM_init( ); //引脚初始化 void forward( ); //前进 void back( ); //后退 void turnLeftOrigin( ); //原地左 void turnRightOrigin( ); //原地右 void turnRightforword( ); //右前 void turnLeftforword( ); //左前 void turnLeftback( ); //左后 void turnRightback( ); //右后 void _stop(); //停车 //蓝牙 static String receive; void reve(); void bule_init(); void setup(){ //pwm初始化 PWM_init(); //蓝牙初始化 bule_init(); } void loop(){ reve(); } void PWM_init(){// PWM初始化,绑定PWM通道到GPIO上 //Left_a,La_Channel ledcSetup(La_Channel, freq, resolution); ledcAttachPin(Left_a, La_Channel); //Left_b,Lb_Channel ledcSetup(Lb_Channel, freq, resolution); ledcAttachPin(Left_b, Lb_Channel); //Right_a,Ra_Channel ledcSetup(Ra_Channel, freq, resolution); ledcAttachPin(Right_a, Ra_Channel); //Right_b,Rb_Channel ledcSetup(Rb_Channel, freq, resolution); ledcAttachPin(Right_b, Rb_Channel); } void reve() { if (Serial.available()) { //用于调试 SerialBT.write(Serial.read()); } if (SerialBT.available()){ receive = ""; receive = SerialBT.readString(); Serial.print("接收数据为:"); //用于调试 Serial.println(receive[0]); //用于调试 } if(receive[0]=='7') {forward( ) ;delay(500);_stop();}//前进 else if(receive[0]=='6') {back( ) ;delay(500);_stop();}//后退 else if(receive[0]=='1') {_stop() ;delay(500);_stop();}//停车 else if(receive[0]=='3') {turnLeftforword( ) ;delay(500);_stop();}//左前 else if(receive[0]=='2') {turnRightforword( );delay(500);_stop();}//右前 else if(receive[0]=='5') {turnLeftback( ) ;delay(500);_stop();}//左后 else if(receive[0]=='4') {turnRightback( ) ;delay(500);_stop();}//右后 else if(receive[0]=='8') {turnLeftOrigin( ) ;delay(250);_stop();}//原地左 else if(receive[0]=='9') {turnRightOrigin( ) ;delay(250);_stop();}//原地右 } void forward( ) { ledcWrite(La_Channel, 180); ledcWrite(Lb_Channel, 0); //左轮前进 ledcWrite(Ra_Channel, 180); ledcWrite(Rb_Channel, 0); //右轮前进 } void back( ) { ledcWrite(La_Channel,0); ledcWrite(Lb_Channel,180); //左轮后退 ledcWrite(Ra_Channel,0); ledcWrite(Rb_Channel,180); //右轮后退 } void turnLeftOrigin( ) { ledcWrite(La_Channel,0); ledcWrite(Lb_Channel,120); //左轮后退 ledcWrite(Ra_Channel,120); ledcWrite(Rb_Channel,0); //右轮前进 } void turnRightOrigin( ) { ledcWrite(La_Channel,120); ledcWrite(Lb_Channel,0); //左轮前进 ledcWrite(Ra_Channel,0); ledcWrite(Rb_Channel,120); //右轮后退 } void turnRightforword( ) { ledcWrite(La_Channel,200); ledcWrite(Lb_Channel,0); //左轮快前进 ledcWrite(Ra_Channel,120); ledcWrite(Rb_Channel,0); //右轮慢前进 } void turnLeftforword( ) { ledcWrite(La_Channel,120); ledcWrite(Lb_Channel,0); //左轮慢前进 ledcWrite(Ra_Channel,200); ledcWrite(Rb_Channel,0); //右轮快前进 } void turnRightback( ) { ledcWrite(La_Channel,0); ledcWrite(Lb_Channel,200); //左轮快后退 ledcWrite(Ra_Channel,0); ledcWrite(Rb_Channel,120); //右轮慢后退 } void turnLeftback( ) { ledcWrite(La_Channel,0); ledcWrite(Lb_Channel,120); //左轮慢后退 ledcWrite(Ra_Channel,0); ledcWrite(Rb_Channel,200); //右轮快后退 } void _stop() { ledcWrite(La_Channel,0); ledcWrite(Lb_Channel,0); //左轮静止不动 ledcWrite(Ra_Channel,0); ledcWrite(Rb_Channel,0); //右轮静止不动 } void bule_init() { Serial.begin(115200); SerialBT.begin("ESP32test"); //蓝牙设备名 Serial.println("The device started, now you can pair it with bluetooth!"); }
本文作者:ClingYang
本文链接:https://www.cnblogs.com/Yang-blackSun/p/18025356
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
本文作者:Yang-blackSun
本文链接:https://www.cnblogs.com/Yang-blackSun/p/18025356
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步