重点:
例 Servo
接线:红棕黄(vcc、GND、信号引脚)
进阶:利用 newdata = map(sensorRead,0,1023,0,255); myservo.write(newdata); 改变舵机转动的度数
sensor = analogRead(引脚);//模拟信号引脚
newservo = map(sensor,0,1023,0,180);//将0-1023转换成舵机的0-180度
myservo.write(newservo);
#include <Servo.h>
Servo myservo; // create servo object to control a servo
//int potpin = 0; // analog pin used to connect the potentiometer
//int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(9); // PIN = 9attaches the servo on pin 9 to the servo object
}
void loop()
{
//val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
//val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
while(1)
{
myservo.write(30); // 90 sets the servo position according to the scaled value
delay(500);
myservo.write(-30); // 90 sets the servo position according to the scaled value
delay(500);
}
}