Skip to content

Instantly share code, notes, and snippets.

@codebudo
Created February 6, 2019 00:34
Show Gist options
  • Save codebudo/4273c5b57352914643f9320427b22151 to your computer and use it in GitHub Desktop.
Save codebudo/4273c5b57352914643f9320427b22151 to your computer and use it in GitHub Desktop.
Calculating PWM timing using interrupts.
#include <EnableInterrupt.h>
const int CH_1_PIN = 9;
volatile int pwmValue;
volatile unsigned long prevTime = 0;
void setup() {
Serial.begin(9600);
enableInterrupt(CH_1_PIN, rising, RISING);
}
void loop() {
delay(33); // 30 FPS
Serial.println(pwmValue);
}
void rising() {
prevTime = micros();
enableInterrupt(CH_1_PIN, falling, FALLING);
}
void falling() {
pwmValue = micros()-prevTime;
disableInterrupt(CH_1_PIN);
enableInterrupt(CH_1_PIN, rising, RISING);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment