#define PWN1_IN 2
#define LED 13
unsigned long pwm1_start_ticks=0;
int pwm1_val=0;
float gain=1;//change it
void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
pinMode(PWN1_IN,INPUT);
pinMode(LED,OUTPUT);
attachInterrupt(0,interrupt_handle,CHANGE);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(pwm1_val);
digitalWrite(LED,HIGH);
delay(pwm1_val*gain);
digitalWrite(LED,LOW);
delay(pwm1_val*gain);
}
void interrupt_handle()
{
if (digitalRead( PWN1_IN ) == HIGH)
{
pwm1_start_ticks = micros(); // store the current micros() value
}
else
{
pwm1_val = micros() - pwm1_start_ticks;
}
}
忘了是抄的还是自己写的代码了,姑且就标注原创吧。