Created
September 11, 2017 20:37
-
-
Save barafael/9cbcb0795e82a80106a9638de273565b to your computer and use it in GitHub Desktop.
This file contains 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 <Servo.h> | |
// Assign your channel in pins | |
#define THROTTLE_IN_PIN 8 | |
volatile uint16_t unThrottleInShared; | |
uint32_t ulThrottleStart; | |
void setup() | |
{ | |
Serial.begin(112500); | |
pinMode(THROTTLE_IN_PIN, INPUT); | |
attachInterrupt(THROTTLE_IN_PIN, calcThrottle,CHANGE); | |
} | |
void loop() | |
{ | |
static uint16_t unThrottleIn = 0; | |
static uint16_t oldThrottle = 1; | |
noInterrupts(); | |
unThrottleIn = unThrottleInShared; | |
interrupts(); | |
if (oldThrottle != unThrottleIn) { | |
Serial.println(unThrottleIn); | |
} | |
oldThrottle = unThrottleIn; | |
} | |
void calcThrottle() | |
{ | |
if(digitalRead(THROTTLE_IN_PIN) == HIGH) | |
{ | |
ulThrottleStart = micros(); | |
} | |
else | |
{ | |
unThrottleInShared = (uint16_t)(micros() - ulThrottleStart); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment