Pages: [1]   Go Down
Author Topic: Bicycle Lights sketch for HolidayV  (Read 295 times)
0 Members and 1 Guest are viewing this topic.
West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 41
Posts: 1229
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My completed sketch for this thread. I did not want to post it there as that would have spoiled HolidayV's fun, he was still working on it at the time.
Code:
//////Bicycle Light System v1.0////////////

/////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 = A0; //Set Pin 8 as Brake Switch
byte Right_Turn_Switch= A1; //Set Pin 4 as Switch
byte LED_RIGHT_REAR = 11;  //Right Rear Set Pin 11 as LED
byte LED_RIGHT_FRONT = 6;  //Right Front Set Pin 7 as LED
byte LEFT_TURN_SWITCH = A1 ; //Set Pin 5 as Switch
byte LED_LEFT_REAR = 10;  //Left Rear Set Pin 10 as LED
byte LED_LEFT_FRONT = 5;  //Left Front Set Pin 6 as LED
byte FADE_TOGGLE_SWITCH = A2; //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;
unsigned long lastTurnUpdate = 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;

int turnLevel = 0;

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);

}

// Smoothly changes the color values
void transition()
{
  if (millis() - lastPatternUpdate >= delayTime) {
    lastPatternUpdate = millis();
   
    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--;
  }
}

void turnTransition()
{
  if (millis() - lastTurnUpdate >= 100) {
    lastTurnUpdate = millis();
   
    if (turnLevel == 0)
      turnLevel = 255;
    else
      turnLevel = 0;
  }
}

// Sets the output voltage on the LED pins.
void setColor()
{
  analogWrite(LED_RIGHT_REAR, rightRearLED);
  analogWrite(LED_LEFT_REAR, leftRearLED);
}

void turnLeft()//turnLeft function
{
  analogWrite(LED_LEFT_REAR, turnLevel); //Set the Rear Right LED to maximum brightness
  analogWrite(LED_LEFT_FRONT, turnLevel); //Set the Front Right LED to maximum brightness
  turnTransition();
}

void turnRight()//turnRight function
{
  analogWrite(LED_RIGHT_REAR, turnLevel); //Set the Rear Right LED to maximum brightness
  analogWrite(LED_RIGHT_FRONT, turnLevel); //Set the Front Right LED to maximum brightness
  turnTransition();
}

void brakelights()
{
  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
}

void frontlights()
{
  analogWrite(LED_LEFT_FRONT, 255);
  analogWrite(LED_RIGHT_FRONT, 255);
}


void loop()
{
  buttonState_Brake_Switch = analogRead(BRAKE_SWITCH)<256 ? HIGH:LOW; //Continually look at the switch to see if its pressed
  buttonState_Left_Turn = analogRead(LEFT_TURN_SWITCH)<256 ? HIGH:LOW; //Continually look at the switch to see if its pressed
  buttonState_Right_Turn = analogRead(Right_Turn_Switch)>768 ? HIGH:LOW; //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
  {
    brakelights();
    turnLeft();
  }
  else if (buttonState_Brake_Switch == HIGH && buttonState_Right_Turn == HIGH) //If the switch goes HIGH, act on it
  {
    brakelights();
    turnRight();
  }
  else if (buttonState_Brake_Switch == HIGH) //If the switch goes HIGH, act on it
  {
    brakelights();
    frontlights();
  }
  else if (buttonState_Right_Turn == HIGH) //If the switch goes HIGH, act on it
  {
    transition();
    setColor();
    turnRight();
  }
  else if (buttonState_Left_Turn == HIGH) //If the switch goes HIGH, act on it
  {
    transition();
    setColor();
    turnLeft();
  }
  else //Normal mode, no buttons pressed
  {
    frontlights();
    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;

      }
    }
  }
}

Logged

Pages: [1]   Go Up
Jump to: