3 //micros when the pin goes HIGH
4 volatile unsigned long timer_start = 0;
5 //difference between timer_start and micros() is the length of time that the pin
6 //was HIGH - the PWM pulse length. volatile int pulse_time;
7 //this is the time that the last interrupt occurred.
8 //you can use this to determine if your receiver has a signal or not.
9 volatile int last_interrupt_time; //calcSignal is the interrupt handler
10 volatile int pulse_time;
13 //record the interrupt time so that we can tell if the receiver has a signal from the transmitter
14 last_interrupt_time = micros();
15 //if the pin has gone HIGH, record the microseconds since the Arduino started up
16 if(digitalRead(PWM_IN_PIN) == HIGH)
18 timer_start = micros();
20 //otherwise, the pin has gone LOW
23 //only worry about this if the timer has actually started
26 //record the pulse time
27 pulse_time = ((volatile int)micros() - timer_start);
34 void setup_pwm_in(int pin)
37 attachInterrupt(digitalPinToInterrupt(pin), calcSignal, CHANGE);
42 uint8_t oldSREG = SREG;
43 cli(); //disable interrupts to get a consistent value
49 inline long map(long x, long in_min, long in_max, long out_min, long out_max)
51 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
54 template <typename T> T limit(T x, T a, T b)
56 return (x<a) ? a : (x>b) ? b : x;
62 byte get_mode(unsigned int rotaryPulse, unsigned int numSteps)
64 return limit(map(rotaryPulse, MINPULSE, MAXPULSE, 0, numSteps), 0L, (long)numSteps-1);