写一个按键按下一次,控制亮灯0.6秒,灯灭0.6秒,闪灯循环两次后舵机从0°旋转到90°的程序
时间: 2024-05-15 16:20:02 浏览: 53
以下为代码实现:
```C++
#include <Arduino.h>
const int buttonPin = 2;
const int ledPin = 13;
const int servoPin = 9;
int buttonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(servoPin, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
delay(600);
digitalWrite(ledPin, LOW);
delay(600);
digitalWrite(ledPin, HIGH);
delay(600);
digitalWrite(ledPin, LOW);
delay(600);
digitalWrite(ledPin, HIGH);
delay(600);
digitalWrite(ledPin, LOW);
delay(600);
digitalWrite(ledPin, HIGH);
delay(600);
digitalWrite(ledPin, LOW);
for (int i = 0; i <= 90; i++) {
digitalWrite(servoPin, HIGH);
delay(15);
digitalWrite(servoPin, LOW);
delayMicroseconds(1500 - i*10);
}
delay(1000);
for (int i = 90; i >= 0; i--) {
digitalWrite(servoPin, HIGH);
delay(15);
digitalWrite(servoPin, LOW);
delayMicroseconds(1500 - i*10);
}
}
}
```
当你按下按键时,LED灯会闪烁两次,之后舵机会从0°旋转到90°,再从90°旋转回0°。
阅读全文