Skip to content

Instantly share code, notes, and snippets.

@barafael
Created September 11, 2017 20:37
Show Gist options
  • Save barafael/9cbcb0795e82a80106a9638de273565b to your computer and use it in GitHub Desktop.
Save barafael/9cbcb0795e82a80106a9638de273565b to your computer and use it in GitHub Desktop.
#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