Arduino 舵机
舵机
舵机是一种非常有意思的,多级齿轮增大扭力的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.
本次实验的器材非常简单,arduino控制板一个,标准舵机一个,电池盒(外接电源)一个。(一定要使用外接电源,直接使用USB供电,有烧毁USB的危险。)
先上硬件连接图
然后把下面代码编译,下载进入arduino控制板中。(感谢坏鸟童鞋提供的代码)
需要查看的东西ASCII表:
ARDUINO 代码:
int i,val;
char a[3];
boolean display;
#include <Servo.h>
Servo servo1;
void setup()
{
Serial.begin(9600);
servo1.attach(4);//舵機一接pin4
}
void loop()
{
if (Serial.available()){ //如果有数据输入.....
delay(30); //等待30毫秒让所有输入数据从串口传输完毕.....
if (Serial.available() <= 3){ //如果输入数据位数'<=3'.....
while (Serial.available()){ //开始读取数据直到[串口输入缓存被清空]
a[i++] = Serial.read(); //读取数据到[数组"a"]
}
display = 1; //数据读取完毕以后'打开'显示输出开关
}
else { //如果输入数据位数'>3'.....
Serial.flush(); //刷新串口输入缓存
}
}
/*======================直接通过串口返回输入数值模块======================
if (display) //如果[显示输出开关]被'打开'则显示[数组"a"]的数据
{
for (i = 0; i <= sizeof(a); i++)
{
Serial.print("a[");
Serial.print(i);
Serial.print("]= ");
Serial.print(a[i]);
Serial.print(" | ");
}
Serial.println();
display = 0; //显示完毕'关闭'显示输出开关
Serial.flush(); //刷新串口输入缓存
for (i = 0; i <= 3; i++) //重置[数组"a"]
{
a[i] = 0;
}
i = 0; //重置"计数变量"[i]
}
//=======================通过加减符号控制舵机增减一度转动=================*/
if (a[0] == 43 && display){ //看ASCII表得到的,这是"-" 符号
val++;
servo1.write(val);
Serial.println(val);
display = 0; //显示完毕'关闭'显示输出开关
Serial.flush(); //刷新串口输入缓存
for (i = 0; i <= 3; i++) //重置[数组"a"]
{
a[i] = 0;
}
i = 0; //重置"计数变量"[i]
}
if (a[0] == 45 && display){
val--;
servo1.write(val);
Serial.println(val);
display = 0; //显示完毕'关闭'显示输出开关
Serial.flush(); //刷新串口输入缓存
for (i = 0; i <= 3; i++) //重置[数组"a"]
{
a[i] = 0;
}
i = 0; //重置"计数变量"[i]
}
//========================判断及修正输入数据位数模块======================
if (display) //如果[显示输出开关]被'打开'则显示[数组"a"]的数据
{
if (!a[2]){ //如果输入数据为两位数(最后一位空)
if (!a[1]){ //如果输入数据为一位数(最后两位空)
a[2] = a[0];
a[1] = 48;
a[0] = 48;
}
else {
a[2] = a[1];
a[1] = a[0];
a[0] = 48;
}
}
//==============转换变量类型后输出给舵机且通过串口返回结果值==============
for (i=0;i<=3;i++){ //变量类型:char to int (48为0的ASCII)
a[i] -= 48;
}
val = 100*a[0] + 10*a[1] + a[2];
// Serial.print("val: ");
servo1.write(val);
Serial.println(val);
/* int val2 = random(50); //int型变量加法测试
val += val2;
Serial.print("+");
Serial.print(val2);
Serial.print("=");
Serial.println(val);
*/
display = 0; //显示完毕'关闭'显示输出开关
Serial.flush(); //刷新串口输入缓存
for (i = 0; i <= 3; i++) //重置[数组"a"]
{
a[i] = 0;
}
i = 0; //重置"计数变量"[i]
// val = 0;
}
}
点击下图箭头指示按钮,打开串口监视器。
完成,就可以观察自己的实验结果了。