Pages: 1 ... 3 4 [5] 6 7   Go Down
Author Topic: Multiple LED's on separate pins, switches and functions - need guidance  (Read 7160 times)
0 Members and 1 Guest are viewing this topic.
West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 1478
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I searched the whole code and the only instances of "loop" are in descriptions following "//" or "voidloop".

I think it was something that happened when I cut & pasted your code into the Arduino IDE. Those comment lines with the word "loop" at the end are the longest lines in the whole sketch. I suspect the cut & paste "chopped" those lines to 132 characters, resulting in new lines containing just the word "loop", and not as part of a comment. In the old days, "green-screen" computer terminals could be set to 80 or 132 characters wide, so I wonder if a little "feature" is lurking somewhere (I use Firefox browser and IDE 1.0.1 I think, runnning on Ubuntu 12.04).
Logged

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 1478
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Chris, another helpful step in the right direction at this stage would be to rename some of your variables to make their meaning more obvious.

Code:
byte BRAKE_SWITCH = 8; //Set Pin 8 as Brake Switch
byte Right_Turn= 4; //Set Pin 4 as Switch
byte PIN_RED = 11;  //Right Rear Set Pin 11 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
byte PIN_TOGG = 3; //Toggle Fades set Pin 3
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

// The initial values of each color.
int red = 175;
int red2 = 175;

// Indicates whether a color is incrementing (1) or decrementing (0).
int incR = 1;
int incR2 = 1;

For example "BRAKE_SWITCH" is pretty good I think, but "Right_Turn" would be better "RIGHT_TURN_SWITCH". Also "PIN_RED" and "PIN_RED2" could be "LED_RED_LEFT" and "LED_RED_RIGHT" and so on. In particular "buttonstate", "button_state" and "button__state" need sorting! There is a search & replace function in the IDE.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Fixed:
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 = 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 PIN_FL = 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;

int currentPattern = 7;
int patternbuttonState_Left_Turn = 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);
  pinMode(LEFT_TURN_SWITCH, INPUT);
  pinMode(LED_LEFT_REAR, OUTPUT);
  pinMode(PIN_FL, 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, 125);
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(PIN_FL, 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(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_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(PIN_FL, 0); //We turn the LED off before leaving our custom function
}

void turnRight()//turnRight function
{
analogWrite(PIN_FL, 255);
analogWrite(LED_RIGHT_REAR, 125);
analogWrite(LED_LEFT_REAR, 125);
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
}

void brakelights()
{

analogWrite(PIN_FL, 255);
analogWrite(LED_RIGHT_FRONT, 255);
buttonState_Brake_Switch = HIGH; //the micro switch is now HIGH

while (buttonState_Brake_Switch == HIGH) //While the switch is NOT pressed do the following
{
  buttonState_Brake_Switch = digitalRead(BRAKE_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_LEFT_REAR, 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
buttonState_Right_Turn = LOW; //First we tell the micro the switch is now LOW
analogWrite(LED_RIGHT_REAR, 125); //We turn the LED off before leaving our custom function
analogWrite(LED_LEFT_REAR, 125); //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()
{
 analogWrite(LED_RIGHT_FRONT, 255);
 analogWrite(PIN_FL, 255);
 transition();
 setColor();
 delay(delayTime);
  
 buttonState_Brake_Switch = digitalRead(BRAKE_SWITCH); //Continually look at the switch to see if its pressed
if (buttonState_Brake_Switch == HIGH) //If the switch goes HIGH, act on it
{
  brakelights(); //new function called brakelights
}
  
buttonState_Left_Turn = digitalRead(LEFT_TURN_SWITCH); //Continually look at the switch to see if its pressed
if (buttonState_Left_Turn == HIGH) //If the switch goes HIGH, act on it
{
turnLeft(); //new function called turnLeft
}
buttonState_Right_Turn = digitalRead(Right_Turn_Switch); //Continually look at the switch to see if its pressed
if (buttonState_Right_Turn == HIGH) //If the switch goes HIGH, act on it
{
turnRight(); //new function called turnRight
}

digitalRead(FADE_TOGGLE_SWITCH);
  if (digitalRead(FADE_TOGGLE_SWITCH) != patternbuttonState_Left_Turn)  { //the button state has changed

    if (patternbuttonState_Left_Turn == LOW) { //the button state must have just gone high
      patternbuttonState_Left_Turn = 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_Left_Turn = 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;

      }
    }
  }
}


 

On to the next step.
« Last Edit: May 06, 2014, 10:25:04 pm by HolidayV » Logged

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 1478
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You missed PIN_FL!
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You missed PIN_FL!

Hmmmm, you appear to be correct. Any chance we can move on to the next step? It will be 7 hours before I can correct that.
Logged

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 1478
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also you made a slip with your find & replace: "patternbuttonState" has become "patternbuttonState_Left_Turn"

But, more worryingly, look at the last version of the sketch you posted. The "delay(delayTime)" has re-appeared in loop(). I thought we took that out?

I'm not trying to stall or annoy you. In fact this evening I have wired up something very like your circuit so I can test your sketch and suggest the next steps. But I have run out of time for this evening and I am busy tomorrow evening as it is my wife's birthday and we are going out to a nice Italian restaurant.

In my circuit I have wired up a small analog joystick (PS2 controller-style) to A0 and A1. That's why I am using analogRead() instead of digitalRead(). But I'm treating the joystick as though it were separate buttons like yours by comparing the analog reading with 256 or 768 and treating those as equivalent to HIGH or LOW. Also my LEDs are on different pins, just for my wiring convenience. Other than those differences, the logic of the code should work exactly the same for your switches & leds.

I have got the fading and brake lights working with no delay() or while(). The turn signals are still to be done.

Have a read below and come back with any questions. Maybe you can take the next step?


* 20140507_231037.jpg (272.69 KB, 1280x720 - viewed 34 times.)
« Last Edit: May 07, 2014, 05:16:41 pm by PaulRB » Logged

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 1478
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;

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, 125);
  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 = analogRead(LEFT_TURN_SWITCH)<256 ? HIGH:LOW; //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, 125);
  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 = analogRead(Right_Turn_Switch)>768 ? HIGH:LOW; //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 = 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
  {
    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
   
  }
  else if (buttonState_Brake_Switch == HIGH && buttonState_Right_Turn == 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
   
  }
  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
   
  }
  else if (buttonState_Right_Turn == HIGH) //If the switch goes HIGH, act on it
  {
   
  }
  else if (buttonState_Left_Turn == HIGH) //If the switch goes HIGH, act on it
  {
   
  }
  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;
 
        }
      }
    }
  }
}

Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I was in a hurry with my editing. Looks like you've been busy. I'll give your code a shot and see how it works.  Enjoy your dinner with your wife.  smiley-grin
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, so I changed the pin numbers on your code to match the pins on mine and the only thing mine does, is light up. Nothing flashes, nothing fades, the brakelights dont work, nothing.

Correction: I had to go back through and change all the analogRead's to digitalRead's and now it all works with exception of the turn signals as you described.
« Last Edit: May 07, 2014, 09:52:47 pm by HolidayV » Logged

The Netherlands
Offline Offline
Edison Member
*
Karma: 51
Posts: 1729
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ofcourse the question that rises now is do you understand what that code does.
Else your problem might now be (partially) solved, but next time you want to change (like add a function) something, you'll immediately run into trouble.
Logged

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 1478
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'll give your code a shot and see how it works.

No, its still your code! I'm just trying to help you get it do do what you want.

As MAS3 is pointing out, if you are not learning from this, we both failed. So how are you getting on?

I seem to have it all working now, turning while breaking and breaking while turning, but I don't want to post the sketch until I see your completed version. Then we can compare sketches.

Paul
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know what I did, but I have the turn signals working while the brake switch is pressed, but the brake switch doesn't set the brake lights to high now.

Code:
//////Bicycle Light System v1.04////////////

/////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, 125);
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, 125);
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()
{
 analogWrite(LED_RIGHT_FRONT, 255);
 analogWrite(LED_LEFT_FRONT, 255);
 transition();
 setColor();
 delay(delayTime);
  
 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
  }
  
buttonState_Left_Turn = digitalRead(LEFT_TURN_SWITCH); //Continually look at the switch to see if its pressed
if (buttonState_Left_Turn == HIGH) //If the switch goes HIGH, act on it
{
turnLeft(); //new function called turnLeft
}
buttonState_Right_Turn = digitalRead(Right_Turn_Switch); //Continually look at the switch to see if its pressed
if (buttonState_Right_Turn == HIGH) //If the switch goes HIGH, act on it
{
turnRight(); //new function called turnRight
}



digitalRead(FADE_TOGGLE_SWITCH);
  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;

      }
    }
  }
}
« Last Edit: May 11, 2014, 01:15:51 am by HolidayV » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

By the way, here's a pic of my Mini Aduino Pro's that just came in, sitting on top of an UNO. These things are TINY!



* 2014-05-10 20.33.47.jpg (1044.16 KB, 2048x1152 - viewed 26 times.)
Logged

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 1478
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Chris, we seem to be going in circles here. In your latest sketch, the delay () command is still there!

I think your break lights are coming on, but they immediately get set back to the fading values as soon as loop() runs again.

Have another look at the last code I posted and figure out why it doesn't happen there.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul, when I remove the delay command from voidLoop, it breaks the fade sequences leaving the tail lights on at 175 and when I press the brake switch, the lights flicker at 255 instead of a solid light.
Logged

Pages: 1 ... 3 4 [5] 6 7   Go Up
Jump to: