看来的,分享给大家,用Arduino做的
原文:http://www.arduino.cn/thread-15212-1-1.html
视频:
http://www.tudou.com/v/Q6dBlYVcEOE/&resourceId=0_04_02_99/v.swf
程序:
- #include <Servo.h>
- Servo servo1;
- Servo servo2;
- Servo servo3;
- Servo servo4;
- int r;
- int h=0;
- int en=4;
- int state=7;
- int rx=0;
- int tx=1;
- int l=90;
- int f=90;
- int u=90;
- int delaytime=1000;
- void setup() {
- pinMode(en,OUTPUT);
- pinMode(state,INPUT);
- pinMode(rx,INPUT);
- pinMode(tx,OUTPUT);
- servo1.attach(3);
- servo1.write(179);
- delay(500);
- servo2.attach(6);
- servo2.write(90);
- delay(500);
- servo3.attach(9);
- servo3.write(90);
- delay(500);
- servo4.attach(11);
- servo4.write(90);
- delay(500);
- digitalWrite(en,HIGH);
- Serial.begin(9600);
- }
- void loop() {
-
- r=Serial.read();
- if(r!=-1){
- h=r;
-
- }
- switch(h)
- {
- case 99:
- servo1.write(135);
- delay(delaytime);
- break;
- case 110:
- servo1.write(179);
- delay(delaytime);
- break;
- case 108:
- l+=15;
- if(l>164)l=164;
- servo2.write(l);
- delay(delaytime);
- break;
- case 114:
- l-=15;
- if(l<15)l=15;
- servo2.write(l);
- delay(delaytime);
- break;
- case 117:
- u+=15;
- if(u>164)u=164;
- servo3.write(u);
- delay(delaytime);
- break;
- case 100:
- u-=15;
- if(u<15)u=15;
- servo3.write(u);
- delay(delaytime);
- break;
- case 102:
- f+=15;
- if(f>164)f=164;
- servo4.write(f);
- delay(delaytime);
- break;
- case 98:
- f-=15;
- if(f<15)f=15;
- servo4.write(f);
- delay(delaytime);
- break;
- case 0:
- delay(delaytime);
- }
- }
复制代码
这里还有一个相关视频
|