Skip to content

Instantly share code, notes, and snippets.

@codebudo
Created February 6, 2019 16:46
Show Gist options
  • Save codebudo/c4d745a5f88aa14d3e630e1d4f7aafd8 to your computer and use it in GitHub Desktop.
Save codebudo/c4d745a5f88aa14d3e630e1d4f7aafd8 to your computer and use it in GitHub Desktop.
Receive signal from RC receiver and proxy to servo.
#include <EnableInterrupt.h>
#include "./ServoTimer2.h"
const int CH_1_PIN = 9;
const int CH_1_PIN_OUT = 3;
const int DEADZONE = 20;
volatile int pwmValue;
volatile unsigned long prevTime = 0;
ServoTimer2 ch_1_out;
void setup() {
Serial.begin(9600);
enableInterrupt(CH_1_PIN, rising, RISING);
ch_1_out.attach(CH_1_PIN_OUT);
}
void loop() {
delay(33); // 30 FPS
if( abs(pwmValue - 1500) < DEADZONE ){
pwmValue = 1500;
}
// debug
Serial.print((pwmValue - 1000) / 1000.0 * 255);
Serial.print(" ");
Serial.print(pwmValue);
Serial.println();
ch_1_out.write(pwmValue);
pwmValue = 0;
}
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