Arduino 各种模块篇 舵机 Modules - Servo

舵机

舵机是一种非常有意思的,多级齿轮增大扭力的widget。非常有用。在很多方面。

Servo is very useful. Now Please check this out : What is Servo Control (from wikipedia)

Variations

When a pulse is sent to a servo that is less than 1.5 ms the servo rotates to a position and holds its output shaft some number of degrees counterclockwise from the neutral point. When the pulse is wider than 1.5 ms the opposite occurs. The minimal width and the maximum width of pulse that will command the servo to turn to a valid position are functions of each servo. Different brands, and even different servos of the same brand, will have different maximum and minimums. Generally the minimum pulse will be about 1 ms wide and the maximum pulse will be 2 ms wide.

See also

 

 

Now we go to implement it on Arduino.

 

Strongly it is recommended that you NEED A 9V~1A ADAPTER to do this experiment.

我们引用下面的文章

http://www.geek-workshop.com/thread-137-1-1.html

arduino学习笔记23 - 任意输出舵机角度实验

[

使用的Arduino
IDE 1.0.1 可行

作者 弘毅

 

本次实验的器材非常简单,arduino控制板一个,标准舵机一个,电池盒(外接电源)一个。(一定要使用外接电源,直接使用USB供电,有烧毁USB的危险。

先上硬件连接图


Snap1.jpg

IMGP5472_调整大小.JPG

然后把下面代码编译,下载进入arduino控制板中。(感谢坏鸟童鞋提供的代码)

需要查看的东西ASCII表

ARDUINO 代码复制打印
  1. int i,val;
  2. char a[3];
  3. boolean display;
  4. #include <Servo.h>
  5. Servo servo1;
  6.  
  7. void setup()
  8. {
  9.   Serial.begin(9600);
  10.   servo1.attach(4);//舵機一接pin4
  11. }
  12.  
  13. void loop()
  14. {
  15.   if (Serial.available()){        //如果有数据输入.....
  16.     delay(30);                    //等待30毫秒让所有输入数据从串口传输完毕.....
  17.     if (Serial.available() <= 3){ //如果输入数据位数'<=3'.....
  18.       while (Serial.available()){ //开始读取数据直到[串口输入缓存被清空]
  19.         a[i++] = Serial.read();   //读取数据到[数组"a"]
  20.       }
  21.       display = 1;                    //数据读取完毕以后'打开'显示输出开关
  22.     }
  23.     else {                        //如果输入数据位数'>3'.....
  24.       Serial.flush();             //刷新串口输入缓存
  25.     }
  26.   }
  27. /*======================直接通过串口返回输入数值模块======================
  28.   if (display)                    //如果[显示输出开关]被'打开'则显示[数组"a"]的数据
  29.   {
  30.     for (i = 0; i <= sizeof(a); i++)
  31.     {
  32.      Serial.print("a[");
  33.      Serial.print(i);
  34.      Serial.print("]= ");
  35.       Serial.print(a[i]);
  36.       Serial.print(" | ");
  37.     }
  38.     Serial.println();
  39.     display = 0;                  //显示完毕'关闭'显示输出开关
  40.     Serial.flush();               //刷新串口输入缓存
  41.     for (i = 0; i <= 3; i++)      //重置[数组"a"]
  42.     {
  43.       a[i] = 0;
  44.     }
  45.     i = 0;                        //重置"计数变量"[i]
  46.   }
  47. //=======================通过加减符号控制舵机增减一度转动=================*/
  48. if (a[0] == 43 && display){ //看ASCII表得到的,这是"-" 符号
  49. val++;
  50. servo1.write(val);
  51. Serial.println(val);
  52.     display = 0;                  //显示完毕'关闭'显示输出开关
  53.     Serial.flush();               //刷新串口输入缓存
  54.     for (i = 0; i <= 3; i++)      //重置[数组"a"]
  55.     {
  56.       a[i] = 0;
  57.     }
  58.     i = 0;                        //重置"计数变量"[i]
  59. }
  60. if (a[0] == 45 && display){
  61. val--;
  62. servo1.write(val);
  63. Serial.println(val);
  64.     display = 0;                  //显示完毕'关闭'显示输出开关
  65.     Serial.flush();               //刷新串口输入缓存
  66.     for (i = 0; i <= 3; i++)      //重置[数组"a"]
  67.     {
  68.       a[i] = 0;
  69.     }
  70.     i = 0;                        //重置"计数变量"[i]
  71. }
  72.  
  73. //========================判断及修正输入数据位数模块======================
  74. if (display)                    //如果[显示输出开关]被'打开'则显示[数组"a"]的数据
  75.   {
  76.    if (!a[2]){ //如果输入数据为两位数(最后一位空)
  77.      if (!a[1]){ //如果输入数据为一位数(最后两位空)
  78.        a[2] = a[0];
  79.        a[1] = 48;
  80.        a[0] = 48;
  81.    }
  82.    else {
  83.    a[2] = a[1];
  84.    a[1] = a[0];
  85.    a[0] = 48;
  86.    }
  87.  }
  88. //==============转换变量类型后输出给舵机且通过串口返回结果值==============
  89. for (i=0;i<=3;i++){ //变量类型:char to int (48为0的ASCII)
  90. a[i] -= 48;
  91. }
  92. val = 100*a[0] + 10*a[1] + a[2];
  93. // Serial.print("val: ");
  94. servo1.write(val);
  95. Serial.println(val);
  96.  
  97. /* int val2 = random(50); //int型变量加法测试
  98. val += val2;
  99. Serial.print("+");
  100. Serial.print(val2);
  101. Serial.print("=");
  102. Serial.println(val);
  103. */
  104.     display = 0;                  //显示完毕'关闭'显示输出开关
  105.     Serial.flush();               //刷新串口输入缓存
  106.     for (i = 0; i <= 3; i++)      //重置[数组"a"]
  107.     {
  108.       a[i] = 0;
  109.     }
  110.     i = 0;                        //重置"计数变量"[i]
  111. //    val = 0;
  112. }
  113. }

点击下图箭头指示按钮,打开串口监视器。

Snap2.jpg

实验效果如下:

posted @ 2013-04-10 10:52  spaceship9  阅读(915)  评论(0编辑  收藏  举报