I think I'm starting to get the hang of this just a little. I now have the LEFT TURN, RIGHT TURN, and BRAKE LIGHTS merged along with keeping the headlights lit during the whole thing. While turning right, the left front stays lit while the right front and back flash, and vise versa. I still have the problem with the right button flashing the left lights before switching to the right lights but it's getting alot closer because I'm now down from 7 separate sketches to 5. Still have no idea how to merge the FADE sketches and toggle them. I think I could merge one of them into the existing sketch that I have now, but that's about as far as I'll be able to get.
Here's what I've got so far:
byte BRAKE_SWITCH = 8; //Set Pin 8 as Brake Switch
byte Right_Turn= 4; //Set Pin 4 as Switch
byte PIN_RED = 9; //Right Rear Set Pin 9 as LED
byte PIN_FR = 7; //Right Front Set Pin 7 as LED
byte Left_Turn = 5 ; //Set Pin 5 as Switch
byte PIN_RED2 = 10; //Left Rear Set Pin 10 as LED
byte PIN_FL = 6; //Left Front Set Pin 6 as LED
boolean buttonstate; //Integer variable named buttonstate Left Turn Switch
boolean button_state; //Integer variable named button_state Right Turn Switch
boolean button__state; //Integer variable named button__state Brake Switch
void setup()
{
pinMode(Left_Turn, INPUT);
pinMode(PIN_RED2, OUTPUT);
pinMode(PIN_FL, OUTPUT);
pinMode(Right_Turn, INPUT);
pinMode(PIN_RED, OUTPUT);
pinMode(PIN_FR, OUTPUT);
pinMode(BRAKE_SWITCH, INPUT);
}
void loop()
{
analogWrite(PIN_FR, 255);
analogWrite(PIN_FL, 255);
button__state = digitalRead(BRAKE_SWITCH); //Continually look at the switch to see if its pressed
if (button__state == HIGH) //If the switch goes HIGH, act on it
{
brakelights(); //new function called brakelights
}
buttonstate = digitalRead(Left_Turn); //Continually look at the switch to see if its pressed
if (buttonstate == HIGH) //If the switch goes HIGH, act on it
{
turnLeft(); //new function called turnLeft
}
button_state = digitalRead(Right_Turn); //Continually look at the switch to see if its pressed
if (button_state == HIGH) //If the switch goes HIGH, act on it
{
turnLeft(); //new function called turnLeft
turnRight(); //new function called turnRight
brakelights(); //new function called brakelights
}
}
void turnLeft()//turnLeft function
{
analogWrite(PIN_FR, 255);
buttonstate = HIGH; //the micro the switch is now HIGH
while (buttonstate == HIGH) //While the switch is NOT pressed do the following
{
buttonstate = digitalRead(Left_Turn); //Continually look at the switch to see if its pressed
analogWrite(PIN_RED2, 255); //Set the Rear Left LED to maximum brightness
analogWrite(PIN_FL, 255); //Set the Front Left LED to maximum brightness
delay(100);
analogWrite(PIN_RED2, 0); //turn the LED off for a blinking effect
analogWrite(PIN_FL, 0); //turn the LED off for a blinking effect
delay(100);
}
//Once the switch is pressed again, break out of the loop above and then break out of the function completely and go back to our main loop
buttonstate = LOW; //First we tell the micro the switch is now LOW
analogWrite(PIN_RED2, 0); //We turn the LED off before leaving our custom function
analogWrite(PIN_FL, 0); //We turn the LED off before leaving our custom function
}
void turnRight()//turnRight function
{
analogWrite(PIN_FL, 255);
button_state = HIGH; //the micro the switch is now HIGH
while (button_state == HIGH) //While the switch is NOT pressed do the following
{
button_state = digitalRead(Right_Turn); //Continually look at the switch to see if its pressed
analogWrite(PIN_RED, 255); //Set the Rear Right LED to maximum brightness
analogWrite(PIN_FR, 255); //Set the Front Right LED to maximum brightness
delay(100);
analogWrite(PIN_RED, 0); //turn the LED off for a blinking effect
analogWrite(PIN_FR, 0); //turn the LED off for a blinking effect
delay(100);
}
//Once the switch is pressed again, break out of the loop above and then break out of the function completely and go back to our main loop
button_state = LOW; //First we tell the micro the switch is now LOW
analogWrite(PIN_RED, 0); //We turn the LED off before leaving our custom function
analogWrite(PIN_FR, 0); //We turn the LED off before leaving our custom function
}
void brakelights()
{
analogWrite(PIN_FL, 255);
analogWrite(PIN_FR, 255);
button__state = HIGH; //the micro switch is now HIGH
while (button__state == HIGH) //While the switch is NOT pressed do the following
{
button__state = digitalRead(BRAKE_SWITCH); //Continually look at the switch to see if its pressed
analogWrite(PIN_RED, 255); //Set the Rear Right LED to maximum brightness
analogWrite(PIN_RED2, 255); //Set the Rear Left LED to maximum brightness
}
//Once the switch is pressed again, break out of the loop above and then break out of the function completely and go back to our main loop
button_state = LOW; //First we tell the micro the switch is now LOW
analogWrite(PIN_RED, 0); //We turn the LED off before leaving our custom function
analogWrite(PIN_RED2, 0); //We turn the LED off before leaving our custom function
}