只是为了澄清,你想测量你的R / C接收器输出的信号的脉冲宽度吗?为此,您需要使用中断。我这样做的方式如下:
volatile int16_t pwm = 0; //pwm value volatile int16_t trig = 0; //timer value #define pin 7 //pin the interrupt is attached to void intHandler() //function to call on interrupt { if(digitalRead(pin)) //if the pin is HIGH, note the time { trig = micros(); } else { pwm = micros()-trig; //if it is low, end the time } } void setup(){ pinMode(pin, INPUT); //set the pin to input attachInterrupt(pin,intHandler,CHANGE); //attach the interrupt function "intHandler" to "pin" whenever the state changes Serial.begin(9600); //begin serial comms } void loop() { Serial.print("PWM = "); Serial.println(pwm); }
请注意,这可能仅适用于Arduino Due,它具有扩展的中断处理能力。但是,这应该让您了解如何做到这一点。中断功能仅适用于 某些针脚 ,这可能就是为什么pulseIn功能不适合你。