欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

以Arduino为主板,利用红外遥控器控制42步进电机同步带滑台移动

程序员文章站 2022-03-11 15:22:02
本案例使用物品Arduino Mega1838红外一体接受器红外遥控器200mm同步带滑台DM542步进电机控制器开关电源线路连接自行体会DM542开关位置除了SW4为OFF,其它都为ON代码//添加库文件#include #include #include "IRremote.h"//定义宏#define DIR 12#define PUL 13#define MAX_DISTANCE 1400...

Arduino控制步进电机 Arduino控制滑台 Arduino红外遥控 红外遥控步进电机 红外遥控滑台

本案例使用物品

  • Arduino Mega
    以Arduino为主板,利用红外遥控器控制42步进电机同步带滑台移动
  • 1838红外一体接受器
    以Arduino为主板,利用红外遥控器控制42步进电机同步带滑台移动
  • 红外遥控器
    以Arduino为主板,利用红外遥控器控制42步进电机同步带滑台移动
  • 200mm同步带滑台
    以Arduino为主板,利用红外遥控器控制42步进电机同步带滑台移动
  • DM542步进电机控制器
    以Arduino为主板,利用红外遥控器控制42步进电机同步带滑台移动
  • 开关电源
    以Arduino为主板,利用红外遥控器控制42步进电机同步带滑台移动

线路连接

自行体会

DM542开关位置

除了SW4为OFF,其它都为ON

代码

调用IRremote库实现红外接收和解码

//添加库文件 #include <SPI.h> #include <Wire.h> #include "IRremote.h" //定义宏 #define DIR 12 #define PUL 13 #define MAX_DISTANCE 1400 #define POWER 0xFFA25D #define MENU 0xFFE21D #define TEST 0xFF22DD #define PLUS 0xFF02FD #define BACK 0xFFC23D #define BACKWARD 0xFFE01F #define PLAY 0xFFA857 #define FORWARD 0xFF906F #define ZERO 0xFF6897 #define MINUS 0xFF9867 #define CLEAR 0xFFB04F #define ONE 0xFF30CF #define TWO 0xFF18E7 #define THREE 0xFF7A85 #define FOUR 0xFF10EF #define FIVE 0xFF38C7 #define SIX 0xFF5AA5 #define SEVEN 0xFF42BD #define EIGHT 0xFF4AB5 #define NINE 0xFF52AD //定义函数与变量 int RECV_PIN = 22; int movein(int distance); int moveback(int distance); int slid_table(int num_input); int to_num(int wanttobenum); int location = 0; int travel = 0; int num = 0; //定义红外编码变量 IRrecv irrecv(RECV_PIN); decode_results results; //初始化接口和串口 void setup() { pinMode(DIR, OUTPUT); pinMode(PUL, OUTPUT); Serial.begin(9600); Serial.println("Enabling IRin"); irrecv.enableIRIn(); Serial.println("Enabled IRin"); } //主函数 void loop() { remote_control(); } //红外控制函数 int remote_control() { //开始红外解码 if (irrecv.decode(&results)) { //检测数字按钮 if (results.value == ZERO || results.value == ONE || results.value == TWO || results.value == THREE || results.value == FOUR || results.value == FIVE || results.value == SIX || results.value == SEVEN || results.value == EIGHT || results.value == NINE) { num = num * 10 + to_num(results.value); } //向前移动 else if (results.value == PLUS) { slid_table(num); num = 0; } //向后移动 else if (results.value == MINUS) { slid_table(-num); num = 0; } //移动50cm else if (results.value == PLAY) { slid_table(50); num = 0; } //复位 else if (results.value == POWER) { slid_table(-21); num = 0 ; } //向后移动1cm else if (results.value == BACKWARD) { slid_table(-1); num = 0; } //向前移动1cm else if (results.value == FORWARD) { slid_table(1); num = 0; } irrecv.resume();//红外复位 } } int slid_table(int num_input) { //厘米转换为脉冲数量 travel = 66.66 * num_input; if (travel > 0 && location + travel >= 0 && location + travel <= MAX_DISTANCE) { movein(travel); location = location + travel; travel = 0; } else if (travel < 0 && location + travel >= 0 && location + travel <= MAX_DISTANCE) { moveback(-travel); location = location + travel; travel = 0; } else if (location + travel <= 0 || (location + travel >= MAX_DISTANCE && location + travel <= MAX_DISTANCE)) { moveback(location); location = 0; travel = 0 ; } else if (location + travel > MAX_DISTANCE) { //如果移动距离大于最大值,按计算来回移动完成路程 while ( travel > 0 ) { if (travel >= MAX_DISTANCE - location) { movein(MAX_DISTANCE - location); travel = travel - (MAX_DISTANCE - location); location = MAX_DISTANCE; } else { movein( travel ); location = travel; travel = 0; } if (travel >= location) { moveback(location); travel = travel - location; location = 0; } else { moveback(travel); location = location - travel; travel = 0; } } } Serial.println(location); } //向前移动函数 int movein(int distance) { digitalWrite(DIR, HIGH); for (int i = 0; i < distance; i++) { digitalWrite(PUL, HIGH); delay(1); digitalWrite(PUL, LOW); } return 0; } //向后移动函数 int moveback(int distance) { digitalWrite(DIR, LOW); for (int i = 0; i < distance; i++) { digitalWrite(PUL, HIGH); delay(1); digitalWrite(PUL, LOW); } return 0; } //红外编码转为数字 int to_num(int wanttobenum) { if (wanttobenum == 26775) { return 0; } else if (wanttobenum == 12495) { return 1; } else if (wanttobenum == 6375) { return 2; } else if (wanttobenum == 31365) { return 3; } else if (wanttobenum == 4335) { return 4; } else if (wanttobenum == 14535) { return 5; } else if (wanttobenum == 23205) { return 6; } else if (wanttobenum == 17085) { return 7; } else if (wanttobenum == 19125) { return 8; } else if (wanttobenum == 21165) { return 9; } return 0; } 

完成效果

暂未上传

本文地址:https://blog.csdn.net/weixin_44196141/article/details/107963352

相关标签: arduino