尝试了一下,0~180度来回摆动:
- void setup() {
- // put your setup code here, to run once:
- pinMode(9, OUTPUT);
- digitalWrite(9,HIGH);
- delayMicroseconds(544);
- digitalWrite(9,LOW);
- }
- unsigned long time1=544;
- bool direction1=true;
- void loop() {
- // put your main code here, to run repeatedly:
- if(direction1)
- {
- time1++;
- digitalWrite(9,HIGH);
- delayMicroseconds(time1);
- digitalWrite(9,LOW);
- delay(5);
- if (time1>=2400)
- direction1=false;
- }
- else
- {
- time1--;
- digitalWrite(9,HIGH);
- delayMicroseconds(time1);
- digitalWrite(9,LOW);
- delay(5);
- if (time1<=544)
- direction1=true;
- }
- }
复制代码
|