极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9603|回复: 1

脉冲信号步进电机问题

[复制链接]
发表于 2014-7-15 09:55:01 | 显示全部楼层 |阅读模式
本帖最后由 マイナス37度 于 2014-7-15 10:27 编辑

想做个步进电机手轮,写代码时遇到点问题
准备用光电测速器做脉冲输入,这是我的代码

  1. int a = 13;
  2. int val;
  3. int n =digitalRead(4);
  4. int b = 1;
  5. int c;
  6. int d;
  7. void setup()
  8. {
  9. Serial.begin(9600);
  10. pinMode( a , OUTPUT);
  11. pinMode(4,INPUT);
  12. }

  13. void loop()
  14. {
  15. if (n==HIGH)                             //判断n是否为高电平,如果是执行下面的语句,不是则跳过。
  16.   {
  17. for (int x=2; x<=7; x++)
  18.    {
  19.     digitalWrite(a,LOW);
  20.     delay(b);
  21.     digitalWrite(a,HIGH);
  22.     }
  23. }
  24. }
复制代码




这段代码在执行时有问题,传感器处于high时会不停的执行,我只想他执行一次,想不出该怎么改了
还有就是正反转怎么用光电传感器控制
回复

使用道具 举报

 楼主| 发表于 2014-7-15 10:06:15 | 显示全部楼层
这样行不行,貌似可以

  1. int a = 13;
  2. int val;
  3. int n =digitalRead(4);
  4. int b = 1;
  5. int c = 0;
  6. int d;
  7. void setup()
  8. {
  9. Serial.begin(9600);
  10. pinMode( a , OUTPUT);
  11. pinMode(4,INPUT);
  12. }

  13. void loop()
  14. {
  15.   if (n==HIGH)
  16.   {
  17.     c = c + 1;
  18.   }
  19.   if (n==LOW)
  20.   {
  21.     c = c + 1;
  22.   }
  23. if (c==2)                             //判断n是否为高电平,如果是执行下面的语句,不是则跳过。
  24.   {
  25. for (int x=2; x<=7; x++)
  26.    {
  27.     digitalWrite(a,LOW);
  28.     delay(b);
  29.     digitalWrite(a,HIGH);
  30.     }
  31.     c = 1;
  32. }
  33. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 04:22 , Processed in 0.049987 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表