【ADI TMC2208 步进电机评估板】第六篇:手机蓝牙控制步进电机运动
<div class='showpostmsg'> 本帖最后由 Maker_kun 于 2024-9-6 21:01 编辑<p>ESP32自带蓝牙功能,通过无线蓝牙即可与手机进行通讯,通过手机发出的指令即可控制,为方便演示,通过简单指令控制步进电机正反转运动</p>
<table align="center" border="1" cellpadding="1" cellspacing="1" style="width: 555px;">
<tbody>
<tr>
<td>蓝牙指令</td>
<td>运动状态</td>
<td>备注</td>
</tr>
<tr>
<td>指令A</td>
<td>正转运动</td>
<td>蓝牙发送“A”</td>
</tr>
<tr>
<td>指令B</td>
<td>反转运动</td>
<td>蓝牙发送“B”</td>
</tr>
</tbody>
</table>
<p> </p>
<p>程序代码</p>
<p>打开案例库</p>
<p> </p>
<p>案例程序代码,在此基础进行修改</p>
<p> </p>
<p>代码</p>
<pre>
<code>#include "BluetoothSerial.h"
String device_name = "ESP32-BT-Slave";
// Check if Bluetooth is available
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
// Check Serial Port Profile
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Port Profile for Bluetooth is not available or not enabled. It is only available for the ESP32 chip.
#endif
const int stepPin=13; //步进引脚
const int dirPin=12;//方向引脚
const int enPin=14; //使能引脚
char BT_date;//蓝牙接收字符串
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin(device_name);//Bluetooth device name
//SerialBT.deleteAllBondedDevices(); // Uncomment this to delete paired devices; Must be called after begin
Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,HIGH); //打开使能
digitalWrite(dirPin,HIGH); //控制转动方向
}
void loop() {
int i;//控制转动步数
i=3000;
if (SerialBT.available())
{
BT_date=SerialBT.read();
}
if (BT_date==0x41)
{
SerialBT.write(BT_date);
BT_date=0x00;//清楚数据
digitalWrite(enPin,LOW); //打开电机使能
digitalWrite(dirPin,HIGH); //正转
while(i)
{
i--;
digitalWrite(stepPin,HIGH);
delayMicroseconds(100);
digitalWrite(stepPin,LOW);
delayMicroseconds(100);
}
}
if (BT_date==0x42)
{
SerialBT.write(BT_date);
BT_date=0x00;//清楚数据
digitalWrite(dirPin,LOW); //正转
digitalWrite(enPin,LOW); //打开电机使能
while(i)
{
i--;
digitalWrite(stepPin,HIGH);
delayMicroseconds(100);
digitalWrite(stepPin,LOW);
delayMicroseconds(100);
}
}
else
{
digitalWrite(enPin,HIGH); //关闭电机使能
}
delay(20);
}
</code></pre>
<p> </p>
<p>实物演示:</p>
<p>使用的蓝牙APP叫作SPP蓝牙串口助手,可以自定义发送字符串内容,有快捷键操作,很方便</p>
<p> </p>
<p>视频演示</p>
<p>微型步进电机滑台控制</p>
<p>e9137987f18014281316b470ca715e42</p>
<p>步进电机控制<br />
af1edf17eb4a60447d3c29cae1f79961<br />
</p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页:
[1]