下面是程序 /* Button Turns on and off a light emitting diode(LED) connected to digital pin 13, when pressing a pushbutton attached to pin 2. The circuit: * LED attached from pin 13 to ground * pushbutton attached to pin 2 from +5V * 10K resistor attached to pin 2 from ground * Note: on most Arduinos there is already an LED on the board attached to pin 13. created 2005 by DojoDave <http://www.0j0.org> modified 30 Aug 2011 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Button */ // constants won't change. They're used here to // set pin numbers: const int buttonPin = 2; // the number of the pushbutton pin const int ledPinA = 8; const int ledPinB = 9; const int ledPinC = 10; const int ledPinD = 11; const int ledPinE = 12;// the number of the LED pin #include Servo myservo; // 定义舵机对象,最多八个 int pos = 0; // 定义舵机转动位置 // variables will change: int buttonState = 0; // variable for reading the pushbutton status void setup() { // initialize the LED pin as an output: pinMode(ledPinA, OUTPUT); pinMode(ledPinB, OUTPUT); pinMode(ledPinC, OUTPUT); pinMode(ledPinD, OUTPUT); pinMode(ledPinE, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); myservo.attach(7); // 设置舵机控制针脚 } void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPinA, HIGH); digitalWrite(ledPinB, LOW); digitalWrite(ledPinC, LOW); digitalWrite(ledPinD, LOW); delay(700); digitalWrite(ledPinD, HIGH); digitalWrite(ledPinE, LOW); for(pos = pos; pos>=1; pos-=1) { myservo.write(pos); delay(10); } } else { // turn LED off: digitalWrite(ledPinA, LOW); digitalWrite(ledPinD, LOW); digitalWrite(ledPinD, HIGH); // 0到180旋转舵机,每次延时15毫秒 for(pos = pos; pos < 180; pos += 1) { myservo.write(pos); delay(10); } delay(800); digitalWrite(ledPinB, HIGH); delay(400); digitalWrite(ledPinB, LOW); digitalWrite(ledPinC, HIGH); delay(400); digitalWrite(ledPinC, LOW); digitalWrite(ledPinB, HIGH); delay(400); digitalWrite(ledPinB, LOW); delay(500); digitalWrite(ledPinC, HIGH); delay(300); digitalWrite(ledPinC, LOW); delay(250); digitalWrite(ledPinB, HIGH); delay(100); digitalWrite(ledPinB, LOW); delay(100); digitalWrite(ledPinB, HIGH); delay(100); digitalWrite(ledPinB, LOW); digitalWrite(ledPinB, HIGH); delay(300); digitalWrite(ledPinB, LOW); delay(250); digitalWrite(ledPinC, HIGH); delay(100); digitalWrite(ledPinC, LOW); delay(100); digitalWrite(ledPinC, HIGH); delay(100); digitalWrite(ledPinC, LOW); digitalWrite(ledPinB, HIGH); delay(300); digitalWrite(ledPinB, LOW); delay(250); digitalWrite(ledPinB, HIGH); delay(100); digitalWrite(ledPinB, LOW); delay(100); digitalWrite(ledPinB, HIGH); delay(100); digitalWrite(ledPinB, LOW); delay(100); digitalWrite(ledPinC, HIGH); delay(80); digitalWrite(ledPinC, LOW); delay(600); digitalWrite(ledPinE, HIGH); delay(300); digitalWrite(ledPinE, LOW); delay(100); digitalWrite(ledPinE, HIGH); delay(100); digitalWrite(ledPinE, LOW); delay(100); digitalWrite(ledPinE, HIGH); delay(100); digitalWrite(ledPinE, LOW); } } |
广告投放|联系我们|手机|投稿|Archiver|About us|Advertise|遥控迷模型网|RCFans ( 粤ICP备10210518号-1 )
版权所有 RCFans.com © 2003-2016