Created
February 6, 2019 00:34
-
-
Save codebudo/4273c5b57352914643f9320427b22151 to your computer and use it in GitHub Desktop.
Calculating PWM timing using interrupts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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