Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 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 160 //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; // maxim brightness reverse lights - from 0 to 254
  36. int tailLightModel[] = {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, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160}; // model array for tail lights leds from 0 - 49 for 50 leds / strip
  37. int reverseLightModel[] = {13,14,15,16,17,18,19,20,21,22};
  38. int turnsignalmodel[] = {0,2,4,6,8,10,12,14}; // model array for reverse lights leds from 0 - 49 for 50 leds / strip
  39.  
  40. int delayTurnLedAnim = 1; //delay of each led in turn light animation
  41. int delayTurnLedOff = 70; //delay from animation to black (is used twice)
  42. int delayLedToDayLight = 500; // delay from animation to day light on
  43. int nrAnimAfterOff = 3; // number of animations for a single impulse
  44. int StartupSpeed = 6;
  45.  
  46. unsigned long currentMillis = 0;
  47. unsigned long previousMillis = 0;
  48.  
  49.  
  50. void setup() {
  51. //Serial.begin(115200);
  52. pinMode(buttonPinTS, INPUT);
  53. pinMode(buttonPinBreak, INPUT);
  54. pinMode(buttonPinTailLights, INPUT);
  55. pinMode(buttonPinReverseLights, INPUT);
  56. pinMode(buttonPinStartup, INPUT);
  57.  
  58.  
  59. for (int i = 0; i < (NUM_LEDS_PER_STRIP/2); i++) { //beggining of Startup Sweep loop
  60.  
  61.  
  62. leds[i] = startupcolordim;
  63. leds[i-1] = black;
  64. leds[(NUM_LEDS_PER_STRIP-1)-i] = startupcolordim;
  65. leds[(NUM_LEDS_PER_STRIP)-i] = black;
  66. FastLED.show();
  67. delay (StartupSpeed);
  68. }
  69.  
  70. for (int j = ((NUM_LEDS_PER_STRIP/2)-1); j >= 0; j--)
  71. {
  72. leds[j] = startupcolordim;
  73. leds[(NUM_LEDS_PER_STRIP/2-1)+((NUM_LEDS_PER_STRIP/2)-j)] = startupcolordim;
  74. FastLED.show();
  75. delay (StartupSpeed);
  76. }
  77.  
  78. for (int j = ((NUM_LEDS_PER_STRIP/2)-1); j >= 0; j--)
  79. {
  80. leds[j] = startupcolorbright;
  81. leds[(NUM_LEDS_PER_STRIP/2-1)+((NUM_LEDS_PER_STRIP)/2-j)] = startupcolorbright;
  82. FastLED.show();
  83. delay (StartupSpeed);
  84. }
  85.  
  86. for (int j = 255; j >= 60; j--)
  87. {
  88. for (int i = 0; i < NUM_LEDS_PER_STRIP; i++)
  89. {
  90. leds[i] = CRGB(j, 0, 0);
  91. }
  92. FastLED.show();
  93. delay (5);
  94. }
  95.  
  96. FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS_PER_STRIP); //led strip front left for arduino
  97. //FastLED.addLeds<NEOPIXEL, 0>(leds[0], NUM_LEDS_PER_STRIP); //led strip front left for attiny85
  98.  
  99. 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
  100. FastLED.show();
  101.  
  102.  
  103.  
  104. }
  105. void loop() {
  106. // read the input state
  107. buttonStateTS = digitalRead(buttonPinTS);
  108.  
  109. if(buttonStateTS == HIGH){
  110. stateTS = 1;
  111. }
  112.  
  113. // turn signal function
  114. if(stateTS != 0){
  115.  
  116. for(int dot = NUM_LEDS_PER_STRIP/2; dot > 0; dot--) {
  117.  
  118. leds[dot] = turnColor;
  119.  
  120. //delay(delayTurnLedAnim);
  121. currentMillis = previousMillis = millis();
  122. while(previousMillis + delayTurnLedAnim >= currentMillis){
  123. stateTSRuns = 1;
  124. TailAndBrake();
  125. FastLED.show();
  126. currentMillis = millis();
  127. }
  128.  
  129. }
  130.  
  131. //delay(delayTurnLedOff);
  132. currentMillis = previousMillis = millis();
  133. while(previousMillis + delayTurnLedOff >= currentMillis){
  134. stateTSRuns = 1;
  135. TailAndBrake();
  136. FastLED.show();
  137. currentMillis = millis();
  138. }
  139.  
  140. fill_solid(leds, NUM_LEDS_PER_STRIP, CRGB::Black);
  141. stateTSRuns = 1;
  142. TailAndBrake();
  143. FastLED.show();
  144.  
  145. //delay(delayTurnLedOff);
  146. currentMillis = previousMillis = millis();
  147. while(previousMillis + delayTurnLedOff >= currentMillis){
  148. stateTSRuns = 1;
  149. TailAndBrake();
  150. FastLED.show();
  151. currentMillis = millis();
  152. }
  153.  
  154. buttonStateTS = digitalRead(buttonPinTS);
  155. if(buttonStateTS == HIGH){
  156. stateTS = 1;
  157. }
  158.  
  159. stateTS++;
  160. if(stateTS >= nrAnimAfterOff && buttonStateTS != HIGH){
  161. stateTS = 0;
  162. //delay(delayLedToDayLight);
  163. currentMillis = previousMillis = millis();
  164. while(previousMillis + delayLedToDayLight >= currentMillis){
  165. stateTSRuns = 1;
  166. TailAndBrake();
  167. FastLED.show();
  168. currentMillis = millis();
  169. }
  170. }
  171.  
  172. }else{
  173.  
  174. fill_solid(leds, NUM_LEDS_PER_STRIP, CRGB::Black);
  175.  
  176. TailAndBrake();
  177. FastLED.show();
  178. }
  179. }
  180.  
  181.  
  182. //function for Brake lights, tail lights and reverse lights
  183. void TailAndBrake(){
  184.  
  185. Break = digitalRead(buttonPinBreak);
  186. TailLights = digitalRead(buttonPinTailLights);
  187. ReverseLights = digitalRead(buttonPinReverseLights);
  188. Startup = digitalRead(buttonPinStartup);
  189.  
  190. if(TailLights == HIGH && Break == LOW && stateTSRuns == 0){
  191.  
  192. for(int i = 0; i <= sizeof(tailLightModel) / sizeof(tailLightModel[0]); i++){
  193. leds[tailLightModel[i]]=CHSV(0,254,maxBrtTailLights);
  194. }
  195.  
  196. }else if(Break == HIGH && stateTSRuns == 0){
  197.  
  198. fill_solid( leds, NUM_LEDS_PER_STRIP, CHSV(0,255,maxBrtBreak));
  199.  
  200. }else if(TailLights == HIGH && Break == LOW && stateTSRuns == 1){
  201.  
  202. int nrLedsTail = NUM_LEDS_PER_STRIP / 4;
  203. for(int i = 0; i <= nrLedsTail; i++){
  204. leds[i]=CHSV(0,254,maxBrtTailLights);
  205. }
  206.  
  207. }else if(Break == HIGH && stateTSRuns == 1){
  208.  
  209. int nrLedsBrake = NUM_LEDS_PER_STRIP / 4;
  210. for(int i = 0; i <= nrLedsBrake; i++){
  211. leds[i]=CHSV(0,254,maxBrtBreak);
  212. }
  213.  
  214. }
  215.  
  216. if(ReverseLights == HIGH){
  217. for(int i = 0; i < sizeof(reverseLightModel) / sizeof(reverseLightModel[0]); i++){
  218. leds[reverseLightModel[i]]=CHSV(0,0,maxBrtReverseLights);
  219. }
  220. }
  221. stateTSRuns = 0;
  222. Startup = digitalRead(buttonPinStartup);
  223.  
  224. if(buttonPinStartup == HIGH){
  225. StartupState = 1;
  226. }
  227.  
  228.  
  229. for (int i = 0; i < (NUM_LEDS_PER_STRIP/2); i++) { //Startup Animation. Having an issue here.
  230.  
  231.  
  232. leds[i] = startupcolordim;
  233. leds[i-1] = black;
  234. leds[(NUM_LEDS_PER_STRIP-1)-i] = startupcolordim;
  235. leds[(NUM_LEDS_PER_STRIP)-i] = black;
  236. FastLED.show();
  237. delay (StartupSpeed);
  238. }
  239.  
  240. for (int j = ((NUM_LEDS_PER_STRIP/2)-1); j >= 0; j--)
  241. {
  242. leds[j] = startupcolordim;
  243. leds[(NUM_LEDS_PER_STRIP/2-1)+((NUM_LEDS_PER_STRIP/2)-j)] = startupcolordim;
  244. FastLED.show();
  245. delay (StartupSpeed);
  246. }
  247.  
  248. for (int j = ((NUM_LEDS_PER_STRIP/2)-1); j >= 0; j--)
  249. {
  250. leds[j] = CRGB(255, 0, 0);
  251. leds[(NUM_LEDS_PER_STRIP/2-1)+((NUM_LEDS_PER_STRIP)-j)] = CRGB(255, 0, 0);
  252. FastLED.show();
  253. delay (StartupSpeed);
  254. }
  255.  
  256. for (int j = 255; j >= 60; j--)
  257. {
  258. for (int i = 0; i < NUM_LEDS_PER_STRIP; i++)
  259. {
  260. leds[i] = CRGB(j, 0, 0);
  261. }
  262. FastLED.show();
  263. delay (5);
  264. }
  265. }
  266.  
  267.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement