Skip to content

Instantly share code, notes, and snippets.

@ivarvong
Last active January 1, 2016 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivarvong/8202180 to your computer and use it in GitHub Desktop.
Save ivarvong/8202180 to your computer and use it in GitHub Desktop.
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