不好意思仓促了,这是完整的:#include <Servo.h>
const int LED1=5;
const int LED2=3;
const int LED3=6;
const int LED4=9;
const int LED5=10;
Servo myservo;
int pos=0;
void setup() {
myservo.attach(11);
}
void loop()
{
{for (pos = 0; pos <= 150; pos += 1) {
myservo.write(pos=30);
delay(1);
analogWrite(LED1,500);
delay(2000);
for (pos =30; pos <= 150; pos += 1) {
myservo.write(pos=60);
delay(1);
analogWrite(LED1,500);
analogWrite(LED2,500);
delay(2000);
for (pos = 60; pos <= 150; pos += 1) {
myservo.write(pos=90);
delay(1);
analogWrite(LED1,500);
analogWrite(LED2,500);
analogWrite(LED3,500);
delay(2000);
for (pos = 90; pos <= 150; pos += 1) {
myservo.write(pos=120);
delay(1);
analogWrite(LED1,500);
analogWrite(LED2,500);
analogWrite(LED3,500);
analogWrite(LED4,500);
delay(2000);
for (pos = 120; pos <= 150; pos += 1) {
myservo.write(pos=150);
delay(1);
analogWrite(LED1,500);
analogWrite(LED2,500);
analogWrite(LED3,500);
analogWrite(LED4,500);
analogWrite(LED5,500);
delay(2000);
for(pos = 150; pos>=120; pos-=1) //从150度到120度运动
{
myservo.write(pos=120); // 指定舵机转向的角度
delay(1); // 等待15ms让舵机到达指定位置
analogWrite(LED5,0);
delay(2000);
for(pos = 120; pos>=90; pos-=1) //从120度到90度运动
{
myservo.write(pos=90); // 指定舵机转向的角度
delay(1); // 等待15ms让舵机到达指定位置
analogWrite(LED4,0);
analogWrite(LED5,0);
delay(2000);
for(pos = 90; pos>=60; pos-=1) //从90度到60度运动
{
myservo.write(pos=60); // 指定舵机转向的角度
delay(1); // 等待15ms让舵机到达指定位置
analogWrite(LED3,0);
analogWrite(LED4,0);
analogWrite(LED5,0);
delay(2000);
for(pos = 60; pos>=30; pos-=1) //从60度到0度运动
{
myservo.write(pos=30); // 指定舵机转向的角度
delay(1); // 等待15ms让舵机到达指定位置
analogWrite(LED2,0);
analogWrite(LED3,0);
analogWrite(LED4,0);
analogWrite(LED5,0);
delay(2000);
for(pos = 30; pos>=0; pos-=1) //从30度到0度运动
{
myservo.write(pos=0); // 指定舵机转向的角度
delay(1); // 等待15ms让舵机到达指定位置
analogWrite(LED1,0);
analogWrite(LED2,0);
analogWrite(LED3,0);
analogWrite(LED4,0);
analogWrite(LED5,0);
delay(1000);
for (pos = 0; pos <= 150; pos += 1) {
myservo.write(pos=30);
delay(1);
analogWrite(LED1,500);
delay(2000);
for (pos =30; pos <= 150; pos += 1) {
myservo.write(pos=60);
delay(1);
analogWrite(LED1,500);
analogWrite(LED2,500);
delay(2000);
for (pos = 60; pos <= 150; pos += 1) {
myservo.write(pos=90);
delay(1);
analogWrite(LED1,500);
analogWrite(LED2,500);
analogWrite(LED3,500);
delay(2000);
for (pos = 90; pos <= 150; pos += 1) {
myservo.write(pos=120);
delay(1);
analogWrite(LED1,500);
analogWrite(LED2,500);
analogWrite(LED3,500);
analogWrite(LED4,500);
delay(2000);
for (pos = 120; pos <= 150; pos += 1) {
myservo.write(pos=150);
delay(1);
analogWrite(LED1,500);
analogWrite(LED2,500);
analogWrite(LED3,500);
analogWrite(LED4,500);
analogWrite(LED5,500);
delay(2000);
for(pos = 150; pos>=120; pos-=1) //从150度到120度运动
{
myservo.write(pos=120); // 指定舵机转向的角度
delay(1); // 等待15ms让舵机到达指定位置
analogWrite(LED5,0);
delay(2000);
for(pos = 120; pos>=90; pos-=1) //从120度到90度运动
{
myservo.write(pos=90); // 指定舵机转向的角度
delay(1); // 等待15ms让舵机到达指定位置
analogWrite(LED4,0);
analogWrite(LED5,0);
delay(2000);
for(pos = 90; pos>=60; pos-=1) //从90度到60度运动
{
myservo.write(pos=60); // 指定舵机转向的角度
delay(1); // 等待15ms让舵机到达指定位置
analogWrite(LED3,0);
analogWrite(LED4,0);
analogWrite(LED5,0);
delay(2000);
for(pos = 60; pos>=30; pos-=1) //从60度到0度运动
{
myservo.write(pos=30); // 指定舵机转向的角度
delay(1); // 等待15ms让舵机到达指定位置
analogWrite(LED2,0);
analogWrite(LED3,0);
analogWrite(LED4,0);
analogWrite(LED5,0);
delay(2000);
for(pos = 30; pos>=0; pos-=1) //从30度到0度运动
{
myservo.write(pos=0); // 指定舵机转向的角度
delay(1); // 等待15ms让舵机到达指定位置
analogWrite(LED1,0);
analogWrite(LED2,0);
analogWrite(LED3,0);
analogWrite(LED4,0);
analogWrite(LED5,0);
delay(1000);
}
}}}}}}}}}}}}}}}}}}}}} |