Guest User

Untitled

a guest
Apr 27th, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 KB | None | 0 0
  1. //Modern turn lights v.6.1.0 with arduino or attiny85 and ws2812b - individual strips easy to embed - tail, reverse, brake and turn signal - by Fedaceag Ionut ( Youtube - Think small, build big! )
  2. #include <FastLED.h> //FastLed library version 3.2.1 - https://github.com/FastLED/FastLED/wiki/Overview or http://fastled.io/ with NEOPIXEL or WS2812B
  3. //#define NUM_STRIPS 1 //number of led strips
  4. #define NUM_LEDS_PER_STRIP 288 //number of leds per each strip
  5. #define LED_PIN 2 //LED Strip Signal Connection
  6. CRGB leds[NUM_LEDS_PER_STRIP];
  7.  
  8. unsigned long turnColor = 0xff6a00; //turn signal color
  9. unsigned long startupcolordim = 0x5a0000;
  10. unsigned long startupcolorbright = 0xff0000;
  11. unsigned long black = 0x000000;
  12. //input pins for arduino
  13. const int buttonPinTS = 6; // turn signal pin for arduino
  14. const int buttonPinTailLights = 3; // night / tail lights for arduino
  15. const int buttonPinBreak = 4; // brake lights for arduino
  16. const int buttonPinReverseLights = 5; // reverse lights for arduino
  17. const int buttonPinStartup = 9;
  18.  
  19. int Startup = 0;
  20. int TailLights = 0;
  21. int ReverseLights = 0;
  22. int Break = 0;
  23.  
  24. int TailState = 0;
  25. int stateTS = 0;
  26. int stateTSRuns = 0;
  27. int ReverseState = 0;
  28. int BrakeState = 0;
  29. int StartupState = 0;
  30. int buttonStateTS = 0;
  31. int buttonstartupstate = 0;
  32.  
  33. int maxBrtBreak = 254; // maxim brightness Break lights - from 0 to 254
  34. int maxBrtTailLights = 70; // maxim brightness tail lights - from 0 to 254
  35. int maxBrtReverseLights = 254;
  36. int tailLightModel[]= {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,
  37. 145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,
  38. 259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288};
  39.  
  40. int reverseLightModel[] = {20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, 248,249,250,251,252,253,254,255,256,257,258,
  41. 259,260,261,262,263,264,265,266,267,268};
  42.  
  43. int turnsignalmodel[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
  44.  
  45. int delayTurnLedAnim = 1; //delay of each led in turn light animation
  46. int delayTurnLedOff = 70; //delay from animation to black (is used twice)
  47. int delayLedToDayLight = 500; // delay from animation to day light on
  48. int nrAnimAfterOff = 3; // number of animations for a single impulse
  49. int StartupSpeed = 6;
  50.  
  51. unsigned long currentMillis = 0;
  52. unsigned long previousMillis = 0;
  53.  
  54.  
  55. void setup() {
  56. //Serial.begin(115200);
  57. FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS_PER_STRIP);
  58. pinMode(buttonPinTS, INPUT);
  59. pinMode(buttonPinBreak, INPUT);
  60. pinMode(buttonPinTailLights, INPUT);
  61. pinMode(buttonPinReverseLights, INPUT);
  62. pinMode(buttonPinStartup, INPUT);
  63.  
  64.  
  65. for (int i = 0; i < (NUM_LEDS_PER_STRIP/2); i++) { //beggining of Startup Sweep loop
  66.  
  67.  
  68. leds[i] = startupcolordim;
  69. leds[i-1] = black;
  70. leds[(NUM_LEDS_PER_STRIP-1)-i] = startupcolordim;
  71. leds[(NUM_LEDS_PER_STRIP)-i] = black;
  72. FastLED.show();
  73. delay (StartupSpeed);
  74. }
  75.  
  76. for (int j = ((NUM_LEDS_PER_STRIP/2)-1); j >= 0; j--)
  77. {
  78. leds[j] = startupcolordim;
  79. leds[(NUM_LEDS_PER_STRIP/2-1)+((NUM_LEDS_PER_STRIP/2)-j)] = startupcolordim;
  80. FastLED.show();
  81. delay (StartupSpeed);
  82. }
  83.  
  84. for (int j = ((NUM_LEDS_PER_STRIP/2)-1); j >= 0; j--)
  85. {
  86. leds[j] = startupcolorbright;
  87. leds[(NUM_LEDS_PER_STRIP/2-1)+((NUM_LEDS_PER_STRIP)/2-j)] = startupcolorbright;
  88. FastLED.show();
  89. delay (StartupSpeed);
  90. }
  91.  
  92. for (int j = 255; j >= 60; j--)
  93. {
  94. for (int i = 0; i < NUM_LEDS_PER_STRIP; i++)
  95. {
  96. leds[i] = CRGB(j, 0, 0);
  97. }
  98. FastLED.show();
  99. delay (5);
  100. }
  101.  
  102. //led strip front left for arduino
  103. //FastLED.addLeds<NEOPIXEL, 0>(leds[0], NUM_LEDS_PER_STRIP); //led strip front left for attiny85
  104.  
  105. fill_solid(leds, NUM_LEDS_PER_STRIP, CRGB::Red); // some led strips are all on at power on, so let's power them off at boot
  106. FastLED.show();
  107.  
  108.  
  109.  
  110. }
  111.  
  112. void loop() {
  113. // read the input state
  114. buttonStateTS = digitalRead(buttonPinTS);
  115.  
  116. if(buttonStateTS == HIGH){
  117. stateTS = 1;
  118. }
  119.  
  120. // turn signal function
  121. if(stateTS != 0){
  122.  
  123. for(int dot = 0; dot < turnsignalmodel; dot++) {
  124.  
  125. leds[dot] = turnColor;
  126.  
  127. //delay(delayTurnLedAnim);
  128. currentMillis = previousMillis = millis();
  129. while(previousMillis + delayTurnLedAnim >= currentMillis){
  130. stateTSRuns = 1;
  131. TailAndBrake();
  132. FastLED.show();
  133. currentMillis = millis();
  134. }
  135.  
  136. }
  137.  
  138. //delay(delayTurnLedOff);
  139. currentMillis = previousMillis = millis();
  140. while(previousMillis + delayTurnLedOff >= currentMillis){
  141. stateTSRuns = 1;
  142. TailAndBrake();
  143. FastLED.show();
  144. currentMillis = millis();
  145. }
  146.  
  147. fill_solid(leds, NUM_LEDS_PER_STRIP, CRGB::Black);
  148. stateTSRuns = 1;
  149. TailAndBrake();
  150. FastLED.show();
  151.  
  152. //delay(delayTurnLedOff);
  153. currentMillis = previousMillis = millis();
  154. while(previousMillis + delayTurnLedOff >= currentMillis){
  155. stateTSRuns = 1;
  156. TailAndBrake();
  157. FastLED.show();
  158. currentMillis = millis();
  159. }
  160.  
  161. buttonStateTS = digitalRead(buttonPinTS);
  162. if(buttonStateTS == HIGH){
  163. stateTS = 1;
  164. }
  165.  
  166. stateTS++;
  167. if(stateTS >= nrAnimAfterOff && buttonStateTS != HIGH){
  168. stateTS = 0;
  169. //delay(delayLedToDayLight);
  170. currentMillis = previousMillis = millis();
  171. while(previousMillis + delayLedToDayLight >= currentMillis){
  172. stateTSRuns = 1;
  173. TailAndBrake();
  174. FastLED.show();
  175. currentMillis = millis();
  176. }
  177. }
  178.  
  179. }else{
  180.  
  181. fill_solid(leds, NUM_LEDS_PER_STRIP, CRGB::Black);
  182.  
  183. TailAndBrake();
  184. FastLED.show();
  185. }
  186. }
  187.  
  188.  
  189. //function for Brake lights, tail lights and reverse lights
  190. void TailAndBrake(){
  191.  
  192. Break = digitalRead(buttonPinBreak);
  193. TailLights = digitalRead(buttonPinTailLights);
  194. ReverseLights = digitalRead(buttonPinReverseLights);
  195. Startup = digitalRead(buttonPinStartup);
  196.  
  197. if(TailLights == HIGH && Break == LOW && stateTSRuns == 0){
  198.  
  199. for(int i = 0; i <= sizeof(tailLightModel) / sizeof(tailLightModel[0]); i++){
  200. leds[tailLightModel[i]]=CHSV(0,254,maxBrtTailLights);
  201. }
  202.  
  203. }else if(Break == HIGH && stateTSRuns == 0){
  204.  
  205. fill_solid( leds, NUM_LEDS_PER_STRIP, CHSV(0,255,maxBrtBreak));
  206.  
  207. }else if(TailLights == HIGH && Break == LOW && stateTSRuns == 1){
  208.  
  209. int nrLedsTail = NUM_LEDS_PER_STRIP / 4;
  210. for(int i = 0; i <= nrLedsTail; i++){
  211. leds[i]=CHSV(0,254,maxBrtTailLights);
  212. }
  213.  
  214. }else if(Break == HIGH && stateTSRuns == 1){
  215.  
  216. int nrLedsBrake = NUM_LEDS_PER_STRIP / 4;
  217. for(int i = 0; i <= nrLedsBrake; i++){
  218. leds[i]=CHSV(0,254,maxBrtBreak);
  219. }
  220.  
  221. }
  222.  
  223. if(ReverseLights == HIGH){
  224. for(int i = 0; i < sizeof(reverseLightModel) / sizeof(reverseLightModel[0]); i++){
  225. leds[reverseLightModel[i]]=CHSV(0,0,maxBrtReverseLights);
  226. }
  227. }
  228. stateTSRuns = 0;
  229.  
  230.  
  231. }
Advertisement
Add Comment
Please, Sign In to add comment