HolidayV

Let me just start by saying, I'm a noob. I am trying to understand arduino code but my brain just isn't wrapping around it very fast. Here's the scenario :4 separate LED's - we'll call them L1, L2, L3, and L4. 4 separate switches - we'll call them S1, S2, S3, and S4I need L1 and L2 to flash in unison in 3 or 4 TBD different sequences to be set by toggling using a momentary switch (S1). While these are flashing in any of the above mentioned sequences, I need L3 and L4 lit to HIGH. If I press S2, I need L1 and L2 to stop performing their flashing sequence and immediately light to HIGH, and then resume the flashing sequence upon release of S2. If I press S3, I need L1 to stop it's flashing sequence and begin a different flashing sequence in unison with L3. Upon release of S3, L1 will return to the unison sequence with L2 and L3 will return to HIGH. If I press S4, I need L2 to stop it's flashing sequence and begin a different flashing sequence in unison with L4. Upon release of S4, L2 will return to the unison sequence with L1 and L4 will return to HIGH.

I have already written a program that fades L1 and L2 up and down and turns them to HIGH when S1 is pressed, while L3 and L4 remain in HIGH state. However, the addition of the S1 button press messes with the code and changes the timing of the fade and if I make the fade timing too short, the HIGH state of L1 and L2 flicker while S1 is pressed.

Well done on that description of your requirements. Much better than most noobs. But there are still some missing details. As an example, while S4 is pressed, what is L1 doing? Do we have two separate flashing sequences running in parallel? Also your sketch is doing stuff you did not describe in your first post, like fading the leds. Is that really what you wanted, or just what some code you copied already did?

Can I suggest an approach?

Starting from a completely blank sketch each time, write just enough code to get the leds to light up or flash in just one of the sequences you want. Repeat this for each sequence. Don't worry for the moment that they are in separate sketches, or how you will detect button presses or move between the sketches.

Then post all the sketches on this thread and we can then move on to the next step of merging them.

First of all, you are describing your problem using different names for the variables (Lx, Sx) than the ones used in your sketch (RED, RED2, BRAKE_SWITCH).This doesn't help in tackling your problem.

You have decided to put each step in its own function and are calling all functions in loop().I'm assuming S1 is your BRAKE switch.The function brakelights checks that switch, and sets one (set of) LED(s) to maximum value.After that you are blocking any operation by doing a delay for 5 milliseconds, which is what messes with your timing.If the switch isn't on, this part is skipped and so is the blocking part.

When you're done doing this, you go to the function setColor, and probably set PIN_RED2 to some other value.

To solve this, you need to use a different approach.First of all, dump the delay right away.Only set all LEDs once, during setColor, do not interfere with that at some other time.At this moment, you seem to be at the same PWM level for both red LEDs all the time, except while braking.To have it work correctly, keep your counters running all the time, but have the LED brightness dependent on the brake switch.You need to have an extra variable, that will make you decide RED2 will be set to max value, or to the counter's value during setColor.It is up to you in what function you will make this decision and set the value of red2.

HolidayV

Well done on that description of your requirements. Much better than most noobs. But there are still some missing details. As an example, while S4 is pressed, what is L1 doing? Do we have two separate flashing sequences running in parallel? Also your sketch is doing stuff you did not describe in your first post, like fading the leds. Is that really what you wanted, or just what some code you copied already did?

Can I suggest an approach?

Starting from a completely blank sketch each time, write just enough code to get the leds to light up or flash in just one of the sequences you want. Repeat this for each sequence. Don't worry for the moment that they are in separate sketches, or how you will detect button presses or move between the sketches.

Then post all the sketches on this thread and we can then move on to the next step of merging them.

Paul

While S4 is pressed, L1 is continuing with whatever it's existing flashing sequence is. The fading effect is the first flashing sequence that I did. It's the first of several selectable (by toggling with S1). I will write the separate sketches and post them. Thank You.

HolidayV

First of all, you are describing your problem using different names for the variables (Lx, Sx) than the ones used in your sketch (RED, RED2, BRAKE_SWITCH).This doesn't help in tackling your problem.

You have decided to put each step in its own function and are calling all functions in loop().I'm assuming S1 is your BRAKE switch.The function brakelights checks that switch, and sets one (set of) LED(s) to maximum value.After that you are blocking any operation by doing a delay for 5 milliseconds, which is what messes with your timing.If the switch isn't on, this part is skipped and so is the blocking part.

When you're done doing this, you go to the function setColor, and probably set PIN_RED2 to some other value.

To solve this, you need to use a different approach.First of all, dump the delay right away.Only set all LEDs once, during setColor, do not interfere with that at some other time.At this moment, you seem to be at the same PWM level for both red LEDs all the time, except while braking.To have it work correctly, keep your counters running all the time, but have the LED brightness dependent on the brake switch.You need to have an extra variable, that will make you decide RED2 will be set to max value, or to the counter's value during setColor.It is up to you in what function you will make this decision and set the value of red2.

(Do you see the importance of choosing good variable names here ?)

I apologize for the inconsistencies in my variables. I was at work when I made the post, and did not have my sketch available to post. This code is for bicycle lights. The tail lights will flash in selectable patterns while the front lights stay on high. I have already designed a hardware based system, but I am trying to eliminate the separate lights for turn signals by using the arduino to flash the left and right front and tail lights instead.

So 2 different and simultaneous flashing sequences then. Tricky. But can be done. Definitely a case for the "blink without delay" technique. 3 things at once: 2 flash sequences plus checking for button presses.

}/////////////////Reads the switch: if the switch is pressed, //////////////////////////////////the lights turn on, if the switch is not ////////////////////////////////////pressed, the lights are off /////////////////////////////////void brakelights(){

if (digitalRead(BRAKE_SWITCH) == HIGH)

digitalWrite(PIN_RED, HIGH);

else (digitalRead(BRAKE_SWITCH) == LOW);

digitalWrite(PIN_RED, LOW);

if (digitalRead(BRAKE_SWITCH) == HIGH)

digitalWrite(PIN_RED2, HIGH);

else (digitalRead(BRAKE_SWITCH) == LOW);

digitalWrite(PIN_RED2, LOW);}

HolidayV

Task #3. Left and Right turn signals ONLY override the LEDs corresponding with that sketch. If FR and RED are in the turn sketch, then FL remains lit and RED2 continues with it's toggled sketch. (I'm so bad at this that I couldn't even get the two combined into one sketch):

void loop(){buttonstate = digitalRead(Left_Turn); //Continually look at the switch to see if its pressedif (buttonstate == HIGH) //If the switch goes HIGH, act on it{turnLeft(); //new function called turnLeft}}

void turnLeft()//turnLeft function {buttonstate = HIGH; //the micro the switch is now HIGHdelay(200);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 pressedanalogWrite(PIN_RED2, 255); //Set the Rear Right LED to maximum brightnessanalogWrite(PIN_FL, 255); //Set the Front Right LED to maximum brightnessdelay(200); analogWrite(PIN_RED2, 0); //turn the LED off for a blinking effectanalogWrite(PIN_FL, 0); //turn the LED off for a blinking effectdelay(200);}//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 loopbuttonstate = LOW; //First we tell the micro the switch is now LOWanalogWrite(PIN_RED2, 0); //We turn the LED off before leaving our custom functionanalogWrite(PIN_FL, 0); //We turn the LED off before leaving our custom functiondelay(200);}

void loop(){buttonstate = digitalRead(Right_Turn); //Continually look at the switch to see if its pressedif (buttonstate == HIGH) //If the switch goes HIGH, act on it{turnRight(); //new function called turnRight}}

void turnRight()//turnRight function {buttonstate = HIGH; //the micro the switch is now HIGHdelay(200);while (buttonstate == HIGH) //While the switch is NOT pressed do the following{buttonstate = digitalRead(Right_Turn); //Continually look at the switch to see if its pressedanalogWrite(PIN_RED, 255); //Set the Rear Right LED to maximum brightnessanalogWrite(PIN_FR, 255); //Set the Front Right LED to maximum brightnessdelay(200); analogWrite(PIN_RED, 0); //turn the LED off for a blinking effectanalogWrite(PIN_FR, 0); //turn the LED off for a blinking effectdelay(200);}//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 loopbuttonstate = LOW; //First we tell the micro the switch is now LOWanalogWrite(PIN_RED, 0); //We turn the LED off before leaving our custom functionanalogWrite(PIN_FR, 0); //We turn the LED off before leaving our custom functiondelay(200);}

I'll be honest, I found this code originally designed for RGB's and modified it to work with two Red's and that's how the original sketch was written. I thought it was a little odd myself too, but it worked, so I didn't try to fix it.

Task #3. Left and Right turn signals ONLY override the LEDs corresponding with that sketch.

And the only difference between these 2 sketches is the left vs right difference?

I like how you have broken the problem down. I suggest tackling merging the sketches in tasks #1 & 3 first. This should leave you with only 3 sketches to merge at the end.

If done correctly, that final merge should be really easy. Before that comes the more difficult task: Removing all the delay() commands from your 3 sketches! Read the "blink without delay" example, then have a go at just one of your 3 sketches. When that works without delays, work on the other two.