Well, it seems to be working. May not be the correct way to do it, but it's working ![]()
//////Bicycle Light System v1.05////////////
/////Author: Christopher Valentine 2014
/////Special Thanks to: PaulRB of the Arduino support forum!!!
////This is a full blown bicyle light system with
////headlights, turn signals (front and back),
////brakelights, and multi-function flashing tail lights
////Although this set up is designed for a bike,
////with the use of a wireless remote control it could
////Also be used on a remote control car or other things
byte BRAKE_SWITCH = 8; //Set Pin 8 as Brake Switch
byte Right_Turn_Switch= 4; //Set Pin 4 as Switch
byte LED_RIGHT_REAR = 11; //Right Rear Set Pin 11 as LED
byte LED_RIGHT_FRONT = 7; //Right Front Set Pin 7 as LED
byte LEFT_TURN_SWITCH = 5 ; //Set Pin 5 as Switch
byte LED_LEFT_REAR = 10; //Left Rear Set Pin 10 as LED
byte LED_LEFT_FRONT = 6; //Left Front Set Pin 6 as LED
byte FADE_TOGGLE_SWITCH = 3; //Toggle Fades set Pin 3
boolean buttonState_Left_Turn; //Integer variable named buttonState_Left_Turn Left Turn Switch
boolean buttonState_Right_Turn; //Integer variable named buttonState_Right_Turn Right Turn Switch
boolean buttonState_Brake_Switch; //Integer variable named buttonState_Brake_Switch Brake Switch
// Delay time: sets the time in milliseconds between loop iterations.
// Make this value large for slower transitions.
unsigned long delayTime = 10;
unsigned long lastPatternUpdate = 0;
int currentPattern = 7;
int patternbuttonState = HIGH;
// The initial values of each color.
int rightRearLED = 175;
int leftRearLED = 175;
// Indicates whether a color is incRighttRearementing (1) or decrementing (0).
int incRighttRear = 1;
int incLefttRear = 1;
void setup()
{
pinMode(FADE_TOGGLE_SWITCH, INPUT_PULLUP);
pinMode(LEFT_TURN_SWITCH, INPUT);
pinMode(LED_LEFT_REAR, OUTPUT);
pinMode(LED_LEFT_FRONT, OUTPUT);
pinMode(Right_Turn_Switch, INPUT);
pinMode(LED_RIGHT_REAR, OUTPUT);
pinMode(LED_RIGHT_FRONT, OUTPUT);
pinMode(BRAKE_SWITCH, INPUT);
}
void turnLeft()//turnLeft function
{
analogWrite(LED_RIGHT_FRONT, 255);
analogWrite(LED_RIGHT_REAR, 255);
analogWrite(LED_LEFT_REAR, 125);
buttonState_Left_Turn = HIGH; //the micro the switch is now HIGH
while (buttonState_Left_Turn == HIGH) //While the switch is NOT pressed do the following
{
buttonState_Left_Turn = digitalRead(LEFT_TURN_SWITCH); //Continually look at the switch to see if its pressed
analogWrite(LED_LEFT_REAR, 255); //Set the Rear Left LED to maximum brightness
analogWrite(LED_LEFT_FRONT, 255); //Set the Front Left LED to maximum brightness
delay(100);
analogWrite(LED_LEFT_REAR, 0); //turn the LED off for a blinking effect
analogWrite(LED_LEFT_FRONT, 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_Left_Turn = LOW; //First we tell the micro the switch is now LOW
analogWrite(LED_LEFT_REAR, 0); //We turn the LED off before leaving our custom function
analogWrite(LED_LEFT_FRONT, 0); //We turn the LED off before leaving our custom function
}
void turnRight()//turnRight function
{
analogWrite(LED_LEFT_FRONT, 255);
analogWrite(LED_RIGHT_REAR, 125);
analogWrite(LED_LEFT_REAR, 255);
buttonState_Right_Turn = HIGH; //the micro the switch is now HIGH
while (buttonState_Right_Turn == HIGH) //While the switch is NOT pressed do the following
{
buttonState_Right_Turn = digitalRead(Right_Turn_Switch); //Continually look at the switch to see if its pressed
analogWrite(LED_RIGHT_REAR, 255); //Set the Rear Right LED to maximum brightness
analogWrite(LED_RIGHT_FRONT, 255); //Set the Front Right LED to maximum brightness
delay(100);
analogWrite(LED_RIGHT_REAR, 0); //turn the LED off for a blinking effect
analogWrite(LED_RIGHT_FRONT, 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_Right_Turn = LOW; //First we tell the micro the switch is now LOW
analogWrite(LED_RIGHT_REAR, 0); //We turn the LED off before leaving our custom function
analogWrite(LED_RIGHT_FRONT, 0); //We turn the LED off before leaving our custom function
}
// Smoothly changes the color values
void transition()
{
if (rightRearLED >= 175)
incRighttRear = 0;
else if (rightRearLED <= 20)
incRighttRear = 1;
if (leftRearLED >= 175)
incLefttRear = 0;
else if (leftRearLED <= 20)
incLefttRear = 1;
if (incRighttRear)
rightRearLED++;
else
rightRearLED--;
if(incLefttRear)
leftRearLED++;
else
leftRearLED--;
}
// Sets the output voltage on the LED pins.
void setColor()
{
analogWrite(LED_RIGHT_REAR, rightRearLED);
analogWrite(LED_LEFT_REAR, leftRearLED);
}
void loop()
{
buttonState_Brake_Switch = digitalRead(BRAKE_SWITCH); //Continually look at the switch to see if its pressed
buttonState_Left_Turn = digitalRead(LEFT_TURN_SWITCH); //Continually look at the switch to see if its pressed
buttonState_Right_Turn = digitalRead(Right_Turn_Switch); //Continually look at the switch to see if its pressed
if (buttonState_Brake_Switch == HIGH && buttonState_Left_Turn == HIGH) //If the switch goes HIGH, act on it
{
turnLeft();
}
else if (buttonState_Brake_Switch == HIGH && buttonState_Right_Turn == HIGH) //If the switch goes HIGH, act on it
{
turnRight();
}
else if (buttonState_Brake_Switch == HIGH) //If the switch goes HIGH, act on it
{
analogWrite(LED_RIGHT_REAR, 255); //Set the Rear Right LED to maximum brightness
analogWrite(LED_LEFT_REAR, 255); //Set the Rear Left LED to maximum brightness
analogWrite(LED_LEFT_FRONT, 255);
analogWrite(LED_RIGHT_FRONT, 255);
}
else if (buttonState_Right_Turn == HIGH) //If the switch goes HIGH, act on it
{
turnRight();
}
else if (buttonState_Left_Turn == HIGH) //If the switch goes HIGH, act on it
{
turnLeft();
}
else { //Normal mode, no buttons pressed
analogWrite(LED_LEFT_FRONT, 255);
analogWrite(LED_RIGHT_FRONT, 255);
if (millis() - lastPatternUpdate >= delayTime) {
lastPatternUpdate = millis();
transition();
setColor();
}
if (digitalRead(FADE_TOGGLE_SWITCH) != patternbuttonState) { //the button state has changed
if (patternbuttonState == LOW) { //the button state must have just gone high
patternbuttonState = HIGH; //record that button has just been pressed but do nothing for now, wait for release
}
else { //it was high, so the button must have just been released
//record the fact that be button has now gone low ie. not pressed
patternbuttonState = LOW;
switch (currentPattern) {
case 1: //Pattern 1 has been running
//set up pattern 2
currentPattern = 2;
rightRearLED = 175;
leftRearLED = 175;
incRighttRear = 1;
incLefttRear = 1;
delayTime = 5;
break;
case 2://Pattern 2 has been running
//set up pattern 3
currentPattern = 3;
rightRearLED = 175;
leftRearLED = 175;
incRighttRear = 1;
incLefttRear = 1;
delayTime = 1;
break;
case 3://Pattern 3 has been running
//set up pattern 4
currentPattern = 4;
rightRearLED = 175;
leftRearLED = 20;
incRighttRear = 1;
incLefttRear = 1;
delayTime = 10;
break;
case 4://Pattern 4 has been running
//set up pattern 5
currentPattern = 5;
rightRearLED = 175;
leftRearLED = 20;
incRighttRear = 1;
incLefttRear = 1;
delayTime = 5;
break;
case 5://Pattern 5 has been running
//set up pattern 6
currentPattern = 6;
rightRearLED = 175;
leftRearLED = 20;
incRighttRear = 1;
incLefttRear = 1;
delayTime = 1;
break;
case 6://Pattern 6 has been running
//set up pattern 7
currentPattern = 7;
rightRearLED = 175;
leftRearLED = 175;
incRighttRear = 0;
incLefttRear = 0;
delayTime = 0;
break;
case 7:
//set up pattern 1
currentPattern = 1;
rightRearLED = 175;
leftRearLED = 175;
incRighttRear = 1;
incLefttRear = 1;
delayTime = 10;
break;
}
}
}
}
}