本帖最后由 Bone 于 2020-6-8 10:54 编辑
可以自己用App Inventor2写一个手机端控制软件
[mw_shl_code=arduino,true]
/*
蓝牙控制步进电机
*/
//四线
int out1=8;
int out2=9;
int out3=10;
int out4=11;
//绝对位置控
int absposition=0;
int num=512; //点击一次的旋转拍数 8*num //4096拍外轴转动一圈
const int speedval=1; ///旋转速度
void setup()
{
//串口通信的设计
Serial.begin(9600);
//设置D8-D11脚为步进电机控制脚。
pinMode(out1,OUTPUT);
pinMode(out2,OUTPUT);
pinMode(out3,OUTPUT);
pinMode(out4,OUTPUT);
}
void loop() {
while(Serial.available())
{
char ValueBle=Serial.read();
switch (ValueBle) {
case 'F':
//Serial.println("开门");
eight_step_f(num,speedval);
wait();
absposition+=1;
break;
case 'B':
//Serial.println("关门");
eight_step_b(num,speedval);
wait();
absposition-=1;
break;
case 'S':
//Serial.println("复位");
if(absposition>0)
for (int i=0; i <absposition; i++){
eight_step_b(num,speedval);
wait();
}
if (absposition<0)
for (int i=0; i>absposition; i--){
eight_step_f(num,speedval);
wait();
}
absposition=0;
break;
default:
break;
}
Serial.println(absposition);
//Serial.write(absposition);
}
}
// 此函数正向驱动8*num拍 参数t控制速度
void eight_step_f(int num,int t) {
for (int i=0; i <= num; i++){
// (0001) 对应8拍不同的输出值。
digitalWrite(out1,LOW);
digitalWrite(out2,LOW);
digitalWrite(out3,LOW);
digitalWrite(out4,HIGH);
delay(t);
// (0011)
digitalWrite(out1,LOW);
digitalWrite(out2,LOW);
digitalWrite(out3,HIGH);
digitalWrite(out4,HIGH);
delay(t);
// (0010)
digitalWrite(out1,LOW);
digitalWrite(out2,LOW);
digitalWrite(out3,HIGH);
digitalWrite(out4,LOW);
delay(t);
// (0110)
digitalWrite(out1,LOW);
digitalWrite(out2,HIGH);
digitalWrite(out3,HIGH);
digitalWrite(out4,LOW);
delay(t);
// (0100)
digitalWrite(out1,LOW);
digitalWrite(out2,HIGH);
digitalWrite(out3,LOW);
digitalWrite(out4,LOW);
delay(t);
// (1100)
digitalWrite(out1,HIGH);
digitalWrite(out2,HIGH);
digitalWrite(out3,LOW);
digitalWrite(out4,LOW);
delay(t);
// (1000)
digitalWrite(out1,HIGH);
digitalWrite(out2,LOW);
digitalWrite(out3,LOW);
digitalWrite(out4,LOW);
delay(t);
// (1001)
digitalWrite(out1,HIGH);
digitalWrite(out2,LOW);
digitalWrite(out3,LOW);
digitalWrite(out4,HIGH);
delay(t);
}
}
// 此函数反向驱动8*num拍 参数t控制速度
void eight_step_b(int num,int t) {
for (int i=0; i <= num; i++){
// (1001)
digitalWrite(out1,HIGH);
digitalWrite(out2,LOW);
digitalWrite(out3,LOW);
digitalWrite(out4,HIGH);
delay(t);
// (1000)
digitalWrite(out1,HIGH);
digitalWrite(out2,LOW);
digitalWrite(out3,LOW);
digitalWrite(out4,LOW);
delay(t);
// (1100)
digitalWrite(out1,HIGH);
digitalWrite(out2,HIGH);
digitalWrite(out3,LOW);
digitalWrite(out4,LOW);
delay(t);
// (0100)
digitalWrite(out1,LOW);
digitalWrite(out2,HIGH);
digitalWrite(out3,LOW);
digitalWrite(out4,LOW);
delay(t);
// (0110)
digitalWrite(out1,LOW);
digitalWrite(out2,HIGH);
digitalWrite(out3,HIGH);
digitalWrite(out4,LOW);
delay(t);
// (0010)
digitalWrite(out1,LOW);
digitalWrite(out2,LOW);
digitalWrite(out3,HIGH);
digitalWrite(out4,LOW);
delay(t);
// (0011)
digitalWrite(out1,LOW);
digitalWrite(out2,LOW);
digitalWrite(out3,HIGH);
digitalWrite(out4,HIGH);
delay(t);
// (0001) 对应8拍不同的输出值。
digitalWrite(out1,LOW);
digitalWrite(out2,LOW);
digitalWrite(out3,LOW);
digitalWrite(out4,HIGH);
delay(t);
}
}
void wait() {
digitalWrite(out1,LOW);
digitalWrite(out2,LOW);
digitalWrite(out3,LOW);
digitalWrite(out4,LOW);
//delay(10);
}
[/mw_shl_code] |