//THIRD BRAKE LIGHT
// LEFT4 LEFT3 LEFT2 LEFT1 (REVERSE)(REVERSE) RIGHT1 RIGHT2 RIGHT3 RIGHT4
//Global Variables
const byte BrakeSwitch = 10; //BRAKE trigger
const byte RightSwitch = 11; //RIGHT trigger
const byte RIGHT1 =6;
const byte RIGHT2 = 7;
const byte RIGHT3 = 8;
const byte RIGHT4 = 9;
const byte LeftSwitch = 12; //LEFT trigger
const byte LEFT1 = 5;
const byte LEFT2 = 4;
const byte LEFT3 = 3;
const byte LEFT4 = 2;
unsigned long buttonPushedMillis; // when button was released
unsigned long ledTurnedOnAt; // when led was turned on
unsigned long LEFTbuttonPushedMillis; // when button was released
unsigned long LEFTledTurnedOnAt; // when led was turned on
unsigned long turnOnDelay = 1; // wait to turn on LED
unsigned long turnOffDelay = 700; // turn off LED after this time
bool ledReady = false; // flag for when button is let go
bool ledState = false; // for LED is on or not.
bool LEFTledReady = false; // flag for when button is let go
bool LEFTledState = false;
void setup() {
pinMode(RIGHT1, OUTPUT);
pinMode(RIGHT2, OUTPUT);
pinMode(RIGHT3, OUTPUT);
pinMode(RIGHT4, OUTPUT);
pinMode(LEFT1, OUTPUT);
pinMode(LEFT2, OUTPUT);
pinMode(LEFT3, OUTPUT);
pinMode(LEFT4, OUTPUT);
pinMode(BrakeSwitch, INPUT_PULLUP);
pinMode(RightSwitch, INPUT_PULLUP);
pinMode(LeftSwitch, INPUT_PULLUP);
digitalWrite(RIGHT1, LOW);
digitalWrite(RIGHT2, LOW);
digitalWrite(RIGHT3, LOW);
digitalWrite(RIGHT4, LOW);
digitalWrite(LEFT1, LOW);
digitalWrite(LEFT2, LOW);
digitalWrite(LEFT3, LOW);
digitalWrite(LEFT4, LOW);
}
void loop() {
// get the time at the start of this loop()
unsigned long currentMillis = millis();
unsigned long LEFTcurrentMillis = millis();
////// RIGHT TURN SIGNAL
if (digitalRead(RightSwitch) == LOW) {
// update the time when button was pushed
buttonPushedMillis = currentMillis;
ledReady = true;
}
// make sure this code isn't checked until after button has been let go
if (ledReady) {
//this is typical millis code here:
if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
// okay, enough time has passed since the button was let go.
digitalWrite(RIGHT1, HIGH);
// setup our next "state"
ledState = true;
// save when the LED turned on
ledTurnedOnAt = currentMillis;
// wait for next button press
ledReady = false;
}
}
// see if we are watching for the time to turn off LED
if (ledState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay/4) {
ledState = true;
digitalWrite(RIGHT2, HIGH);
}
if (ledState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay/2) {
ledState = true;
digitalWrite(RIGHT3, HIGH);
}
}
if (ledState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= 3*turnOffDelay/4) {
ledState = true;
digitalWrite(RIGHT4, HIGH);
}
}
if (ledState) {
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
ledState = false;
digitalWrite(RIGHT1, LOW);
digitalWrite(RIGHT2, LOW);
digitalWrite(RIGHT3, LOW);
digitalWrite(RIGHT4, LOW);
}
}
}
/*
if (digitalRead(RightSwitch) == LOW) {
// update the time when button was pushed
buttonPushedMillis = currentMillis;
ledReady = true;
}
// make sure this code isn't checked until after button has been let go
if (ledReady)
{
//this is typical millis code here:
if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
// okay, enough time has passed since the button was let go.
digitalWrite(RIGHT1, HIGH);
// setup our next "state"
ledState = true;
// save when the LED turned on
ledTurnedOnAt = currentMillis;
// wait for next button press
ledReady = false;
}
}
// see if we are watching for the time to turn off LED
if (ledState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay/4) {
ledState = true;
digitalWrite(RIGHT2, HIGH);
}
if (ledState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay/2) {
ledState = true;
digitalWrite(RIGHT3, HIGH);
}
}
if (ledState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= 3*turnOffDelay/4) {
ledState = true;
digitalWrite(RIGHT4, HIGH);
}}
if (ledState) {
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
digitalWrite(RIGHT1, LOW);
digitalWrite(RIGHT2, LOW);
digitalWrite(RIGHT3, LOW);
digitalWrite(RIGHT4, LOW);
ledState = false;
}}
}
*/
///// LEFT TURN SIGNAL
if (digitalRead(LeftSwitch) == LOW) {
// update the time when button was pushed
LEFTbuttonPushedMillis = LEFTcurrentMillis;
LEFTledReady = true;
}
// make sure this code isn't checked until after button has been let go
if (LEFTledReady) {
//this is typical millis code here:
if ((unsigned long)(LEFTcurrentMillis - LEFTbuttonPushedMillis) >= turnOnDelay) {
// okay, enough time has passed since the button was let go.
digitalWrite(LEFT1, HIGH);
// setup our next "state"
LEFTledState = true;
// save when the LED turned on
LEFTledTurnedOnAt = LEFTcurrentMillis;
// wait for next button press
LEFTledReady = false;
}
}
// see if we are watching for the time to turn off LED
if (LEFTledState) {
// okay, led on, check for now long
if ((unsigned long)(LEFTcurrentMillis - LEFTledTurnedOnAt) >= turnOffDelay/4) {
LEFTledState = true;
digitalWrite(LEFT2, HIGH);
}
if (LEFTledState) {
// okay, led on, check for now long
if ((unsigned long)(LEFTcurrentMillis - LEFTledTurnedOnAt) >= turnOffDelay/2) {
LEFTledState = true;
digitalWrite(LEFT3, HIGH);
}
}
if (LEFTledState) {
// okay, led on, check for now long
if ((unsigned long)(LEFTcurrentMillis - LEFTledTurnedOnAt) >= turnOffDelay/1.333) {
LEFTledState = true;
digitalWrite(LEFT4, HIGH);
}
}
if (LEFTledState) {
if ((unsigned long)(LEFTcurrentMillis - LEFTledTurnedOnAt) >= turnOffDelay) {
LEFTledState = false;
digitalWrite(LEFT1, LOW);
digitalWrite(LEFT2, LOW);
digitalWrite(LEFT3, LOW);
digitalWrite(LEFT4, LOW);
}
}
}
///////BRAKE LIGHTS
/*
if (digitalRead(BrakeSwitch) == LOW) {
// update the time when button was pushed
buttonPushedMillis = currentMillis;
ledReady = true;
}
// make sure this code isn't checked until after button has been let go
if (ledReady) {
//this is typical millis code here:
if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
// okay, enough time has passed since the button was let go.
digitalWrite(LEFT1, HIGH);
digitalWrite(RIGHT1, HIGH);
// setup our next "state"
ledState = true;
// save when the LED turned on
ledTurnedOnAt = currentMillis;
// wait for next button press
ledReady = false;
}
}
// see if we are watching for the time to turn off LED
if (ledState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay/4) {
ledState = true;
digitalWrite(LEFT2, HIGH);
digitalWrite(RIGHT2, HIGH);
}
if (ledState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay/2) {
ledState = true;
digitalWrite(LEFT3, HIGH);
digitalWrite(RIGHT3, HIGH);
}
}
if (ledState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay/1.333) {
ledState = true;
digitalWrite(LEFT4, HIGH);
digitalWrite(RIGHT4, HIGH);
}}
if (ledState) {
if (digitalRead(BrakeSwitch) == HIGH) {
ledState = false;
digitalWrite(LEFT1, LOW);
digitalWrite(LEFT2, LOW);
digitalWrite(LEFT3, LOW);
digitalWrite(LEFT4, LOW);
digitalWrite(RIGHT1, LOW);
digitalWrite(RIGHT2, LOW);
digitalWrite(RIGHT3, LOW);
digitalWrite(RIGHT4, LOW);
}}
}
*/
}