Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7112916
  • 博文数量: 3857
  • 博客积分: 6409
  • 博客等级: 准将
  • 技术积分: 15948
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-02 16:48
个人简介

迷彩 潜伏 隐蔽 伪装

文章分类

全部博文(3857)

文章存档

2017年(5)

2016年(63)

2015年(927)

2014年(677)

2013年(807)

2012年(1241)

2011年(67)

2010年(7)

2009年(36)

2008年(28)

分类:

2012-06-12 08:18:22

原文地址:Arduino 控制步进电机 作者:soloforce


  1. int Pin0 = 8;
  2. int Pin1 = 9;
  3. int Pin2 = 10;
  4. int Pin3 = 11;
  5. int _step = 0;
  6. boolean dir = true;//正反转
  7. int stepperSpeed = 1;//电机转速,1ms一步
  8. void setup()
  9. {
  10.   pinMode(Pin0, OUTPUT);
  11.   pinMode(Pin1, OUTPUT);
  12.   pinMode(Pin2, OUTPUT);
  13.   pinMode(Pin3, OUTPUT);
  14. }
  15.  
  16. void loop()
  17. {
  18.   switch(_step){
  19.     case 0:
  20.       digitalWrite(Pin0, LOW);
  21.       digitalWrite(Pin1, LOW);
  22.       digitalWrite(Pin2, LOW);
  23.       digitalWrite(Pin3, HIGH);
  24.     break;
  25.     case 1:
  26.       digitalWrite(Pin0, LOW);
  27.       digitalWrite(Pin1, LOW);
  28.       digitalWrite(Pin2, HIGH);
  29.       digitalWrite(Pin3, HIGH);
  30.     break;
  31.     case 2:
  32.       digitalWrite(Pin0, LOW);
  33.       digitalWrite(Pin1, LOW);
  34.       digitalWrite(Pin2, HIGH);
  35.       digitalWrite(Pin3, LOW);
  36.     break;
  37.     case 3:
  38.       digitalWrite(Pin0, LOW);
  39.       digitalWrite(Pin1, HIGH);
  40.       digitalWrite(Pin2, HIGH);
  41.       digitalWrite(Pin3, LOW);
  42.     break;
  43.     case 4:
  44.       digitalWrite(Pin0, LOW);
  45.       digitalWrite(Pin1, HIGH);
  46.       digitalWrite(Pin2, LOW);
  47.       digitalWrite(Pin3, LOW);
  48.     break;
  49.     case 5:
  50.       digitalWrite(Pin0, HIGH);
  51.       digitalWrite(Pin1, HIGH);
  52.       digitalWrite(Pin2, LOW);
  53.       digitalWrite(Pin3, LOW);
  54.     break;
  55.       case 6:
  56.       digitalWrite(Pin0, HIGH);
  57.       digitalWrite(Pin1, LOW);
  58.       digitalWrite(Pin2, LOW);
  59.       digitalWrite(Pin3, LOW);
  60.     break;
  61.     case 7:
  62.       digitalWrite(Pin0, HIGH);
  63.       digitalWrite(Pin1, LOW);
  64.       digitalWrite(Pin2, LOW);
  65.       digitalWrite(Pin3, HIGH);
  66.     break;
  67.     default:
  68.       digitalWrite(Pin0, LOW);
  69.       digitalWrite(Pin1, LOW);
  70.       digitalWrite(Pin2, LOW);
  71.       digitalWrite(Pin3, LOW);
  72.     break;
  73.   }
  74.   if(dir){
  75.     _step++;
  76.   }else{
  77.     _step--;
  78.   }
  79.   if(_step>7){
  80.     _step=0;
  81.   }
  82.   if(_step<0){
  83.     _step=7;
  84.   }
  85.   delay(stepperSpeed);

  86. }

阅读(354) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~