完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
blinker控制步进电机起保停,正反转,加减速(Arduino uno+esp8266+TB6600驱动器)
本文针对上一篇的《blinker控制步进电机起保停,正反转(Arduino uno+esp8266+TB6600驱动器)》新增加减速控制按键与《外部按键中断精准控制步进电机起保停,正反转,加减速Arduino+TB6600驱动器)》搭配实现联合控制 将步进电机控制接入blinker平台,通过物联网,使用手机APP来控制步进电机起保停,正反转,加减速控制。 *程序源码: //****库***** #define BLINKER_WIFI //官方wifi协议库 #define BLINKER_PRINT Serial //串口协议库 #include 《Blinker.h》 //blinker官方库 #define TEXTE_ip “tex-ip” #define TEXTE_t “tex-t” #define Motorz D7 //DIR+,GPIO13 #define Motorr D8 //DIR-,GPIO15 #define button D6 //stop,GPIO12 #define speedup D5 //DIR-,GPIO14 #define speeddown D3 //stop,GPIO0 //#define reset_button D2 //GPIO4 const unsigned char cout[6]={D3,D4,D5,D6,D7,D8}; //****wifi、设备的定义**** char auth[] = “64116c5fa49f”; //设备key号 char ssid[] = “MERCURY_D268G”; //wifi名称 char pswd[] = “pba5ayzk”; //wifi密码 static unsigned int speeds = 500; //io口定义 //int value = 0;//读取的原始模拟数值 //int voltage = 0;//实际电压值 boolean state1 = true; boolean state2 = true; boolean state3 = true; //****新建组件对象**** /********************************************************************************************************* 新建数据类型组件对象,作用:将数据传输到手机blinker app *********************************************************************************************************/ BlinkerText Text_ip(“tex-ip”);//ip地址显示 BlinkerText Text_t(“tex-t”);//显示在线时间 BlinkerNumber Number1(“num-xinhao”); //定义wifi信号强度键名 BlinkerButton Button1(“btn-zon”); //设置app按键的键名 BlinkerButton Button2(“btn-rol”); //设置app按键的键名 BlinkerButton Button3(“btn-stop”); //设置app按键的键名 BlinkerButton Button4(“btn-reset”); //设置app按键的键名 BlinkerButton Button5(“btn-speedup”); //设置app按键的键名 BlinkerButton Button6(“btn-speeddown”); // 按下按键即会执行该函数 void button1_callback(const String & state) {///正转 BLINKER_LOG(“电机状态: ”, state); if (state == “on”) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); //板载LED状态切换 //state == BLINKER_CMD_BUTTON_TAP 按键按下 //state == BLINKER_CMD_BUTTON_RELEASED按键复位 //state == BLINKER_CMD_ON 自锁按键打开 //state == BLINKER_CMD_OFF自锁按键关闭 digitalWrite(Motorz, LOW);//D7方向 Button1.icon(“fal fa-space-shuttle”); Button1.color(“#FF0000”); //1#按钮按下时,app按键颜色状态显示是红色 // 反馈开关状态 Button1.text(“正转已启动”); Button1.print(“on”); BLINKER_LOG (“正转已启动”); //串口打印 Blinker.vibrate(); //开启继电器时反馈,让手机震动 } else if (state == “off”) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); //板载LED状态切换 digitalWrite(Motorz, HIGH); digitalWrite(button, HIGH); delay(200); digitalWrite(button, LOW); Button1.color(“#0000FF”); //3#按钮没有按下时,app按键颜色状态显示是黑色 Button1.text(“正转已关闭”); Button1.print(“off”); BLINKER_LOG (“正转已关闭”); } } void button2_callback(const String & state) { BLINKER_LOG(“反转状态: ”, state); if (state == “on”) {//反转 digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); //板载LED状态切换 digitalWrite(Motorr, LOW); Button2.icon(“fal fa-recycle”); Button2.color(“#FF0000”); //按钮按下时,app按键颜色状态显示是红色 // 反馈开关状态 Button2.text(“反转已启动”); Button2.print(“on”); BLINKER_LOG (“反转已启动”); //串口打印 Blinker.vibrate(); //开启继电器时反馈,让手机震动 } else if (state == “off”) { digitalWrite(Motorr, HIGH); digitalWrite(button, HIGH); delay(200); digitalWrite(button, LOW); Button2.color(“#0000FF”); //3#按钮没有按下时,app按键颜色状态显示是黑色 Button2.text(“反转已关闭”); Button2.print(“off”); BLINKER_LOG (“反转已关闭”); } } void button3_callback(const String & state) { BLINKER_LOG(“电机状态: ”, state); if (state == “on”) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); //板载LED状态切换 digitalWrite(button, HIGH); delay(200); digitalWrite(button, LOW); Button3.icon(“fas fa-ban”); Button3.color(“#FF0000”); //3#按钮按下时,app按键颜色状态显示是红色 // 反馈开关状态 Button3.text(“急停已按下”); Button3.print(“on”); BLINKER_LOG (“MotorStar”); //串口打印 Blinker.vibrate(); //开启继电器时反馈,让手机震动 } else if (state == “off”) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); //D4--GPIO2板载LED状态切换 digitalWrite(button, !digitalRead(button)); delay(200); digitalWrite(button, !digitalRead(button)); Button3.color(“#0000FF”); //3#按钮没有按下时,app按键颜色状态显示是黑色 Button3.text(“急停”); Button3.print(“off”); BLINKER_LOG (“MotorStop”); } } void button4_callback(const String & state) { BLINKER_LOG(“电机状态: ”, state); if (state == “tap”) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); //板载LED状态切换 digitalWrite(button, HIGH); digitalWrite(Motorz, HIGH); digitalWrite(Motorr, HIGH); Button4.icon(“far fa-cogs”); Button4.color(“#FF0000”); //3#按钮按下时,app按键颜色状态显示是红色 // 反馈开关状态 Button4.text(“复位”); Button4.print(“on”); BLINKER_LOG (“Motor reset”); //串口打印 Blinker.vibrate(); //开启继电器时反馈,让手机震动 } } //《i 》《/i》fad fa-angle-double-up fas fa-chevron-double-down void button5_callback(const String & state) { BLINKER_LOG(“电机加速: ”, state); if (state == “tap”) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); //板载LED状态切换 digitalWrite(speedup, LOW); delay(200); digitalWrite(speedup, HIGH); Button5.icon(“fad fa-chevron-double-up”); Button5.color(“#FF0000”); //3#按钮按下时,app按键颜色状态显示是红色 // 反馈开关状态 Button5.text(“加速”); Button5.print(“on”); BLINKER_LOG (“速度+50”); //串口打印 Blinker.vibrate(); //开启继电器时反馈,让手机震动 } } void button6_callback(const String & state) { BLINKER_LOG(“电机减速: ”, state); if (state == “tap”) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); //板载LED状态切换 digitalWrite(speeddown, LOW); delay(200); digitalWrite(speeddown, HIGH); Button6.icon(“fad fa-chevron-double-down”); Button6.color(“#FF0000”); //3#按钮按下时,app按键颜色状态显示是红色 // 反馈开关状态 Button6.text(“减速”); Button6.print(“on”); BLINKER_LOG (“速度-50”); //串口打印 Blinker.vibrate(); //开启继电器时反馈,让手机震动 } } //心跳包函数 void heartbeat() { unsigned long timecnt = millis(); unsigned long fen1 = timecnt / 60000L; //计算取商 unsigned long fen2 = fen1 % 60L; //计算分钟取余 unsigned int hour1 = fen1 / 60L; //计算小时取商 unsigned int hour2 = hour1 % 24L; //计算小时取余 unsigned int day1 = hour1 / 24; //计算天 String str = String(day1) + “:” + String(hour2) + “:” + String(fen2); Text_t.icon(“far fa-cloud-upload-alt”); Text_t.print(str); Text_t.color(“#33ff00”); //button1状态 Button1.icon(“fal fa-space-shuttle”); Button1.color(“#0000FF”); //1#按钮按下时,app按键颜色状态显示是红色 //button2状态 Button2.icon(“fal fa-recycle”); Button2.color(“#0000FF”); //1#按钮按下时,app按键颜色状态显示是红色 //button3状态 Button3.icon(“fas fa-ban”); Button3.color(“#0000FF”); //1#按钮按下时,app按键颜色状态显示是红色 //button4状态 Button4.icon(“far fa-cogs”); Button4.color(“#0000FF”); //1#按钮按下时,app按键颜色状态显示是红色 //button5状态 Button5.icon(“far fa-cogs”); Button5.color(“#0000FF”); //1#按钮按下时,app按键颜色状态显示是红色 //button6状态 Button6.icon(“far fa-cogs”); Button6.color(“#0000FF”); //1#按钮按下时,app按键颜色状态显示是红色 Number1.icon(“fal fa-wifi”); Number1.print(WiFi.RSSI()); Number1.color(“#ff6600”); Text_ip.icon(“far fa-location-arrow”); Text_ip.print(WiFi.localIP().toString().c_str()); Text_ip.color(“#00ff00”); } void setup() { // ****初始化串口 Serial.begin(115200); BLINKER_DEBUG.stream(Serial);//开启调试信息 //**** 初始化IO口,包括板载有LED的IO,D4 int i; for(i=0;i《6;i++){ pinMode(cout, OUTPUT);delay(20);digitalWrite(cout, HIGH); } // pinMode(LED_BUILTIN, OUTPUT); //将数字引脚LED_BUILTIN初始化为输出。 // pinMode(Motorz, OUTPUT); // digitalWrite(Motorz, HIGH); // pinMode(Motorr, OUTPUT); // digitalWrite(Motorr, HIGH); // pinMode(button, OUTPUT);//停止 // digitalWrite(button, HIGH); // ****初始化blinker Blinker.begin(auth, ssid, pswd); //****app关联函数**** Button1.attach(button1_callback); //app上操作必须的注册回调函数关联按键名“Button1”和判断程序“button1_callback“-- Button2.attach(button2_callback); //app上操作必须的注册回调函数关联按键名“Button2”和判断程序“button2_callback”-- Button3.attach(button3_callback); //app上操作必须的注册回调函数关联按键名“Button2”和判断程序“button2_callback“-- Button4.attach(button4_callback); Button5.attach(button5_callback); Button6.attach(button6_callback); Blinker.attachHeartbeat(heartbeat); //注册一下心跳包 } void loop() { // value = analogRead(A0); // //次函数就可以把数值进行范围映射,可以测量的最大值为4095,而我实际的最大测量电压为3.3v // voltage = map(value, 0, 1024, 0, 330); // Serial.println((float)voltage/100.00); //保留两位小数 Blinker.run(); } |
|
|
|
只有小组成员才能发言,加入小组>>
647浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 21:00 , Processed in 1.167895 second(s), Total 48, Slave 42 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号