I'm sure this has come up many times, but I probably wasn't using the right key words when searching
The code inserted below is what I am using to run the lights for my motorcycle. All the lights are functioning just fine by themselves, but I need to get multiple things to happen at once. Specifically, if my blinker is on my brake light cant come on and visa-versa. This makes sense now because of the "while" loops I use to trap the Arduino into constantly checking the status of the blinker/brake switch. How can I get my blinker to respond appropriately while the brake light is on, or any combination of that? Thanks.
//A sketch to run the LED lights on the cafe racer.
//Headlight - No code needed to run. Headlight switch will run this like normal,
//with the addition of a self contained blue LED for Hi-Beam indicator. Also use
//as a button to indicate taillight running light status.
//Taillight - Use PWM to run the brakelight at ~1/2 brightness when headlight is on.
//Brakelight on a MOSFET.
//Brake light - Use PWM to run brake light at full brightness when either brake is used.
//Brake light will flash on and off four times in rapid succession before holding at 100%
//Brake lights on a MOSFET.
//Blinkers - Rapidly flash proper blinker light five times before settling into normal blink rate.
//Sets separate buttons for left and right blinker.
const int LeftBlink = 11; //left blinker output
const int RightBlink = 10; //right blinker output
const int BrakeLight = 9; //taillight/brakelight output
const int LeftBlinkSwitch = 8; //right blinker switch input
const int RightBlinkSwitch = 7; //left blinker switch input
const int FrontBrake = 6; //front brake switch input
const int BackBrake = 5; //back brake switch input
const int Headlight = 4; //headlight on/off? input
int i = 0; //used to count the pulse
int LeftBlinkSwitchState = 0;
int RightBlinkSwitchState = 0;
int FrontBrakeState = 0;
int BackBrakeState = 0;
int HeadlightState = 0;
void setup()
{
pinMode(LeftBlink, OUTPUT); //defines LeftBlink as an output
pinMode(RightBlink, OUTPUT); //defines RightBlink as an output
pinMode(BrakeLight, OUTPUT); //defines Tail as an output
pinMode(LeftBlinkSwitch, INPUT); //defines LeftBlinkSwitch as an input
pinMode(RightBlinkSwitch, INPUT); //defines RightBlinkSwitch as an input
pinMode(FrontBrake, INPUT); //defines FontBrake as an input
pinMode(BackBrake, INPUT); //defines BackBrake as an input
pinMode(Headlight, INPUT); //defines Headlight as an input
}
void loop()
{
LeftBlinkSwitchState = digitalRead(LeftBlinkSwitch); //reads the switch value
RightBlinkSwitchState = digitalRead(RightBlinkSwitch);
FrontBrakeState = digitalRead(FrontBrake);
BackBrakeState = digitalRead(BackBrake);
HeadlightState = digitalRead(Headlight);
////////Start Left Blinker Code//////
if(LeftBlinkSwitchState == HIGH) //if the left blinker switch is on, then...
{
analogWrite(LeftBlink, 225); //starts initial rapid blink
delay(50);
analogWrite(LeftBlink, 0);
delay(50);
analogWrite(LeftBlink, 225);
delay(50);
analogWrite(LeftBlink, 0);
delay(50);
analogWrite(LeftBlink, 225);
delay(50);
analogWrite(LeftBlink, 0);
delay(50);
analogWrite(LeftBlink, 225);
delay(50);
analogWrite(LeftBlink, 0);
delay(50);
analogWrite(LeftBlink, 225);
delay(50);
analogWrite(LeftBlink, 0);
delay(50); //ends initial rapid blink
}
else
{
analogWrite(LeftBlink, 0);
}
while(LeftBlinkSwitchState == HIGH) //if the left blinker switch is on, then...
//start pulse
{
for (i = 0; i < 255;i++) //loop from 0 to 254(fade in)
{
analogWrite(LeftBlink, i); //set the brightness
delay(1.5); //Wait because analogWrite is instantaneous and we would not see a change
}
for (i = 255; i > 0; i--) //loop from 255 to 1 (fade out)
{
analogWrite(LeftBlink, i); //set the brightness
delay(1.5);
}
delay(1); //stay full off
//end pulse
LeftBlinkSwitchState= digitalRead(LeftBlinkSwitch); //checks to see if blinker switch is still on
}
////////End Left Blinker Code///////////////
////////Start Right Blinker Code//////
if(RightBlinkSwitchState == HIGH) //if the right blinker switch is on, then...
{
analogWrite(RightBlink, 225); //starts initial rapid blink
delay(50);
analogWrite(RightBlink, 0);
delay(50);
analogWrite(RightBlink, 225);
delay(50);
analogWrite(RightBlink, 0);
delay(50);
analogWrite(RightBlink, 225);
delay(50);
analogWrite(RightBlink, 0);
delay(50);
analogWrite(RightBlink, 225);
delay(50);
analogWrite(RightBlink, 0);
delay(50);
analogWrite(RightBlink, 225);
delay(50);
analogWrite(RightBlink, 0);
delay(50); //ends initial rapid blink
}
else
{
analogWrite(RightBlink, 0);
}
while(RightBlinkSwitchState == HIGH) //if the right blinker switch is on, then...
//start pulse
{
for (i = 0; i < 255;i++) //loop from 0 to 254(fade in)
{
analogWrite(RightBlink, i); //set the brightness
delay(1); //Wait because analogWrite is instantaneous and we would not see a change
}
for (i = 255; i > 0; i--) //loop from 255 to 1 (fade out)
{
analogWrite(RightBlink, i); //set the brightness
delay(1);
}
delay(1); //stay full off
//end pulse
RightBlinkSwitchState= digitalRead(RightBlinkSwitch); //checks to see if blinker switch is still on
}
////////End Right Blinker Code///////////////
////////Start Brake Light Code///////////////
if (FrontBrakeState == HIGH || BackBrakeState == HIGH) //if either brake is on, then...
{
analogWrite(BrakeLight, 225); //starts initial rapid blink
delay(45);
analogWrite(BrakeLight, 0);
delay(45);
analogWrite(BrakeLight, 225);
delay(45);
analogWrite(BrakeLight, 0);
delay(45);
analogWrite(BrakeLight, 225);
delay(45);
analogWrite(BrakeLight, 0);
delay(45);
analogWrite(BrakeLight, 225);
delay(45);
analogWrite(BrakeLight, 0);
delay(45);
analogWrite(BrakeLight, 225);
delay(45);
analogWrite(BrakeLight, 0);
delay(45); //ends initial rapid blink
}
else if(HeadlightState == HIGH)
{
analogWrite(BrakeLight, 112); //if the headlight is on, run the brakelight at 1/2 bright
}
else
{
analogWrite(BrakeLight, 0); //if the headlight isnt on and no brake is on, turn the braikelight off
}
while((FrontBrakeState == HIGH || BackBrakeState == HIGH))
{
digitalWrite(BrakeLight, HIGH);
FrontBrakeState= digitalRead(FrontBrake);
BackBrakeState= digitalRead(BackBrake);
}
}