Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



How do I change col...
 
Share:
Notifications
Clear all

[Solved] How do I change color in my code

9 Posts
3 Users
0 Likes
2,985 Views
 sid
(@sid)
Active Member
Joined: 3 years ago
Posts: 3
Topic starter  

Hi,

I need to be able to use different color in following code for each input, Red for Brake and Yellow for turn signals.

The current code changes color for all inputs. I appreciate your help in advance.

thanks

 

int Pin2 = 9; // pin 9 is used for Left turn
int Left;
int Pin3 = 8; // pin 8 is used for Right turn 
int Right;
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds < WS2812, PIN, GRB > (leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  pinMode(Pin1, INPUT);
  pinMode(Pin2, INPUT);
  pinMode(Pin3, INPUT);

}
void loop() {

  Stop = digitalRead(Pin1);
  Left = digitalRead(Pin2);
  Right = digitalRead(Pin3);
  colorWipe(0xff, 0xff, 0x00, 150);
  colorWipe(0x00, 0x00, 0x00, 150);
}

void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {

  if (Stop == HIGH) {

    for (int i = 0; i <= (NUM_LEDS / 2 - 1); ++i) {
      leds[i] = CRGB(red, green, blue);
      leds[NUM_LEDS - i - 1] = CRGB(red, green, blue);
      delay(SpeedDelay);
      FastLED.show();
    }

    if (Stop == LOW) {
      FastLED.clear();
    }

  } else if (Right == HIGH) {
    for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++) {
      leds[i] = CRGB(red, green, blue); // removed the "mirror" leds
      FastLED.show();
      delay(60); // chnage the number here to chnage the speed of the LEDs
    }

    if (Right == LOW) {
      FastLED.clear();

    }

  } else if (Left == HIGH) {
    for (int i = NUM_LEDS / 2 - 1; i >= 0; i--) { //left turn
      leds[i] = CRGB(red, green, blue); // removed the "mirror" leds
      FastLED.show();
      delay(60); // chnage the number here to chnage the speed of the LEDs
    }

    if (Left == LOW) {
      FastLED.clear();

    }
  }
}

   
ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
 

Hi Sid,

welcome to the forum! 😊 

I also notice that you lock "if(Stop==LOW)" inside the IF-loop where you test for Stop==HIGH - so that condition will never be reached (if I'm reading this right - note: always a good idea to attach the INO file to your post in the future, but as a new user you will have to post a few legit posts first before you can do that).

So example this part of the code: the bold red code will never be executed, since in this section Stop==HIGH.

if (Stop == HIGH) {
  for (int i = 0; i <= (NUM_LEDS / 2 - 1); ++i) {
    leds[i] = CRGB(red, green, blue);
    leds[NUM_LEDS - i - 1] = CRGB(red, green, blue);
    delay(SpeedDelay);
    FastLED.show();
  }
 
  if (Stop == LOW) {
    FastLED.clear();
  }
}

Now you could move that IF outside of that initial IF-loop, but then this would set the LEDs to black each time - not what you had in mind.
So to avoid all that, I'd probably modify the colorSwpie function a little bit, since the leds are all turned off when all 3 (left, right, stop) are not HIGH.

 

Tip: You can use shorter colors notations, see the FastLED color reference list if you'd like to use those (not required of course, but it does make the code more readable if you use just a few basic colors). For example  CRGB(0x00,0x00,0x00) = CRGB::Black   and   CRGB(0xFF,0xFF,0x00) = CRGB::Yellow. 😊 

Also not sure if required, but I think you have to do a FastLED.show() after a FastLED.clear(). I could be wrong though.

 

So you could do something like this (I removed the speedDelay since it didn't have much of a purpose there):

!! Don't forget:
You will have to add the beginning of your code (#defines, #include, int's and void setup()) - since this code was a little incomplete, probably because I tried putting it in code format in your post.
The bold parts of the code are my changes.

...
#define BrakeDelay 150
#define BlinkerDelay 60
...

void loop() { Stop = digitalRead(Pin1); Left = digitalRead(Pin2); Right = digitalRead(Pin3); colorWipe(CRGB::Yellow); colorWipe(CRGB::Black); } void colorWipe(CRGB WipeColor) { if (Stop == HIGH) // brake - outsides to center { for (int i = 0; i <= (NUM_LEDS / 2 - 1); ++i) { leds[i] = WipeColor; leds[NUM_LEDS - i - 1] = WipeColor; delay(BrakeDelay); FastLED.show(); } } else if (Right == HIGH) // right turn - right to center { for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++) { leds[i] = WipeColor; // removed the "mirror" leds FastLED.show(); delay(BlinkerDelay); // change the number here to change the speed of the LEDs } } else if (Left == HIGH) //left turn - left to center { for (int i = NUM_LEDS / 2 - 1; i >= 0; i--) { leds[i] = WipeColor; // removed the "mirror" leds FastLED.show(); delay(BlinkerDelay); // change the number here to change the speed of the LEDs } } else { FastLED.clear(); FastLED.show(); } }

 

I couldn't resist so I made another modification, so Stop becomes red and Left/Right remains yellow (note: I have not tested this with switches).

...
#define BrakeDelay 150
#define BlinkerDelay 60
...
void loop() { Stop = digitalRead(Pin1); Left = digitalRead(Pin2); Right = digitalRead(Pin3); if( (Stop==HIGH) || (Left==HIGH) || (Right==HIGH)) { if(Stop==HIGH) { colorWipe(CRGB::Red); } else { colorWipe(CRGB::Yellow); } colorWipe(CRGB::Black); } else { FastLED.clear(); FastLED.show(); } } void colorWipe(CRGB WipeColor) { if (Stop == HIGH) // brake - outsides to center { for (int i = 0; i <= (NUM_LEDS / 2 - 1); ++i) { leds[i] = WipeColor; leds[NUM_LEDS - i - 1] = WipeColor; delay(BrakeDelay); FastLED.show(); } } else if (Right == HIGH) // right turn - right to center { for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++) { leds[i] = WipeColor; FastLED.show(); delay(BlinkerDelay); } } else if (Left == HIGH) //left turn - left to center { for (int i = NUM_LEDS / 2 - 1; i >= 0; i--) { leds[i] = WipeColor; FastLED.show(); delay(BlinkerDelay); } } }

 

And another note: this all can be done more efficient, but I think this should do what you want it to do, 😊 


   
ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
 

p.s. this is a related post for a blinker in this forum, and the result of that conversation can also be found on GitHub.
It is missing the stop/brake though.


   
ReplyQuote
(@saeidans)
Active Member
Joined: 4 years ago
Posts: 8
 

@hans Hello Hans,

thank you for taking the time to look at the my code.

I applied your suggestions and it seems to be working as desired.

 

thanks again


   
ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
 

Awesome! Good to hear that! 👍 

Enjoy the holidays and have a great new year! 😉 


   
ReplyQuote
 sid
(@sid)
Active Member
Joined: 3 years ago
Posts: 3
Topic starter  

@hans

Hello again

I need another favor to ask. I need to make some changes so that when the Hazard light is on ( both left and right turn signals are flashing ) , the LEDs stay off even when the brake is on. other words, the turn signal effect should only work when the left or right turn signal is on. brake should not have any effect when either doing turn signal or the hazard

I could not figure out how to make this change so it wont affect other functions of the code.

would you please kindly help me with that?

thanks

 


   
ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
 

Let me see if I understand this correctly; so the effect should only work for turning left OR turning right?

In that case I'd change this line (in void loop() ):

if( (Stop==HIGH) || (Left==HIGH) || (Right==HIGH))

 

to something like this:

if( (Stop==HIGH) || ( (Left==HIGH) && (Right!=HIGH) ) || ( (Right==HIGH) && (Left!=HIGH) )

 

So only show the effect when stop is high (since you mentioned that stop should do anything, you may want to remove that), and only when just going LEFT (and not RIGHT), or going RIGHT (and not LEFT)

Without STOP:

if( ( (Left==HIGH) && (Right!=HIGH) ) || ( (Right==HIGH) && (Left!=HIGH) )

   
ReplyQuote
 sid
(@sid)
Active Member
Joined: 3 years ago
Posts: 3
Topic starter  

@hans

I tried your suggestions but it threw up an error for each.

I have a better suggestion . can the code from video below be modified to just have the right, left and brake effect with brake effect of my code ( Leds from out to center and turning off in same way just for the brake mode and not any other combinations. meaning in hazard + brake or brake + left or brake + right the number of leds for brake can stay solid.

 

please refer to this video , the code can be downloaded form the description of the video

 

https://www.youtube.com/watch?v=-kA0ffAG03A&t=122s

 

thank you

 


   
ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
 

What kind of error did you get?

As for adapting the code from that video (which looks pretty cool by the way):

You see in the code that Hasitha (the developer) made functions for each of the effects (at the end of the code).
Simply do not call the ones you do not need, and modify the ones you'd like modified.
Shouldn't take too much effort.

Note: For reference and convenience, I've uploaded the code that goes with the video ... just incase


   
ReplyQuote
Share: