Create a gist now

Instantly share code, notes, and snippets.

Read PWM input with Arduino. I used it to check the pulse lengths on a Futaba RC receiver to set the endpoints.
const int pin = 2;
const int numSamples = 1000;
volatile unsigned long microseconds;
volatile byte index = 0;
volatile unsigned long results[numSamples];
void setup() {
Serial.begin(115200);
pinMode(pin, INPUT);
attachInterrupt(0, analyze, CHANGE);
results[0] = 0;
}
void analyze() {
if (index < numSamples) {
if (index > 0) {
results[index] = micros() - microseconds;
}
index = index + 1;
}
microseconds = micros();
}
void loop() {
if (index >= numSamples) {
for(byte i=0; i < numSamples; i++) {
if (results[i] < 3000) {
Serial.println(results[i]);
}
}
index = 0;
Serial.println("--");
}
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment