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

I just realized that if the brake switch is pressed, the turn signal won't work simultaneously. It doesn't matter if the turn signal is turned on first because the rear LEDs are set to high when not flashing in the turn signal circuit, but if someone is riding the brake while approaching the turn, the signal doesn't flash. How can I fix this?

Can you see now why I was surprised when you said it was working? I said at the time I thought there was more to do.

Like MAS3 says, the delay() functions have to go, as will the while() loops. These will need to be replaced with more variables to record the current states of the various lights and the times that the last or next changes have to happen.

Think of it like this. You have 6 states:
1. Normal, no buttons pressed, fading light pattern running
2. Braking
3. Left turn
4. Left turn and braking
5. Right turn
6. Right turn and braking

Make a new variable to hold that state, or determine it from the button states.
« Last Edit: May 04, 2014, 12:27:10 am by PaulRB » Logged

Offline Offline
Edison Member
*
Karma: 65
Posts: 2499
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Car lights have one end at Ground.
They're on/off by interrupting +V to the lamp (not Ground.)
I guess it's a good thing this light set up is for a bicycle and not a car.

If Frame == Ground then that's one less wire to run.  Just sayin'.

> > >  Or you could make frame == +V
« Last Edit: May 04, 2014, 08:53:19 pm by Runaway Pancake » Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

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

Car lights have one end at Ground.
They're on/off by interrupting +V to the lamp (not Ground.)
I guess it's a good thing this light set up is for a bicycle and not a car.

If Frame == Ground then that's one less wire to run.  Just sayin'.

> > >  Or you could make frame == +V

Yes, but the design of this kit will be all self contained.without needing to locate a ground screw or bolt to attach the ground wire to. It's being designed as full plug and play with as few tools needed to install as possible. You do bring up a good point for someone else designing their own from this code though. Thanks.
Logged

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

I just realized that if the brake switch is pressed, the turn signal won't work simultaneously. It doesn't matter if the turn signal is turned on first because the rear LEDs are set to high when not flashing in the turn signal circuit, but if someone is riding the brake while approaching the turn, the signal doesn't flash. How can I fix this?

Can you see now why I was surprised when you said it was working? I said at the time I thought there was more to do.

Like MAS3 says, the delay() functions have to go, as will the while() loops. These will need to be replaced with more variables to record the current states of the various lights and the times that the last or next changes have to happen.

Think of it like this. You have 6 states:
1. Normal, no buttons pressed, fading light pattern running
2. Braking
3. Left turn
4. Left turn and braking
5. Right turn
6. Right turn and braking

Make a new variable to hold that state, or determine it from the button states.

And here's where my "Noobiness" shines. My brain started to hurt when I read this. I tried to apply "BlinkWithoutDelay" to the turn signals but the example is controlling one LED and each of my turn signals control more than one. Needless to say, I failed miserably. To take a break, I DID manage to re-code MAKEzine's "Hot and Cold LED" project to work with a 4 pin ping sensor rather than the 3 pin in their example without any examples to work from, so I AM learning a little. I guess I just took on a project a little too big for myself too soon.
Logged

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

I guess I just took on a project a little too big for myself too soon.
So... with our support do you want to take the next steps?

There's no need for your brain to hurt. I would imagine that once you "get it", you will think "oh, OK, that's what they meant".
Logged

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

Yes please.
Logged

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

Can you re-post your sketch?

Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
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 = 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

// 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 = HIGH;

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

void setup()
{
  pinMode(PIN_TOGG, INPUT);
  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 turnLeft()//turnLeft function
{
analogWrite(PIN_FR, 255);
analogWrite(PIN_RED, 125);
analogWrite(PIN_RED2, 125);
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);
analogWrite(PIN_RED, 125);
analogWrite(PIN_RED2, 125);
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, 125); //We turn the LED off before leaving our custom function
analogWrite(PIN_RED2, 125); //We turn the LED off before leaving our custom function
}

// Smoothly changes the color values
void transition()
{
  if (red >= 175)
    incR = 0;
  else if (red <= 20)
    incR = 1;
  if (red2 >= 175)
    incR2 = 0;
  else if (red2 <= 20)
    incR2 = 1;
 
  if (incR)
    red++;
  else
    red--;
  if(incR2)
    red2++;
  else
    red2--;
}

// Sets the output voltage on the LED pins.
void setColor()
{
  analogWrite(PIN_RED, red);
  analogWrite(PIN_RED2, red2);
}
 
 
void loop()
{
 analogWrite(PIN_FR, 255);
 analogWrite(PIN_FL, 255);
 transition();
 setColor();
 delay(delayTime);
 
 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
{
turnRight(); //new function called turnRight
}

digitalRead(PIN_TOGG);
  if (digitalRead(PIN_TOGG) != 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;
          red = 175;
          red2 = 175;
          incR = 1;
          incR2 = 1;
          delayTime = 5;
          break;

case 2://Pattern 2 has been running
          //set up pattern 3
          currentPattern = 3;
          red = 175;
          red2 = 175;
          incR = 1;
          incR2 = 1;
          delayTime = 1;
          break;

case 3://Pattern 3 has been running
          //set up pattern 4
          currentPattern = 4;
          red = 175;
          red2 = 20;
          incR = 1;
          incR2 = 1;
          delayTime = 10;
          break;

case 4://Pattern 4 has been running
          //set up pattern 5
          currentPattern = 5;
          red = 175;
          red2 = 20;
          incR = 1;
          incR2 = 1;
          delayTime = 5;
          break;
         
case 5://Pattern 5 has been running
          //set up pattern 6
          currentPattern = 6;
          red = 175;
          red2 = 20;
          incR = 1;
          incR2 = 1;
          delayTime = 1;
          break;
         
case 6://Pattern 6 has been running
          //set up pattern 7
          currentPattern = 7;
          red = 175;
          red2 = 175;
          incR = 0;
          incR2 = 0;
          delayTime = 0;
          break;
         
case 7:
          //set up pattern 1
          currentPattern = 1;
          red = 175;
          red2 = 175;
          incR = 1;
          incR2 = 1;
          delayTime = 10;
          break;

      }
    }
  }
}


Logged

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

Chris, that sketch doesn't compile:

Code:
HolidayV.cpp: In function ‘void turnLeft()’:
HolidayV.cpp:82:5: error: expected ‘;’ before ‘buttonstate’
HolidayV.cpp: In function ‘void turnRight()’:
HolidayV.cpp:106:5: error: expected ‘;’ before ‘button_state’
HolidayV.cpp: In function ‘void brakelights()’:
HolidayV.cpp:127:5: error: expected ‘;’ before ‘button_state’

The problem seems to be the word "loop" after the end of each of your 3 while statements in turnLeft(), turnRight() and brakelights(). That's not valid C/C++. It compiles OK if you remove those "loop" words.

Post the one you had working.
« Last Edit: May 05, 2014, 03:22:42 pm by PaulRB » Logged

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

Lets take it one step at a time and test after each change. First we will remove the delay() in loop(). This is the one that controls the fading of the red (rear?) leds.

Just as in "BlinkWithoutDelay", we need a variable to record the time that we last updated the fading pattern:

Code:
unsigned long lastPatternUpdate = 0;

(It must be an unsigned long because that is the type of value returned by the millis() function that we want to compare it to.)

Then in loop(), we remove the delay like this:
Code:
  if (millis() - lastPatternUpdate >= delayTime) {
    lastPatternUpdate = millis();
    transition();
    setColor();
  }

As the Arduino executes loop() over and over, eventually the difference between millis() and lastPatternUpdate will be greater than delayTime, and then transition() and set Color() get run. We also re-set lastPatternUpdate to the current value of millis(), so that this code does not get executed again until another delayTime interval has passed.

Make these changes to your (compiling/working) sketch. Does it still work OK?
Logged

The Netherlands
Offline Offline
Edison Member
*
Karma: 49
Posts: 1619
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If i copy / paste the last posted code to IDE 1.0.5-r2, it will compile without errors.
So every line that needs it has it's semicolon.

The only times i see "loop" words, is in the //comments, and of course in void loop().
As comments are quite rudimentary, you could choose a synonym to the word "loop" or just dump it.

In my last post, i did identify while next to delay() as blocking code, but removed it (i didn't check how many whiles there actualy are).
While is a blocking code because it exclusively performs the code between the {curly braces} as long as the condition isn't met yet.

The blink without delay sketch is about keeping track of what you are doing and what you have been doing.
That means you need to store some values, and compare them each iteration of the program (the loop() ).
Once you do that and dump delay() and while(), you'll run through loop() very fast.
That will grant you time to do things while waiting for some other things to meet a set condition.

Try to understand that blink without delay sketch.
Once you do, you'll recognise that there isn't a lot of difference (none that i can think of right now) between delay() and while() if you can handle them using this technique.
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: 42
Posts: 1323
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Re: the "loop" word comments. My bad, I think. I did an Auto Format Sketch before compiling. That shouldn't have caused compile errors, but I did nothing else other than copy & paste from Firefox to the IDE...
Logged

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

Chris, that sketch doesn't compile:

Code:
HolidayV.cpp: In function ‘void turnLeft()’:
HolidayV.cpp:82:5: error: expected ‘;’ before ‘buttonstate’
HolidayV.cpp: In function ‘void turnRight()’:
HolidayV.cpp:106:5: error: expected ‘;’ before ‘button_state’
HolidayV.cpp: In function ‘void brakelights()’:
HolidayV.cpp:127:5: error: expected ‘;’ before ‘button_state’

The problem seems to be the word "loop" after the end of each of your 3 while statements in turnLeft(), turnRight() and brakelights(). That's not valid C/C++. It compiles OK if you remove those "loop" words.

Post the one you had working.

Originally, I compiled, loaded, tested, copied, and pasted to page 3. When I reposted, I was at work and simply quoted my original post from the previous page, so there shouldn't be any reason why it wouldn't work.
Logged

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


The problem seems to be the word "loop" after the end of each of your 3 while statements in turnLeft(), turnRight() and brakelights(). That's not valid C/C++. It compiles OK if you remove those "loop" words.

Post the one you had working.
I searched the whole code and the only instances of "loop" are in descriptions following "//" or "voidloop". Here's the sketch again

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= 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

// 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 = HIGH;

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

void setup()
{
  pinMode(PIN_TOGG, INPUT);
  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 turnLeft()//turnLeft function
{
analogWrite(PIN_FR, 255);
analogWrite(PIN_RED, 125);
analogWrite(PIN_RED2, 125);
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);
analogWrite(PIN_RED, 125);
analogWrite(PIN_RED2, 125);
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, 125); //We turn the LED off before leaving our custom function
analogWrite(PIN_RED2, 125); //We turn the LED off before leaving our custom function
}

// Smoothly changes the color values
void transition()
{
  if (red >= 175)
    incR = 0;
  else if (red <= 20)
    incR = 1;
  if (red2 >= 175)
    incR2 = 0;
  else if (red2 <= 20)
    incR2 = 1;
 
  if (incR)
    red++;
  else
    red--;
  if(incR2)
    red2++;
  else
    red2--;
}

// Sets the output voltage on the LED pins.
void setColor()
{
  analogWrite(PIN_RED, red);
  analogWrite(PIN_RED2, red2);
}
 
 
void loop()
{
 analogWrite(PIN_FR, 255);
 analogWrite(PIN_FL, 255);
 transition();
 setColor();
 delay(delayTime);
 
 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
{
turnRight(); //new function called turnRight
}

digitalRead(PIN_TOGG);
  if (digitalRead(PIN_TOGG) != 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;
          red = 175;
          red2 = 175;
          incR = 1;
          incR2 = 1;
          delayTime = 5;
          break;

case 2://Pattern 2 has been running
          //set up pattern 3
          currentPattern = 3;
          red = 175;
          red2 = 175;
          incR = 1;
          incR2 = 1;
          delayTime = 1;
          break;

case 3://Pattern 3 has been running
          //set up pattern 4
          currentPattern = 4;
          red = 175;
          red2 = 20;
          incR = 1;
          incR2 = 1;
          delayTime = 10;
          break;

case 4://Pattern 4 has been running
          //set up pattern 5
          currentPattern = 5;
          red = 175;
          red2 = 20;
          incR = 1;
          incR2 = 1;
          delayTime = 5;
          break;
         
case 5://Pattern 5 has been running
          //set up pattern 6
          currentPattern = 6;
          red = 175;
          red2 = 20;
          incR = 1;
          incR2 = 1;
          delayTime = 1;
          break;
         
case 6://Pattern 6 has been running
          //set up pattern 7
          currentPattern = 7;
          red = 175;
          red2 = 175;
          incR = 0;
          incR2 = 0;
          delayTime = 0;
          break;
         
case 7:
          //set up pattern 1
          currentPattern = 1;
          red = 175;
          red2 = 175;
          incR = 1;
          incR2 = 1;
          delayTime = 10;
          break;

      }
    }
  }
}


 
 


 

Logged

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

Lets take it one step at a time and test after each change. First we will remove the delay() in loop(). This is the one that controls the fading of the red (rear?) leds.

Just as in "BlinkWithoutDelay", we need a variable to record the time that we last updated the fading pattern:

Code:
unsigned long lastPatternUpdate = 0;

(It must be an unsigned long because that is the type of value returned by the millis() function that we want to compare it to.)

Then in loop(), we remove the delay like this:
Code:
 if (millis() - lastPatternUpdate >= delayTime) {
    lastPatternUpdate = millis();
    transition();
    setColor();
  }

As the Arduino executes loop() over and over, eventually the difference between millis() and lastPatternUpdate will be greater than delayTime, and then transition() and set Color() get run. We also re-set lastPatternUpdate to the current value of millis(), so that this code does not get executed again until another delayTime interval has passed.

Make these changes to your (compiling/working) sketch. Does it still work OK?

Inserted here:
Code:
// 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;  //Just as in "BlinkWithoutDelay", we need a variable to record the time that we last updated the fading pattern:

int currentPattern = 7;
int patternButtonState = HIGH;

Deleted delayTime here:
Code:
void loop()
{
 analogWrite(PIN_FR, 255);
 analogWrite(PIN_FL, 255);
 transition();
 setColor();

  
 button__state = digitalRead(BRAKE_SWITCH); //Continually look at the switch to see if its pressed

Inserted Here:
Code:
void loop()
{
 analogWrite(PIN_FR, 255);
 analogWrite(PIN_FL, 255);


if (millis() - lastPatternUpdate >= delayTime) {
    lastPatternUpdate = millis();
    transition();
    setColor();
  }
 
 button__state = digitalRead(BRAKE_SWITCH); //Continually look at the switch to see if its pressed

Everything working so far.
« Last Edit: May 05, 2014, 10:36:25 pm by HolidayV » Logged

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