- #include <SoftwareSerial.h>
- #include <Servo.h>
- #include "AccelStepper.h"
-
- // 电机步进方式定义
- #define FULLSTEP 4 //半步进参数
-
- // 定义步进电机引脚
-
- #define baPin1 5 // 一号28BYJ48连接的ULN2003电机驱动板引脚 in1
- #define baPin2 6 // 一号28BYJ48连接的ULN2003电机驱动板引脚 in2
- #define baPin3 7 // 一号28BYJ48连接的ULN2003电机驱动板引脚 in3
- #define baPin4 8 // 一号28BYJ48连接的ULN2003电机驱动板引脚 in4
-
-
- #define saPin1 A0 // 二号28BYJ48连接的ULN2003电机驱动板引脚 in1
- #define saPin2 A1 // 二号28BYJ48连接的ULN2003电机驱动板引脚 in2
- #define saPin3 A2 // 二号28BYJ48连接的ULN2003电机驱动板引脚 in3
- #define saPin4 A3 // 二号28BYJ48连接的ULN2003电机驱动板引脚 in4
-
- #define ytPin1 10 // 二号28BYJ48连接的ULN2003电机驱动板引脚 in1
- #define ytPin2 11 // 二号28BYJ48连接的ULN2003电机驱动板引脚 in2
- #define ytPin3 12 // 二号28BYJ48连接的ULN2003电机驱动板引脚 in3
- #define ytPin4 13 // 二号28BYJ48连接的ULN2003电机驱动板引脚 in4
-
- #define Servo_Pin 9
-
- #define Blu_RX A4
- #define Blu_TX A5
-
-
- #define MINVAL 400
- #define MAXVAL 620
-
- long lastsendtime = 0;
- int val[8] = {512, 512, 512, 512, 512, 512, 0, 0};
-
- // 定义两个步进电机对象
- // 定义中ULN2003驱动板引脚顺序为 in1-in3-in2-in4
- // 1号电机设置为全步进运行,2号电机设置为全步进运行
- AccelStepper ba(FULLSTEP, baPin1, baPin3, baPin2, baPin4);
- AccelStepper sa(FULLSTEP, saPin1, saPin3, saPin2, saPin4);
- AccelStepper yt(FULLSTEP, ytPin1, ytPin3, ytPin2, ytPin4);
- Servo jia; // create servo object to control a servo
-
- /////////////蓝牙定义部分///////////////////
- String string;
- char cmdMSP;
-
- SoftwareSerial mySerial(Blu_RX, Blu_TX);
-
- int ba_flag, sa_flag, yt_flag, jia_flag;
- int ba_speed = 300;
- int sa_speed = 300;
- int yt_speed = 300;
-
- int myangle = 87;
- int maxangle = 170;
- int minangle = 87;
- long lasttime;
-
- int flag[4];
-
-
- void BluSend()
- {
- if (millis() - lastsendtime > 500)
- {
- Serial.print("A");
- lastsendtime = millis();
- }
- }
- void BluReceive() {
- static enum _serial_state {
- IDLE,
- HEADER_START,
- HEADER_M,
- HEADER_ARROW,
- HEADER_CMD,
- } c_state = IDLE;
- while (Serial.available() > 0)
- {
- char c = Serial.read(); // 读串口缓冲区
- if (c_state == IDLE)
- { //串口状态空闲 等待HEADER_START状态的到来
- c_state = (c == '
- [/align]四、组装过程[/b][/size][/font][/align][font=新宋体]
- [/font][align=center][font=新宋体][media=x,500,375]https://www.ixigua.com/7031100524908937758?utm_source=xiguastudio[/media][/font][/align][font=新宋体]
- [/font][align=left][font=新宋体][size=4][b]五、效果演示[/b][/size][/font][/align][font=新宋体]
- [/font][align=center][font=新宋体][media=x,500,375]https://www.ixigua.com/7029505449263104520?utm_source=xiguastudio[/media][/font][/align][font=新宋体]
- [/font][align=center][align=left][b][font=新宋体][size=4]六、遥控器制作[/size][/font][/b][/align][/align][align=center][align=left][font=新宋体][size=4]遥控器外壳、内设及程序代码参考新贴内容更新:[/size][url=https://mc.dfrobot.com.cn/thread-311464-1-1.html]https://mc.dfrobot.com.cn/thread-311464-1-1.html[/url][/font][/align][/align][align=center][align=center][font=新宋体][size=6][b](未完待续)[/b][/size][/font][/align][/align][font=新宋体]
- [/font]
- ) ? HEADER_START : IDLE; //判定是$字符吗?是则进入HEADER_START状态
- if (c_state == IDLE)
- {
- }// evaluateOtherData(c); // evaluate all other incoming serial data
- }
- else if (c_state == HEADER_START)
- {
-
- c_state = (c == 'M') ? HEADER_M : IDLE; //判定是M字符吗?是则进入HEADER_M状态
- }
- else if (c_state == HEADER_M)
- {
-
- c_state = (c == '>') ? HEADER_ARROW : IDLE; //判定是>字符吗?是则进入HEADER_ARROW状态
- }
- else if (c_state == HEADER_ARROW)
- {
- string = "";
- cmdMSP = c; //接收 指令(code)
- c_state = HEADER_CMD; //接收到指令,进入HEADER_CMD状态
- }
- else if (c_state == HEADER_CMD)
- {
- if (c != ';')
- string += c;
- else
- {
- c_state = IDLE; //返回IDLE状态
- evaluateCommand(); //数据处理程序
- }
- }
- }
- }
-
-
- void evaluateCommand() {
- // Serial.print("receive cmd:");
- // Serial.println(cmdMSP);
- // Serial.println(string);
- // mySerial.print("receive cmd:");
- // mySerial.println(cmdMSP);
- switch (cmdMSP)
- {
- case 'D':
- val[0] = atoi(strtok(string.c_str(), ",")) ;
- for (int i = 1; i < 8; i++)
- val[i] = atoi(strtok(NULL, ","));
-
- // sa_flag = atoi(strtok(string.c_str(), ",")) ;
- // jia_flag = atoi(strtok(NULL, ","));
- // ba_flag = atoi(strtok(NULL, ","));
- // yt_flag = atoi(strtok(NULL, ","));
- //
- // ba.setSpeed(ba_speed * ba_flag);
- // yt.setSpeed(yt_speed * yt_flag);
- // sa.setSpeed((-1)*sa_speed * sa_flag);
- break;
-
- default:
- {
-
- }
- }
- }
-
- void dataDeal()
- {
- for (int i = 0; i < 4; i++)
- {
- if (val[i] > MAXVAL)
- flag[i] = 1;
- else if (val[i] < MINVAL)
- flag[i] = -1;
- else
- flag[i] = 0;
- }
- sa_flag = flag[0];
- jia_flag = flag[1];
- ba_flag = flag[2];
- yt_flag = flag[3];
- ba_speed = map(val[4], 0, 1023, 100, 600);
- sa_speed = ba_speed;
- yt_speed = ba_speed;
- ba.setSpeed(ba_speed * ba_flag);
- yt.setSpeed(yt_speed * yt_flag);
- sa.setSpeed((-1)*sa_speed * sa_flag);
- }
-
- void setup()
- {
- ba.setMaxSpeed(800); // 设置电机最大速度为500
- sa.setMaxSpeed(800); // 设置电机最大速度为500
- yt.setMaxSpeed(800); // 设置电机最大速度为500
- // ba.setSpeed(ba_speed); // 初始化电机速度为300
- // sa.setSpeed(300); // 初始化电机速度为300
- // yt.setSpeed(300); // 初始化电机速度为300
-
- Serial.begin(9600);
- mySerial.begin(9600);
-
- jia.attach(Servo_Pin);
- }
-
- void loop()
- {
- BluReceive();
- BluSend();
- dataDeal();
- if (jia_flag)
- {
- if (millis() - lasttime > 5)
- {
- if (jia_flag == -1 && myangle < maxangle)
- {
- jia.write(++myangle);
- }
- else if (jia_flag == 1 && myangle > minangle)
- {
- jia.write(--myangle);
- }
- else
- {
- jia_flag = 0;
- }
- lasttime = millis();
- }
- }
- if (ba_flag)
- {
-
- ba.runSpeed();
- }
- if (sa_flag)
- {
-
- sa.runSpeed();
- }
- if (yt_flag)
- {
-
- yt.runSpeed();
- }
- }
复制代码
四、组装过程