Advertisement
atuline

Turn signal

Nov 29th, 2019
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. /* Turn signal
  2. *
  3. * By: Andrew Tuline
  4. * Date: Nov 29, 2019
  5. *
  6. * To re-create a turn signal, both with delay based code (ugh), as well as non-delay based code. If you want to use buttons, you need to get rid of those delays.
  7. *
  8. */
  9.  
  10. #include <FastLED.h>
  11.  
  12. #define LED_TYPE WS2812B //TYPE OF LED
  13. #define COLOUR_ORDER GRB //SEQUENCA OF DATA STREAM
  14. #define NUM_LEDS 10 // Number of LEDS
  15. #define DATA_PIN D5 // D8 D1 mini
  16.  
  17. CRGB leds[NUM_LEDS];
  18.  
  19.  
  20.  
  21. void setup() {
  22.  
  23. FastLED.addLeds<LED_TYPE, DATA_PIN, COLOUR_ORDER>(leds, NUM_LEDS); // for GRB LEDs
  24. FastLED.setBrightness(150);
  25.  
  26. } // setup()
  27.  
  28.  
  29.  
  30. void loop() {
  31.  
  32. // method1();
  33. method2();
  34.  
  35. } // loop()
  36.  
  37.  
  38.  
  39. void method1() { // This uses delays and is nice and simple to understand for beginners.
  40.  
  41. for(int i=0;i<NUM_LEDS;i++){
  42. leds[i].setRGB(100,42,0);
  43. FastLED.show();
  44. delay(50); }
  45.  
  46. for(int i=0;i<NUM_LEDS;i++){
  47. leds[i].setRGB(0,0,0);
  48. FastLED.show();
  49. delay(50); }
  50.  
  51. }
  52.  
  53.  
  54.  
  55. void method2() { // This doesn't use delays, but takes a bit more thought.
  56.  
  57. int myNum = (millis() / 50) % 20; // Realtime counter from 0 to 19 (or 2 * NUM_LEDS).
  58. leds[myNum %10] = (myNum < 10) ? 0 : 0x642a00; // Assigns value to leds of 0 through 9 and if myNUM > 9, then give it a colour.
  59. FastLED.show();
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement