Skip to content

Instantly share code, notes, and snippets.

@TheEmpty
Created January 7, 2015 22:52
Show Gist options
  • Save TheEmpty/92c02f93fda67509d826 to your computer and use it in GitHub Desktop.
Save TheEmpty/92c02f93fda67509d826 to your computer and use it in GitHub Desktop.
/*
Dimming Brake Lights, Idle Hazards, and External Control for LEDs via Arduino for R/C.
Copyright (c) 2015 Mohammad El-Abid
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// TODO: for overall project:
// Strobe effect? :D
// Perhaps blinking headlights at full throttle... perhaps; not like the car even goes full throttle @.@
// Perhaps automated day/night time via light sensor?
#include <Serial.h>
// EASY CONFIGURATION
const int brakingPoint = 1300; // PWM reading <= braking
const int movingPoint = 1500; // PWM reading >= acceleration
const int lazyHazardTime = 5000; // milliseconds idle before putting on hazard lights
const int SWITCH_DELAY = 500; // milliseconds between switch presses
const int NIGHT_BRAKES_IDLE = 50; // PWM for brake lights when idle and isNight
// END OF EASY CONFIGURATION
// ADVANCED CONFIGURATION
const int throttlePin = A0; // PWM wire for throttle
const int switchPin = A1; // PWM wire for switch
const int brakeLightPins[2] = {3, 5}; // Pins that the brake lights are connected to
const int hazardLightPins[1] = {13};
// END OF ADVANCED CONFIGURATION
// APPLICATION VARIABLES
// Array lengths
const int brakePinsLen = sizeof(brakeLightPins) / sizeof(int);
const int hazardPinsLen = sizeof(hazardLightPins) / sizeof(int);
// Throttle
int throttleValue;
unsigned long idleStart = 0; /* Time that idle state was first detected. */
boolean isIdle = false;
// Lighting setup
boolean isNight = true;
// Hazards
int hazardState = LOW;
boolean userHazard = false;
unsigned long hazardSwitch = 0; /* Timer for hazard lights */
// Switch
int prevSwitchVal = -1;
int switchPresses = 0;
unsigned long lastSwitch = 0;
// END OF APPLICATION VARIABLES
/****** HELPERS ******/
/* Takes an array of light pins and sets to a brightness. Useful if brakes are on more than one pin, etc. */
void dimLights(const int* lights, int len, int brightness) {
for(int i = 0; i < len; i++) {
analogWrite(lights[i], brightness);
}
}
/* Takes an array of light pins and sets them to HIGH or LOW) */
void setLights(const int* lights, int len, int state) {
for(int i = 0; i < len; i++) {
digitalWrite(lights[i], state);
}
}
/****** FUNCTIONALITY ******/
void brakeLights() {
if(throttleValue >= brakingPoint) {
dimLights(brakeLightPins, brakePinsLen, NIGHT_BRAKES_IDLE);
} else if(isNight) {
dimLights(brakeLightPins, brakePinsLen, 255);
} else {
dimLights(brakeLightPins, brakePinsLen, 0);
}
}
void hazardLights() {
boolean idleHazard = isIdle && (millis() - idleStart >= lazyHazardTime);
if(userHazard || idleHazard) {
unsigned long current = millis();
if(current - hazardSwitch >= 500) {
hazardState = !hazardState;
setLights(hazardLightPins, hazardPinsLen, hazardState);
hazardSwitch = current;
}
} else if(hazardState == HIGH) {
hazardState = LOW;
setLights(hazardLightPins, hazardPinsLen, LOW);
}
}
/* Monitor throttle state, idle, forward, backward, etc. */
void throttleMonitor() {
throttleValue = pulseIn(throttlePin, HIGH, 25000);
if(throttleValue < movingPoint && throttleValue > brakingPoint) {
if(isIdle == false) {
idleStart = millis();
isIdle = true;
}
} else {
isIdle = false;
}
}
int switchMonitor() {
int curVal = pulseIn(switchPin, HIGH, 25000);
if(abs(curVal - prevSwitchVal) >= 100) {
switchPresses++;
prevSwitchVal = curVal;
lastSwitch = millis();
}
if(lastSwitch != 0 && millis() - lastSwitch > SWITCH_DELAY) {
int copy = switchPresses;
switchPresses = 0;
lastSwitch = 0;
return copy;
}
return 0;
}
/****** MAIN ******/
/* Executes first */
void setup() {
pinMode(throttlePin, INPUT);
pinMode(switchPin, INPUT);
Serial.begin(9600);
idleStart = millis();
prevSwitchVal = pulseIn(switchPin, HIGH, 25000);
}
/* Main program loop, executes after setup() */
void loop() {
throttleMonitor();
brakeLights();
hazardLights();
int switches = switchMonitor();
if(switches == 1) {
userHazard = !userHazard;
} else if(switches == 2) {
isNight = !isNight;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment