Advertisement
Bucher100

Arduino Uno - Corvette Bumper Controller

Jun 10th, 2020
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.39 KB | None | 0 0
  1. /*  
  2.  *    *** Corvette Bumper Controller ***
  3.  *       Firmware 0.9 beta June 2020
  4.  *             by Michael Buch
  5.  *            
  6.  *  // Description:
  7.  *  Control of LED light in Corvette bumper decoration in Mom's Diner Burger Joint
  8.  *  
  9.  *  The overall voltage is 12VDC 5A to feed the Arduino Uno controller and a total of 180 led's on strips
  10.  *  LED Strips are controlled via 4 shift registers
  11.  *  2x RGB is controlled by the 6 PWM pins
  12.  *  Just after this intro you can customize the 5 effects with the following commands
  13.  *  They are case sensitive!
  14.  *  
  15.  *  // Common effects:
  16.  *  AllOff();       // turns all lights off    
  17.  *  delay(5000);    // delay between effects 5000 = 5 sec.
  18.  *  delay(pottime); // delay set from the on board potentiometer 5 sec to 60 sec
  19.  *  
  20.  *  // Normal Car Effects
  21.  *  EngineOn();         //  Engine On effect (ends with Normal driveing light on)
  22.  *  EngineOff();        //  Engine Off effect (ends with All Off)
  23.  *  posistionlightOn(); //  Normal driveing light on
  24.  *  posistionlightOff();//  Normal driveing light off
  25.  *  breakOn();          //  Press break
  26.  *  breakOff();         //  Release break
  27.  *  TurnRight(10);      //  Turn Right 10 times = 10 sec
  28.  *  TurnLeft(5);        //  Turn Left 5 times = 5 sec
  29.  *  ThirdbreakOn();     //  turns third break light on
  30.  *  ThirdbreakOff();    //  turns third break light on
  31.  *  Hazard(10);        //  Hazzard light 10 times = 10 sec
  32.  *  
  33.  *  // Reverse lights:
  34.  *  revOn();            //  turn on reverse light (as white as RGB can be :)
  35.  *  RGBOff();           //  turn off both RGB/reverse lights  
  36.  *  setLeftColor(50,150,250);   //  Turn on Left reverse light in a RGB color (0,0,0) turns it off
  37.  *  setRightColor(250,150,50);  //  Turn on Right reverse light in a RGB color - Max (255,255,255) = white'ish.
  38.  *  
  39.  *  //SPECIAL EFFECTS the effects takes time before next effect is running
  40.  *  rainbow(3);                 //  Both RGB runs through 512 colors of the rainbow 3 times
  41.  *  USPolice(10);             //  Blue and Red police flash 10 times = 7 sec - Ends in RGB off
  42.  *  EUPolice(10);             //  Blue police flash 10 times = 7 sec - Ends in RGB off
  43.  *  USFire(10);               //  Red fire flash slow 10 times = 8 sec - Ends in RGB off
  44.  *  EUFire(10);               //  Blue fire flash slow 10 times = 8 sec - Ends in RGB off
  45.  *  FlashSidemarker(50);      //  Flash side markers 50 times - Ends in On state
  46.  *  Numberplatestrobe(50);    //  Numberplate Strobelight 50 times - Ends in On state
  47.  *  Redstrobe(5);             //  Red light under bunper Strobelight 5 times - Ends in On state
  48.  *  
  49.  *  // Third breaklight effects  (the veriable "speed" is uses as a random speed/delay between 30ms and 100ms)
  50.  *  FlashEffect1(speed, 3);    // speed of animation
  51.  *  FlashEffect2(speed, 3);    // speed of animation
  52.  *  FlashEffect3(50, 20);       // Kight Rider effect - speed(50 is good) and number of times to animate
  53.  *  FlashEffect4(speed, 3); // speed and times of animation
  54.  *  FlashEffect5(speed, 3);    // speed of animation
  55.  *
  56.  *  
  57. */  
  58.  
  59. // 1st User Effect (use commands from description)
  60. void effectA(int speed, int pottime){
  61.   /*STRAT USER COMMANDS*/
  62.   EngineOn();
  63.   delay(pottime);
  64.   Numberplatestrobe(50);
  65.   delay(pottime);
  66.   USFire(10);
  67.   delay(1000);
  68.   revOn();
  69.   TurnLeft(5);
  70.   delay(500);  
  71.   RGBOff();
  72.   TurnLeft(5);
  73.   delay(pottime);
  74.   ThirdbreakOn();
  75.   delay(10000);
  76.   ThirdbreakOff();
  77.   delay(pottime);
  78.   rainbow(5);
  79.   delay(pottime);
  80.   USPolice(20);
  81.   breakOn();
  82.   USPolice(10);
  83.   breakOff();
  84.   USPolice(10);
  85.   delay(pottime);
  86.   FlashEffect1(speed, 3);
  87.   delay(pottime);
  88.   Hazard(20);
  89.   delay(pottime);
  90.   FlashEffect2(speed, 3);
  91.   Redstrobe(50);
  92.   delay(pottime);
  93.   EngineOff();
  94.   delay(pottime); // Time until next effect starts
  95.   /*END USER COMMANDS*/
  96.   }
  97.  
  98.  
  99. // 2nd User Effect (use commands from description)
  100. void effectB(int speed, int pottime){
  101.   /*STRAT USER COMMANDS*/
  102.   EngineOn();
  103.   delay(5000);
  104.   breakOn();
  105.   delay(1500);
  106.   revOn();
  107.   delay(1500);
  108.   breakOff();
  109.   delay(10000);
  110.   breakOn();
  111.   delay(3000);
  112.   RGBOff();
  113.   delay(2000);
  114.   breakOff();
  115.   delay(pottime);
  116.   TurnLeft(2);
  117.   breakOn();
  118.   TurnLeft(10);
  119.   breakOff();
  120.   TurnLeft(5);
  121.   delay(pottime);
  122.   FlashEffect3(50, 20); //Night rider @ 50ms 10 times
  123.   delay(pottime);
  124.   breakOn();
  125.   delay(10000);
  126.   breakOff();
  127.   delay(pottime);
  128.   TurnRight(2);
  129.   breakOn();
  130.   TurnRight(10);
  131.   breakOff();
  132.   TurnRight(5);
  133.   delay(pottime);
  134.   rainbow(10);
  135.   delay(pottime);
  136.   FlashEffect4(speed, 10);
  137.   delay(pottime);
  138.   breakOn();
  139.   TurnRight(10);
  140.   delay(5000);
  141.   breakOff();
  142.   delay(5000);
  143.   rainbow(5);
  144.   delay(10000);
  145.   EngineOff();
  146.   delay(pottime); // Time until next effect starts
  147.   /*END USER COMMANDS*/
  148. }
  149.  
  150.  
  151. //  3rd User Effect (use commands from description)
  152. void effectC(int speed, int pottime){
  153.   /*STRAT USER COMMANDS*/
  154.   EngineOn();
  155.   delay(5000);
  156.   breakOn();
  157.   delay(2000);
  158.   TurnLeft(2);
  159.   breakOff();
  160.   TurnLeft(6);
  161.   delay(pottime);
  162.   USPolice(10);
  163.   breakOn();
  164.   USPolice(10);
  165.   breakOff();
  166.   USPolice(5);
  167.   delay(pottime);
  168.   TurnLeft(2);
  169.   breakOff();
  170.   TurnLeft(6);
  171.   delay(30000);
  172.   rainbow(5);
  173.   delay(30000);
  174.   FlashEffect1(speed, 10);
  175.   delay(pottime);
  176.   breakOn();
  177.   TurnRight(10);
  178.   delay(5000);
  179.   breakOff();
  180.   delay(pottime);
  181.   breakOn();
  182.   delay(7000);
  183.   revOn();
  184.   delay(2000);
  185.   breakOff();
  186.   delay(6000);
  187.   breakOn();
  188.   delay(1000);
  189.   RGBOff();
  190.   delay(1000);
  191.   breakOff();
  192.   TurnLeft(6);
  193.   EUPolice(20);
  194.   delay(pottime);
  195.   breakOn();
  196.   delay(10000);
  197.   breakOff();
  198.   delay(pottime);
  199.   Numberplatestrobe(70);
  200.   delay(pottime);
  201.   breakOn();
  202.   TurnRight(10);
  203.   breakOff();
  204.   TurnRight(2);
  205.   breakOn();
  206.   TurnRight(3);
  207.   breakOff();
  208.   delay(5000);
  209.   EngineOff();
  210.   delay(pottime); // Time until next effect starts
  211.   /*END USER COMMANDS*/
  212.   }
  213.  
  214.  
  215. //  4th User Effect (use commands from description)
  216. void effectD(int speed, int pottime){
  217.   /*STRAT USER COMMANDS*/
  218.   delay(5000);
  219.   FlashEffect3(50, 1); //Night rider @ 50ms 1 times  
  220.   delay(5000);
  221.   FlashEffect3(50, 1); //Night rider @ 50ms 1 times  
  222.   delay(5000);
  223.   Redstrobe(50);
  224.   FlashEffect3(50, 20); //Night rider @ 50ms 20 times  
  225.   EngineOn();
  226.   delay(pottime);
  227.   FlashSidemarker(50);
  228.   delay(pottime);
  229.   FlashEffect1(speed, 10);
  230.   delay(pottime);
  231.   FlashEffect2(speed, 10);
  232.   delay(pottime);
  233.   Hazard(20);
  234.   delay(3000);
  235.   EngineOff();
  236.   delay(pottime); // Time until next effect starts
  237.   /*END USER COMMANDS*/
  238.   }
  239.  
  240.  
  241. //  5th User Effect (use commands from description)
  242. void effectE(int speed, int pottime){
  243.   /*STRAT USER COMMANDS*/
  244.   EUPolice(20);
  245.   delay(5000);
  246.   EngineOn();
  247.   delay(pottime);
  248.   rainbow(5);
  249.   delay(pottime);
  250.   Hazard(20);
  251.   delay(pottime);
  252.   posistionlightOff();
  253.   delay(2000);
  254.   FlashSidemarker(50);
  255.   Numberplatestrobe(50);
  256.   Redstrobe(50);
  257.   delay(2000);
  258.   posistionlightOn();
  259.   delay(pottime);
  260.   EngineOff();
  261.   delay(pottime); // Time until next effect starts
  262.   /*END USER COMMANDS*/
  263.   }
  264.  
  265. /*  
  266.  *   WARNING!!
  267.  *    Below is the code to make the above work
  268.  *    change at own rick :)
  269.  */
  270.  
  271. /* START PROGRAM */
  272.  
  273. //  Define Constants and Pin Setup
  274. int AUX1 = 2; // Spare Output
  275. int LR = 3;  // PWM for Left side RGB led (Red)
  276. int AUX2 = 4; // Spare Output
  277. int LG = 5;  // PWM for Left side RGB led (Green)
  278. int LB = 6;  // PWM for Left side RGB led (Blue)
  279. int latchPin = 7;  // Latch pin of 74HC595 is connected to Digital pin 2
  280. int clockPin = 8; // Clock pin 11 of 74HC595 is connected to Digital pin 6
  281. int RR = 9;  // PWM for Right side RGB led (Red)
  282. int RG = 10;  // PWM for Right side RGB led (Green)
  283. int RB = 11;  // PWM for Right side RGB led (Blue)
  284. int dataPin = 12;  // Data pin 14 of 74HC595 is connected to Digital pin 5
  285. int statusled = 13;  // test/status led
  286. int pot = 0;  //  Potmeter on PCB for speed ajustment
  287. int numColors = 512;  // Used in the rainbow effect
  288.  
  289. // Define Veriables
  290. byte* registerState;    //  Byte to send
  291. int effectId = 0;    //  effect id for random effects
  292. int prevEffect = 0;  //  prev. effect id for random effects
  293. int effectRepeat = 0;  //  repeat for random effects
  294. int effectSpeed = 30;  //  speed in ms for random effects
  295. int potval = 0;     // on board pot used for delay time
  296. int LRed = 255;     // can be used for the left side red color
  297. int LGreen = 255;   // can be used for the left side green color
  298. int LBlue = 255;    // can be used for the left side blue color
  299. int RRed = 255;     // can be used for the right side red color
  300. int RGreen = 255;   // can be used for the right side green color
  301. int RBlue = 255;        // can be used for the right side blue color
  302.  
  303. int counter = 0;  // Used in the rainbow effect
  304. int animationDelay = 10; // // Used in the rainbow effect
  305.  
  306.  
  307. // Setup micro controller
  308. void setup() {
  309.    //Initialize byte array to send to shift registers
  310.   registerState = new byte[4];
  311.   for (size_t i = 0; i < 4; i++) {
  312.     registerState[i] = 0;
  313.   }
  314.  
  315.   //set pins to output to control the shift registers
  316.   pinMode(latchPin, OUTPUT);
  317.   pinMode(clockPin, OUTPUT);
  318.   pinMode(dataPin, OUTPUT);
  319.   //set pins to output pwm control the 2 x RGB lights
  320.   pinMode(LR, OUTPUT);
  321.   pinMode(LG, OUTPUT);
  322.   pinMode(LB, OUTPUT);
  323.   pinMode(RR, OUTPUT);
  324.   pinMode(RG, OUTPUT);
  325.   pinMode(RB, OUTPUT);
  326.   //set pins to output for Digital outs
  327.   pinMode(AUX1, OUTPUT);
  328.   pinMode(AUX2, OUTPUT);
  329.   // Start serial to display effect names running
  330.   Serial.begin(9600);
  331.   AllOff();
  332.   // Show credits and version
  333.   Serial.println("");
  334.   Serial.println("*** Covette Bumper Controller ***");
  335.   Serial.println("   Firmware 0.9 beta June 2020");
  336.   Serial.println("       by Michael Buch");
  337.   Serial.println("");
  338.   Serial.println(" <<< Program Running >>>");
  339.   Serial.println("");
  340. }
  341.  
  342.  
  343. // Start The Loop
  344. void loop(){
  345.   do{
  346.     effectId = random(1, 5);  // Let's find a random effect
  347.     }
  348.   while (effectId == prevEffect);
  349.   prevEffect = effectId;
  350.   switch (effectId){
  351.     case 1:
  352.     effectRepeat = random(1, 4);  // How many times to repeat effect 1
  353.     break;
  354.     case 2:
  355.     effectRepeat = random(1, 4);  // How many times to repeat effect 2
  356.     break;
  357.    case 3:
  358.     effectRepeat = random(1, 4);  // How many times to repeat effect 3
  359.     break;
  360.    case 4:
  361.     effectRepeat = random(1, 4);  // How many times to repeat effect 4
  362.     break;
  363.   case 5:
  364.     effectRepeat = random(1, 4);  // How many times to repeat effect 5
  365.     break;
  366.     }
  367.  
  368.   for (int i = 1; i <= effectRepeat; i++) {  // Repeat the effect
  369.        potval = analogRead(pot);
  370.     int pottime = map(potval, 0, 1023, 5000, 60000);
  371.     if (pottime < 0) {
  372.       pottime = pottime * (-1);
  373.           }
  374.     effectSpeed = random(10, 100);     // Random speed for some effects
  375.     switch (effectId){
  376.     case 1:
  377.       Serial.print("Effect A running: ");Serial.print(i);Serial.print(" of ");Serial.println(effectRepeat);
  378.       Serial.print("    Annimation delay  = ");Serial.println(effectSpeed);
  379.       Serial.print("    Effect delay      = ");Serial.println(pottime);
  380.       effectA(effectSpeed, pottime);
  381.       break;
  382.     case 2:
  383.       Serial.print("Effect B running: ");Serial.print(i);Serial.print(" of ");Serial.println(effectRepeat);
  384.       Serial.print("    Annimation delay  = ");Serial.println(effectSpeed);
  385.       Serial.print("    Effect delay      = ");Serial.println(pottime);
  386.       effectB(effectSpeed, pottime);
  387.       break;
  388.     case 3:
  389.       Serial.print("Effect C running: ");Serial.print(i);Serial.print(" of ");Serial.println(effectRepeat);
  390.       Serial.print("    Annimation delay  = ");Serial.println(effectSpeed);
  391.       Serial.print("    Effect delay      = ");Serial.println(pottime);
  392.       effectC(effectSpeed, pottime);
  393.       break;
  394.     case 4:
  395.       Serial.print("Effect D running: ");Serial.print(i);Serial.print(" of ");Serial.println(effectRepeat);
  396.       Serial.print("    Annimation delay  = ");Serial.println(effectSpeed);
  397.       Serial.print("    Effect delay      = ");Serial.println(pottime);
  398.       effectD(effectSpeed, pottime);
  399.       break;
  400.     case 5:
  401.       Serial.print("Effect E running: ");Serial.print(i);Serial.print(" of ");Serial.println(effectRepeat);
  402.       Serial.print("    Annimation delay  = ");Serial.println(effectSpeed);
  403.       Serial.print("    Effect delay      = ");Serial.println(pottime);
  404.       effectE(effectSpeed, pottime);
  405.       break;
  406.       }
  407.     }
  408.   }
  409.  
  410. //  Write to the shift register
  411. void regWrite(int pin, bool state){
  412.   //Determines register
  413.   int reg = pin / 8;
  414.   //Determines pin for actual register
  415.   int actualPin = pin - (8 * reg);
  416.   //Begin session
  417.   digitalWrite(latchPin, LOW);
  418.   for (int i = 0; i < 4; i++){
  419.     //Get actual states for register
  420.     byte* states = &registerState[i];
  421.     //Update state
  422.     if (i == reg){
  423.       bitWrite(*states, actualPin, state);
  424.       }
  425.     //Write
  426.     shiftOut(dataPin, clockPin, LSBFIRST, *states);
  427.     }
  428.   //End session
  429.   digitalWrite(latchPin, HIGH);
  430.   }
  431.  
  432.  
  433. /* START EFFECTS*/
  434.  
  435. //  All off
  436. void AllOff(){
  437.       for (int k = 0; k <= 31; k++){
  438.       regWrite(k, LOW);
  439.       }
  440.       RGBOff();
  441.   }
  442.  
  443. //  Third break light off
  444. void ThirdbreakOff(){
  445.   for (int i = 23; i >= 8; i--){
  446.      regWrite(i, LOW);
  447.   }
  448. }
  449.  
  450. //  Third break light on
  451. void ThirdbreakOn(){
  452.   for (int i = 23; i >= 8; i--){
  453.      regWrite(i, HIGH);
  454.    }
  455. }
  456.  
  457. //  Posistion light on
  458. void posistionlightOn(){
  459. regWrite(0, HIGH);
  460. regWrite(2, HIGH);
  461. regWrite(5, HIGH);
  462. regWrite(7, HIGH);
  463. regWrite(24, HIGH);
  464. regWrite(26, HIGH);
  465. regWrite(29, HIGH);
  466. regWrite(31, HIGH);
  467. }
  468.  
  469. //  Posistion light off
  470. void posistionlightOff(){
  471. regWrite(0, LOW);
  472. regWrite(2, LOW);
  473. regWrite(5, LOW);
  474. regWrite(7, LOW);
  475. regWrite(24, LOW);
  476. regWrite(26, LOW);
  477. regWrite(29, LOW);
  478. regWrite(31, LOW);
  479. }
  480.  
  481. //  Break light on
  482. void breakOn(){
  483. regWrite(4, HIGH);
  484. regWrite(6, HIGH);
  485. regWrite(25, HIGH);
  486. regWrite(27, HIGH);
  487. ThirdbreakOn();
  488. }
  489.  
  490. //  Break light off
  491. void breakOff(){
  492. regWrite(4, LOW);
  493. regWrite(6, LOW);
  494. regWrite(25, LOW);
  495. regWrite(27, LOW);
  496. ThirdbreakOff();
  497. }
  498.  
  499. //  Reverse on
  500. void revOn(){
  501.  setLeftColor(255,255,255);
  502.  setRightColor(255,255,255);
  503. }
  504.  
  505. //  RGB off
  506. void RGBOff(){
  507. setLeftColor(0,0,0);
  508.  setRightColor(0,0,0);
  509. }
  510.  
  511. //  Turn Left
  512. void TurnLeft(int times){
  513.    for (int i = 0; i <= times; i++){
  514.     regWrite(30, HIGH);
  515.     regWrite(28, HIGH);
  516.     delay(500);
  517.     regWrite(30, LOW);
  518.     regWrite(28, LOW);
  519.      delay(500);
  520.    }
  521. }
  522.  
  523.  
  524. //  Turn Right
  525. void TurnRight(int times){
  526.    for (int i = 0; i <= times; i++){
  527.     regWrite(1, HIGH);
  528.     regWrite(3, HIGH);
  529.     delay(500);
  530.     regWrite(1, LOW);
  531.     regWrite(3, LOW);
  532.      delay(500);
  533.    }
  534. }
  535.  
  536. //  Hazard Light
  537. void Hazard(int times){
  538.    for (int i = 0; i <= times; i++){
  539.     regWrite(1, HIGH);
  540.     regWrite(3, HIGH);
  541.     regWrite(30, HIGH);
  542.     regWrite(28, HIGH);
  543.     delay(500);
  544.     regWrite(30, LOW);
  545.     regWrite(28, LOW);
  546.     regWrite(1, LOW);
  547.     regWrite(3, LOW);
  548.      delay(500);
  549.    }
  550. }
  551.  
  552. //  Engine on (start up)
  553. void EngineOn(){
  554.   regWrite(3, HIGH);
  555.   regWrite(6, HIGH);
  556.   regWrite(25, HIGH);
  557.   regWrite(28, HIGH);
  558.   delay(500);
  559.   regWrite(2, HIGH);
  560.   regWrite(5, HIGH);
  561.   regWrite(26, HIGH);
  562.   regWrite(29, HIGH);
  563.   delay(500);
  564.   regWrite(1, HIGH);
  565.   regWrite(4, HIGH);
  566.   regWrite(27, HIGH);
  567.   regWrite(30, HIGH);
  568.   delay(300);
  569.   FlashEffect4(30, 1);
  570.   regWrite(3, LOW);
  571.   regWrite(6, LOW);
  572.   regWrite(25, LOW);
  573.   regWrite(28, LOW);
  574.   regWrite(2, LOW);
  575.   regWrite(5, LOW);
  576.   regWrite(26, LOW);
  577.   regWrite(29, LOW);
  578.   regWrite(1, LOW);
  579.   regWrite(4, LOW);
  580.   regWrite(27, LOW);
  581.   regWrite(30, LOW);
  582.   delay(400);
  583.   posistionlightOn();
  584. }
  585.  
  586. //  Engine off (shot down)
  587. void EngineOff(){
  588.   AllOff();
  589.   ThirdbreakOn();
  590.   regWrite(3, HIGH);
  591.   regWrite(6, HIGH);
  592.   regWrite(25, HIGH);
  593.   regWrite(28, HIGH);
  594.   regWrite(2, HIGH);
  595.   regWrite(5, HIGH);
  596.   regWrite(26, HIGH);
  597.   regWrite(29, HIGH);
  598.   regWrite(1, HIGH);
  599.   regWrite(4, HIGH);
  600.   regWrite(27, HIGH);
  601.   regWrite(30, HIGH);
  602.   delay(300);
  603.   for (int k = 16; k <= 23; k++){
  604.     delay(100);
  605.     regWrite(k, LOW);
  606.     regWrite(31 - k, LOW);
  607.   }
  608.   delay(300);
  609.   regWrite(1, LOW);
  610.   regWrite(4, LOW);
  611.   regWrite(27, LOW);
  612.   regWrite(30, LOW);
  613.   delay(500);
  614.   regWrite(2, LOW);
  615.   regWrite(5, LOW);
  616.   regWrite(26, LOW);
  617.   regWrite(29, LOW);
  618.   delay(500);
  619.   regWrite(3, LOW);
  620.   regWrite(6, LOW);
  621.   regWrite(25, LOW);
  622.   regWrite(28, LOW);
  623.   AllOff();
  624. }
  625.  
  626. //  Numberplate strobe Light
  627. void Numberplatestrobe(int strobe){
  628.   for (int i = 0; i <= strobe; i++){
  629.     regWrite(7, LOW);
  630.     delay(30);
  631.     regWrite(7, HIGH);
  632.     delay(30);
  633.   }
  634. }
  635.  
  636. //  Red strobe Light
  637. void Redstrobe(int strobe){
  638.   for (int i = 0; i <= strobe; i++){
  639.     regWrite(24, LOW);
  640.     delay(30);
  641.     regWrite(24, HIGH);
  642.     delay(30);
  643.   }
  644. }
  645.  
  646. //  Flash side marker lights
  647. void FlashSidemarker(int times){
  648.   for (int i = 0; i <= times; i++){
  649.     regWrite(0, LOW);
  650.     regWrite(31, LOW);
  651.     delay(400);
  652.     regWrite(0, HIGH);
  653.     regWrite(31, HIGH);
  654.     delay(50);
  655.     regWrite(0, LOW);
  656.     regWrite(31, LOW);
  657.     delay(50);
  658.     regWrite(0, HIGH);
  659.     regWrite(31, HIGH);
  660.     delay(50);
  661.   }
  662. }
  663.  
  664. //  US Police effect
  665. void USPolice(int times){
  666.   RGBOff();
  667.   for (int i = 0; i <= times; i++){
  668.     setRightColor(255,0,0);
  669.     delay(100);
  670.     setRightColor(0,0,0);
  671.     delay(100);
  672.     setRightColor(255,0,0);
  673.     delay(100);
  674.     setRightColor(0,0,0);
  675.     delay(100);
  676.     setLeftColor(0,0,255);
  677.     delay(100);
  678.     analogWrite(LB, 0);
  679.     delay(100);
  680.     analogWrite(LB, 255);
  681.     delay(100);
  682.     analogWrite(LB, 0);
  683.   }
  684. }
  685.  
  686. //  US Fire effect
  687. void USFire(int times){
  688.   RGBOff();
  689.   for (int i = 0; i <= times; i++){
  690.     analogWrite(RR, 255);
  691.     analogWrite(LR, 0);
  692.     delay(400);
  693.     analogWrite(LR, 255);
  694.     analogWrite(RR, 0);
  695.     delay(400);
  696.   }
  697.    RGBOff();
  698. }
  699.  
  700. //  EU Police effect
  701. void EUPolice(int times){
  702.   RGBOff();
  703.   for (int i = 0; i <= times; i++){
  704.     analogWrite(RB, 255);
  705.     delay(100);
  706.     analogWrite(RB, 0);
  707.     delay(100);
  708.     analogWrite(RB, 255);
  709.     delay(100);
  710.     analogWrite(RB, 0);
  711.     delay(100);
  712.     analogWrite(LB, 255);
  713.     delay(100);
  714.     analogWrite(LB, 0);
  715.     delay(100);
  716.     analogWrite(LB, 255);
  717.     delay(100);
  718.     analogWrite(LB, 0);
  719.   }
  720. }
  721.  
  722. //  EU Fire effect
  723. void EUFire(int times){
  724.   RGBOff();
  725.   for (int i = 0; i <= times; i++){
  726.     analogWrite(RB, 255);
  727.     analogWrite(LB, 0);
  728.     delay(400);
  729.     analogWrite(LB, 255);
  730.     analogWrite(RB, 0);
  731.     delay(400);
  732.   }
  733.   RGBOff();
  734. }
  735.  
  736. //    RGB rainbow
  737. void rainbow(int repeat){
  738.   for(int r = 0; r <= repeat; r++){
  739.     for(int i = 0; i <= numColors; i++){
  740.       float colorNumber = counter > numColors ? counter - numColors: counter;
  741.       float saturation = 1; // Between 0 and 1 (0 = gray, 1 = full color)
  742.       float brightness = 1; // Between 0 and 1 (0 = dark, 1 is full brightness)
  743.       float hue = (colorNumber / float(numColors)) * 360; // Number between 0 and 360
  744.       long color = HSBtoRGB(hue, saturation, brightness);
  745.       int red = color >> 16 & 255;
  746.       int green = color >> 8 & 255;
  747.       int blue = color & 255;
  748.       setLeftColor(red, green, blue);
  749.       setRightColor(red, green, blue);
  750.       counter = (counter + 1) % (numColors * 2);
  751.       delay(animationDelay);
  752.     }
  753.   }
  754. RGBOff();
  755. }
  756.  
  757. // 1st Flash Effect
  758. void FlashEffect1(int speed, int repeat){
  759.   for (int r = 1; r <= repeat; r++){
  760.     for (int i = 8; i < 24; i++){
  761.       for (int k = i; k < 24; k++){
  762.         regWrite(k, HIGH);
  763.         delay(speed);
  764.         regWrite(k, LOW);
  765.       }
  766.     regWrite(i, HIGH);
  767.     }
  768.   delay(speed * 5);
  769.   for (int i = 23; i >= 8; i--){
  770.     regWrite(i, LOW);
  771.   }
  772.   }
  773. delay(speed * 5);
  774. ThirdbreakOff();
  775. }
  776.  
  777. // 2nd Flash Effect
  778. void FlashEffect2(int speed, int repeat){
  779.   for (int r = 1; r <= repeat; r++){
  780.     for (int i = 23; i >= 8; i--){
  781.       for (int k = 8; k < i; k++){
  782.         regWrite(k, HIGH);
  783.         delay(speed);
  784.         regWrite(k, LOW);
  785.       }
  786.       regWrite(i, HIGH);
  787.      }
  788.    delay(speed * 5);
  789.    for (int i = 23; i >= 8; i--){
  790.     regWrite(i, LOW);
  791.    }
  792.    }
  793. delay(speed * 5);
  794. ThirdbreakOff();
  795. }
  796.  
  797. //  3rd Flash Effect (knight rider)
  798. void FlashEffect3(int speed, int repeat){
  799.   for (int r = 1; r <= repeat; r++){
  800.     int prevI = 8;
  801.     for (int i = 8; i < 24; i++){
  802.       regWrite(prevI, LOW);
  803.       regWrite(i, HIGH);
  804.       prevI = i;
  805.       delay(speed);
  806.     }
  807.     for (int i = 23; i >= 8; i--){
  808.       regWrite(prevI, LOW);
  809.       regWrite(i, HIGH);
  810.       prevI = i;
  811.       delay(speed);
  812.     }
  813.     //delay(speed * 5);
  814.     for (int i = 23; i >= 8; i--){
  815.       regWrite(i, LOW);
  816.     }
  817.   }
  818. delay(1000);
  819. ThirdbreakOff();
  820. }
  821.  
  822.  
  823. //  4th Flash Effect
  824. void FlashEffect4(int speed, int repeat){
  825.   for (int r = 1; r <= repeat; r++){
  826.     for (int i = 8; i < 16; i++){
  827.       for (int k = i; k < 16; k++){
  828.         regWrite(k, HIGH);
  829.         regWrite(31 - k, HIGH);
  830.         delay(speed);
  831.         regWrite(k, LOW);
  832.         regWrite(31 - k, LOW);
  833.       }
  834.       regWrite(i, HIGH);
  835.       regWrite(31 - i, HIGH);
  836.     }
  837.   delay(speed * 5);
  838.   for (int i = 23; i >= 8; i--){
  839.     regWrite(i, LOW);
  840.   }
  841.   }
  842. delay(speed * 5);
  843. ThirdbreakOff();
  844. }
  845.  
  846.  
  847. //  5th Flash Effect
  848. void FlashEffect5(int speed, int repeat){
  849.   for (int r = 1; r <= repeat; r++){
  850.     for (int i = 23; i >= 16; i--){
  851.       for (int k = 16; k <= i; k++){
  852.         regWrite(k, HIGH);
  853.         regWrite(31 - k, HIGH);
  854.         delay(speed);
  855.         regWrite(k, LOW);
  856.         regWrite(31 - k, LOW);
  857.       }
  858.       regWrite(i, HIGH);
  859.       regWrite(31 - i, HIGH);
  860.     }
  861.     delay(speed * 5);
  862.    
  863.    }
  864.     delay(speed * 5);
  865.     ThirdbreakOff();
  866. }
  867.  
  868. // Set the color of the left reverse light
  869. void setLeftColor (unsigned char red, unsigned char green, unsigned char blue){      
  870.   analogWrite(LR, red);
  871.   analogWrite(LG, green);
  872.   analogWrite(LB, blue);
  873. }
  874.  
  875. // Set the color of the left reverse light
  876. void setRightColor (unsigned char red, unsigned char green, unsigned char blue){
  877.   analogWrite(RR, red);
  878.   analogWrite(RG, green);
  879.   analogWrite(RB, blue);
  880. }
  881.  
  882. // Convert Hue, Sat and brightness to RGB - Used in rainbow effect
  883. long HSBtoRGB(float _hue, float _sat, float _brightness) {
  884.    float red = 0.0;
  885.    float green = 0.0;
  886.    float blue = 0.0;
  887.    if (_sat == 0.0) {
  888.     red = _brightness;
  889.     green = _brightness;
  890.     blue = _brightness;
  891.    } else {
  892.     if (_hue == 360.0) {
  893.       _hue = 0;
  894.     }
  895.     int slice = _hue / 60.0;
  896.     float hue_frac = (_hue / 60.0) - slice;
  897.     float aa = _brightness * (1.0 - _sat);
  898.     float bb = _brightness * (1.0 - _sat * hue_frac);
  899.     float cc = _brightness * (1.0 - _sat * (1.0 - hue_frac));
  900.     switch(slice) {
  901.       case 0:
  902.         red = _brightness;
  903.         green = cc;
  904.         blue = aa;
  905.         break;
  906.       case 1:
  907.         red = bb;
  908.         green = _brightness;
  909.         blue = aa;
  910.         break;
  911.       case 2:
  912.         red = aa;
  913.         green = _brightness;
  914.         blue = cc;
  915.         break;
  916.       case 3:
  917.         red = aa;
  918.         green = bb;
  919.         blue = _brightness;
  920.         break;
  921.       case 4:
  922.         red = cc;
  923.         green = aa;
  924.         blue = _brightness;
  925.         break;
  926.       case 5:
  927.         red = _brightness;
  928.         green = aa;
  929.         blue = bb;
  930.         break;
  931.         default:
  932.         red = 0.0;
  933.         green = 0.0;
  934.         blue = 0.0;
  935.         break;
  936.     }
  937.   }
  938.   long ired = red * 255.0;
  939.   long igreen = green * 255.0;
  940.   long iblue = blue * 255.0;
  941.   return long((ired << 16) | (igreen << 8) | (iblue));
  942. }
  943.  
  944. /* END PROGRAM */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement