您的位置:首页 > 产品设计 > UI/UE

Arduino编程->步进电机角度控制

2017-07-20 10:18 573 查看
 


#include<Stepper.h>

#define KEY 2 //定义 key_1 I/O number is 2
// 参考文件
/*
28BYJ-48.pdf

该参数根据电机每一转的步数来修改
*/
const int stepsPerRevolution = 256;
const int stepsDelay = 10000;
int stepin=0;
int count=1;
/*
steps=5.68888 旋转1度
steps=170.666 旋转30度
steps=256 旋转45度
steps=341.333 旋转60度
steps=512 旋转90度
steps=1024 旋转180度
steps=2048 旋转360度
*/
// 初始化步进电机要使用的Arduino的引脚编号
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup()
{
// 设置转速,单位r/min
myStepper.setSpeed(20);

// 初始化串口
Serial.begin(9600);
}
void clockwise()
{
// 顺时针一次旋转
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(stepsDelay);
}
void anticlockwise()
{
// 逆时针一次旋转
Serial.println("counterclockwise");
myStepper.step(-2048);
delay(500);
}
void loop01()
{
int i;
while(1)
{
i=analogRead(5);

if(i>900) //开关打开
{

if((count==0)&&(stepin<8))
{
clockwise();
count=1;
stepin=stepin+1;
}
if((count==0)&&(stepin==8))
{
anticlockwise();
count=1;
stepin=0;
}
delay(1000);
}
else if(i<10)//开关关闭
{
count=0;
}
}

}

void loop()
{
// 顺时针一次旋转
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(stepsDelay);
// 顺时针一次旋转
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(stepsDelay);
// 顺时针一次旋转
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(stepsDelay);
// 顺时针一次旋转
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(stepsDelay);
// 顺时针一次旋转
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(stepsDelay);
// 顺时针一次旋转
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(stepsDelay);
// 顺时针一次旋转
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(stepsDelay);
// 顺时针一次旋转
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(stepsDelay);

// 逆时针一次旋转
Serial.println("counterclockwise");
myStepper.step(-2048);
delay(500);
}
void main1()
{

int i;
while(1)
{
i=analogRead(5);
if(i>900)
{
// 顺时针一次旋转
Serial.println("clockwise");
myStepper.step(2048);
delay(1000);

// 逆时针一次旋转
Serial.println("counterclockwise");
myStepper.step(-2048);
delay(5000);
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: