This is cool. Here's something I have been working on for a roadwork vehicle that makes frequent stops on a highway median.
I already have the amber turn signals and white reverse LED's strobing in a specific pattern that are being handled by a separate sketch in a separate ATtiny85 circuit with the turn signal switch, and hazard button.
The brake lights are still Off, Dim, or Bright and controlled by the attached sketch. I decided to use a Arduino for the brake lights for a couple reasons.
- The LED's I am using aren't designed to dim, but will with the Arduino.
- I wanted to use Arduino to expand the functionality of the brake lights in the future.
I have read and attempted to utilize your final code within the attached code, but failed. Either it does nothing, throws errors, or strobes continuously.
Keep in mind, what I am asking for help with does not involve the turn signals or reverse lights. Only 1 analog output for the brake LED allthough you may notice I am using 2 outputs. 1 for each side of the truck. This is also for future enhancements, but for now, I want the both to mirror each other.
// Brake Light control with driving light dimming and debouncing
// constants won't change. They're used here to
// set pin numbers:
const int BUTTONpin = 12; // the number of the pushbutton pin
const int SWITCHpin = 7; // the number of the pushSWITCH pin
// Variables will change:
int pwr = 0;
int BUTTONstate; // the current BUTTONreading from the input pin
int lastBUTTONstate = LOW; // the previous BUTTONreading from the input pin
long lastBUTTONDebounceTime = 0; // the last time the output pin was toggled
int SWITCHstate; // the current SWITCHreading from the input pin
int lastSWITCHstate = LOW; // the previous SWITCHreading from the input pin
long lastSWITCHDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(BUTTONpin, INPUT);
pinMode(SWITCHpin, INPUT);
}
void loop()
{
getSWITCHreading();
getBUTTONreading();
//SWITCHstate is the current state of the SWITCH
//BUTTONstate is the current state of the BUTTON
if(SWITCHstate==1) //if SWITCH is on
{
if(BUTTONstate == 1) //if BUTTON is pressed
{
analogWrite(10, 255);
analogWrite(11, 255);
}
else //if BUTTON not pressed
{
analogWrite(10, 60);
analogWrite(11, 60);
}
}
else //SWITCHstate=LOW //if SWITCH is off
{
if(BUTTONstate == 1) //if BUTTON is pressed
{
analogWrite(10, 255);
analogWrite(11, 255);
}
else //if BUTTON not pressed
{
analogWrite(10, 0);
analogWrite(11, 0);
}
}
}
//Debouncing stuff...
void getSWITCHreading()
{
// read the state of the switch into a local variable:
int SWITCHreading = digitalRead(SWITCHpin);
// check to see if you just pressed the SWITCH
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (SWITCHreading != lastSWITCHstate) {
// reset the debouncing timer
lastSWITCHDebounceTime = millis();
}
if ((millis() - lastSWITCHDebounceTime) > debounceDelay) {
// whatever the SWITCHreading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
SWITCHstate = SWITCHreading;
}
// save the SWITCHreading. Next time through the loop,
// it'll be the lastSWITCHstate:
lastSWITCHstate = SWITCHreading;
}
void getBUTTONreading()
{
// read the state of the switch into a local variable:
int BUTTONreading = digitalRead(BUTTONpin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (BUTTONreading != lastBUTTONstate) {
// reset the debouncing timer
lastBUTTONDebounceTime = millis();
}
if ((millis() - lastBUTTONDebounceTime) > debounceDelay) {
// whatever the BUTTONreading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
BUTTONstate = BUTTONreading;
}
// save the BUTTONreading. Next time through the loop,
// it'll be the lastBUTTONstate:
lastBUTTONstate = BUTTONreading;
}
How can I make the brake light flash 3 times in rapid succession (120MS total / 20MS per on/off) before remaining solid until the brake (button) is released regardless if the running lights (Switch) is on or off?