Skip to content

Instantly share code, notes, and snippets.

@JvGinkel
Created June 27, 2020 15:55
Show Gist options
  • Save JvGinkel/11d5ce2d6dd82c470e0b2fa5fe1ec34d to your computer and use it in GitHub Desktop.
Save JvGinkel/11d5ce2d6dd82c470e0b2fa5fe1ec34d to your computer and use it in GitHub Desktop.
int channel; // channel value
const int ledPin = 0; // Output pin for rearlight LED
const int inputPin = 1; // Input signal from RC-receiver
int ledState = LOW; // Start with led off
unsigned long previousMillis = 0; // will store last time LED was updated
int interval = 0; // interval at which to blink default value
void setup() {
pinMode(inputPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read value from input pin
channel = pulseIn(inputPin, HIGH);
if (channel == 0) { // Turn sender on, pulse 2sec
interval = 2000;
updateLED();
}
else if (channel > 1 && channel < 1500 && ledState == LOW) { // Throttle, keep LED on (no blinking)
ledState = HIGH;
digitalWrite(ledPin, ledState);
}
else if ( channel > 1500 ) {
if (channel > 1600) { // Reverse, fast blinking
interval = 100;
updateLED();
} else { // neutral
interval = 400;
updateLED();
}
}
}
void updateLED() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment