如何由电脑通过arduino控制步进电机

 我来答
味小熟卤味
2016-12-31 · 知道合伙人互联网行家
味小熟卤味
知道合伙人互联网行家
采纳数:2854 获赞数:22831
从事网络营销、餐饮创业11年,餐饮小而美创业导师

向TA提问 私信TA
展开全部
int val=0;
int i=0;
int Pin0 = 8;
int Pin1 = 9
;int Pin2 = 10;
int Pin3 = 11;
int step = 0;
int stepperSpeed = 1;

void setup()
{
pinMode(Pin0, OUTPUT);
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);
Serial.begin(9600);
}

void loop()
{
val=Serial.read();
if (val=='a')
for(i=0;i<100;i++)
{
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
}

if (val=='b')
for(i=100;i>0;i--)
{
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式