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 most likely no translated for the post!




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

How do I change color in my code


 sid
(@sid)
New Member
Joined: 4 months ago
Posts: 0
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: 8 years ago
Posts: 1845
 

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: 8 years ago
Posts: 1845
 

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: 10 months 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: 8 years ago
Posts: 1845
 

Awesome! Good to hear that! 👍 

Enjoy the holidays and have a great new year! 😉 


ReplyQuote


Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: