Last active
January 24, 2019 16:57
-
-
Save droter/50cd4e0affc4a6071945a8c68b975629 to your computer and use it in GitHub Desktop.
Arduino Mini Pro Servo Fail-Safe
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> | |
// Base Controller Input | |
#define cmdPin 2 | |
// Servo Output | |
#define servoPin 5 | |
// Transmission Neutral Safe Position | |
#define transCenter 1550 | |
Servo myservo; | |
volatile unsigned long timer_start; | |
volatile int last_interrupt_time; | |
volatile unsigned long last_signal; | |
volatile int pulse_time = 0; | |
void calcSignal() | |
{ | |
//record the interrupt time so that we can tell if the receiver has a signal from the base controller | |
last_interrupt_time = millis(); | |
//if the pin has gone HIGH, record the microseconds since the Arduino started up | |
if(digitalRead(cmdPin) == HIGH) | |
{ | |
timer_start = micros(); | |
} | |
//otherwise, the pin has gone LOW | |
else | |
{ | |
//only worry about this if the timer has actually started | |
if(timer_start != 0) | |
{ | |
//record the pulse time | |
pulse_time = ((volatile int)micros() - timer_start); | |
//restart the timer | |
timer_start = 0; | |
} | |
} | |
} | |
void setup() | |
{ | |
timer_start = 0; | |
attachInterrupt(digitalPinToInterrupt(cmdPin), calcSignal, CHANGE); | |
myservo.attach(servoPin); | |
myservo.writeMicroseconds(transCenter); | |
} | |
void loop() | |
{ | |
// Record when we received the last signal | |
last_signal = ((volatile int)millis() - last_interrupt_time); | |
// Check to see if there is an input signal and if we have a new one | |
if (pulse_time < 500 || pulse_time > 2500 || last_signal > 2000) { | |
// Default servo to transmission center | |
myservo.writeMicroseconds(transCenter); | |
} else { | |
// Pass signal from Base Controller to Servo | |
myservo.writeMicroseconds(pulse_time); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment