Advertisement
jetx9300

Wrexus Troubleshooting - Nano 3

Jan 8th, 2022
1,442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 38.37 KB | None | 0 0
  1. /*
  2. ********************************************
  3. *                                          *
  4. *        Project: Wrexus Carduino Controls *
  5. *          Board: Nano 3                   *
  6. * Devices Served: Side RGB Light Bars      *
  7. *                 Side Flood Lights        *
  8. *                 Chase Lights             *
  9. *        Version: 0.001                    *
  10. *   Last Updated: 2021.12.29               *
  11. *                                          *
  12. ********************************************
  13. */
  14.  
  15. // Include libraries
  16. #include <Adafruit_NeoPixel.h>
  17. #include <Wire.h>
  18.  
  19. // Definitions
  20. #define SIDE_LIGHT_FRONT_LEFT_DATA_PIN 12
  21. #define SIDE_LIGHT_REAR_LEFT_DATA_PIN 11
  22. #define SIDE_LIGHT_FRONT_RIGHT_DATA_PIN 10
  23. #define SIDE_LIGHT_REAR_RIGHT_DATA_PIN 8
  24. #define SIDE_LIGHT_NUM_LEDS 13
  25. #define RGB_LIGHTS_RELAY_PIN 7
  26. #define LEFT_FLOOD_LIGHTS_RELAY_PIN 6
  27. #define RIGHT_FLOOD_LIGHTS_RELAY_PIN 5
  28. #define LEFT_CHASE_LIGHT_RELAY_PIN 4
  29. #define RIGHT_CHASE_LIGHT_RELAY_PIN 3
  30. #define RGB_LIGHTS_INITIAL_BRIGHNESS 255
  31. #define PATTERN_CYCLE_TIME 5000
  32. #define FLASH_ON_TIME_SETTING 400
  33. #define FLASH_OFF_TIME_SETTING 20
  34. #define I2C_MESSAGE_RECEIVED_LED_FLASH_LENGTH 250 // The milliseconds time that the light will flash for
  35.  
  36. // I2C Variables
  37. int i2c_pattern;            // data received from I2C bus
  38. int i2c_option;             // last data received from I2C bus
  39. unsigned long i2c_message_LED_flash_start_timer;  // start time in milliseconds for flash
  40. int i2c_message_LED_status;               // status of LED: 1 = ON, 0 = OFF
  41.  
  42. // Currenly running pattern variables
  43. int currentPattern;
  44. int currentOption;
  45.  
  46. // Pattern Variables
  47. unsigned long lastPatternChange = millis();
  48. byte currentLightPattern = 2;
  49. byte randomPatternFlair = random(1,3);
  50.  
  51. // Setup Strips
  52. Adafruit_NeoPixel sideLightFrontLeftStrip = Adafruit_NeoPixel(SIDE_LIGHT_NUM_LEDS, SIDE_LIGHT_FRONT_LEFT_DATA_PIN, NEO_GRB + NEO_KHZ800);
  53. Adafruit_NeoPixel sideLightRearLeftStrip = Adafruit_NeoPixel(SIDE_LIGHT_NUM_LEDS, SIDE_LIGHT_REAR_LEFT_DATA_PIN, NEO_GRB + NEO_KHZ800);
  54. Adafruit_NeoPixel sideLightFrontRightStrip = Adafruit_NeoPixel(SIDE_LIGHT_NUM_LEDS, SIDE_LIGHT_FRONT_RIGHT_DATA_PIN, NEO_GRB + NEO_KHZ800);
  55. Adafruit_NeoPixel sideLightRearRightStrip = Adafruit_NeoPixel(SIDE_LIGHT_NUM_LEDS, SIDE_LIGHT_REAR_RIGHT_DATA_PIN, NEO_GRB + NEO_KHZ800);
  56.  
  57.  
  58. // Define named colors
  59. //uint32_t hudColor = strip.Color(0, 0, 0, 255); // Current hud color
  60. uint32_t white = sideLightFrontLeftStrip.Color(255,255,255); // Daytime indicator hud state
  61. uint32_t red = sideLightFrontLeftStrip.Color(255,0,0); // Nightime indicator hud state
  62. uint32_t green = sideLightFrontLeftStrip.Color(0,255,0); // On indicator state
  63. uint32_t blue = sideLightFrontLeftStrip.Color(0,0,255);
  64. uint32_t orange = sideLightFrontLeftStrip.Color(255,80,0); // Auto indicator state
  65. uint32_t yellow = sideLightFrontLeftStrip.Color(255,180,10);
  66. uint32_t purple = sideLightFrontLeftStrip.Color(128,0,128);
  67. uint32_t off = sideLightFrontLeftStrip.Color(0,0,0);
  68.  
  69. // Build Classes
  70. class RGBLightBar {
  71.   private:
  72.  
  73.     // Stip that will be associated with this Light Bar
  74.     Adafruit_NeoPixel neopixelStrip;
  75.     // Variables
  76.     byte lastLedAddress;
  77.     byte currentCautionPatternCall;
  78.     byte currentPattern;
  79.     byte currentState;
  80.     byte numOfPatternCycles;
  81.     unsigned long lastUpdateTime;
  82.     int flashOnTime;
  83.     byte flashCount;
  84.     long firstPixelHue;
  85.  
  86.     // Light segment calculations
  87.     byte lowerRightCorner(){
  88.       return 2;
  89.     }
  90.  
  91.     byte upperLeftCorner(){
  92.       byte toReturn = (lastLedAddress / 2) + 2;
  93.       return toReturn;
  94.     }
  95.  
  96.     byte width(){
  97.       byte toReturn = lastLedAddress - upperLeftCorner();
  98.       return toReturn;
  99.     }
  100.  
  101.     byte lowerMidPoint(){
  102.       byte toReturn = (width() / 2) + 2;
  103.       return toReturn;
  104.     }
  105.  
  106.     byte upperMidPoint(){
  107.       byte toReturn = upperLeftCorner() + (width() / 2);
  108.       return toReturn;
  109.     }
  110.  
  111.     byte halfWidth(){
  112.       byte toReturn;
  113.  
  114.       if(lastLedAddress == 12){
  115.         toReturn = (width() / 2);
  116.       } else {
  117.         toReturn = (width() / 2) + 1;
  118.       }
  119.  
  120.       return toReturn;
  121.     }
  122.  
  123.     byte leftWrap(){
  124.       byte toReturn;
  125.  
  126.       if(lastLedAddress == 12){
  127.         toReturn = upperMidPoint() - lowerMidPoint();
  128.       } else {
  129.         toReturn = upperMidPoint() - lowerMidPoint() + 1;
  130.       }
  131.  
  132.       return toReturn;
  133.     }
  134.  
  135.     byte rightWraps(){
  136.       byte toReturn;
  137.  
  138.       if(lastLedAddress == 12){
  139.         toReturn = (upperMidPoint() - lowerMidPoint()) / 2;
  140.       } else {
  141.         toReturn = (upperMidPoint() - lowerMidPoint()) / 2 + 1;
  142.       }
  143.  
  144.       return toReturn;
  145.     }
  146.  
  147.   public:
  148.  
  149.     // Constructor
  150.     RGBLightBar(byte lastLedAddress, Adafruit_NeoPixel &neopixelStrip){
  151.       this->lastLedAddress = lastLedAddress;
  152.       this->neopixelStrip = neopixelStrip;
  153.     }
  154.  
  155.     // Methods
  156.     void mainLightOn(){
  157.       neopixelStrip.setPixelColor(0,red);
  158.     }
  159.  
  160.     void mainLightOff(){
  161.       neopixelStrip.setPixelColor(0,green);
  162.     }
  163.  
  164.     void solidColor(uint32_t color){
  165.       neopixelStrip.fill(color,1,lastLedAddress);
  166.     }
  167.  
  168.     void cautionPatternCycle(){
  169.  
  170.       if(currentCautionPatternCall < 2 or currentCautionPatternCall > 5){
  171.         currentCautionPatternCall = 2;
  172.       }
  173.  
  174.       if(currentCautionPatternCall == 2){
  175.         if(flashFull(randomPatternFlair,yellow,white,yellow,5)){
  176.           currentCautionPatternCall ++;
  177.           randomPatternFlair = random(1,4);
  178.         }
  179.       } else if(currentCautionPatternCall == 3){
  180.         if(upAndDown(randomPatternFlair,yellow,white,yellow,5)){
  181.           currentCautionPatternCall ++;
  182.           randomPatternFlair = random(1,4);
  183.         }
  184.       } else if(currentCautionPatternCall == 4){
  185.         if(leftAndRight(randomPatternFlair,yellow,white,yellow,5)){
  186.           currentCautionPatternCall ++;
  187.           randomPatternFlair = random(1,4);
  188.         }
  189.       } else if(currentCautionPatternCall == 5){
  190.         if(crissCross(randomPatternFlair,yellow,white,yellow,5)){
  191.           currentCautionPatternCall ++;
  192.           randomPatternFlair = random(1,4);
  193.         }
  194.       }
  195.     }
  196.  
  197.     // Rainbow cycle along whole strip. Pass speed 0 = Full Speed | 1 = Fast | 2 = Moderate | 3 = Slow
  198.     void rainbow(int wait) {
  199.  
  200.       // Check if this pattern has just been activated - Pattern #4
  201.       if(currentPattern != 1){
  202.         currentPattern = 1; // Set to new current patern
  203.         firstPixelHue = 0; // Reset state to initial value
  204.         lastUpdateTime = millis() + wait * 10; // Set lastUpdateTime so code will run first time
  205.       }
  206.  
  207.       if(millis() > lastUpdateTime + wait * 10){
  208.         // Reset pixel hue if we have gone over the amount
  209.         if(firstPixelHue > 5*65536){
  210.           firstPixelHue = 0;
  211.         }
  212.  
  213.         // Fill light with the current ranbow patern
  214.         for(int i=1; i<neopixelStrip.numPixels(); i++) { // For each pixel in strip...
  215.           // Offset pixel hue by an amount to make one full revolution of the
  216.           // color wheel (range of 65536) along the length of the strip
  217.           // (strip.numPixels() steps):
  218.           int pixelHue = firstPixelHue + (i * 65536L / neopixelStrip.numPixels());
  219.           // neopixelStrip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  220.           // optionally add saturation and value (brightness) (each 0 to 255).
  221.           // Here we're using just the single-argument hue variant. The result
  222.           // is passed through neopixelStrip.gamma32() to provide 'truer' colors
  223.           // before assigning to each pixel:
  224.           neopixelStrip.setPixelColor(i, neopixelStrip.gamma32(neopixelStrip.ColorHSV(pixelHue)));
  225.         }
  226.  
  227.         // Incrament Hue value
  228.         firstPixelHue += 256;
  229.  
  230.         // Set last update time
  231.         lastUpdateTime = millis();
  232.       }
  233.     }
  234.  
  235.     // Flash whole strip at same time
  236.     boolean flashFull(byte multiFlash, uint32_t colorOne, uint32_t colorTwo = off, uint32_t colorThree = off, byte patternCyclesToRun = 1){
  237.  
  238.       //Definitions
  239.       //#define FLASH_ON_TIME_SETTING 900
  240.       //#define FLASH_OFF_TIME_SETTING 50
  241.  
  242.       // Caculate actual flash time
  243.       if(multiFlash > 1){
  244.         flashOnTime = (FLASH_ON_TIME_SETTING / multiFlash) - (FLASH_OFF_TIME_SETTING * multiFlash);
  245.       } else {
  246.         flashOnTime = FLASH_ON_TIME_SETTING;
  247.       }
  248.  
  249.  
  250.       // Check if this pattern has just been activated - Pattern #2
  251.       if(currentPattern != 2){
  252.         currentPattern = 2; // Set to new current patern
  253.         currentState = 0; // Reset state to initial value
  254.         numOfPatternCycles = 0; // Reset number of pattern cycles
  255.       }
  256.  
  257.       switch(currentState){
  258.         case 0: // Reset to initial ON state
  259.           neopixelStrip.fill(colorOne,1,lastLedAddress); // Set the color
  260.           lastUpdateTime = millis(); // Mark the update time
  261.           currentState = 1; // Update State
  262.           flashCount = 0; //update flash count
  263.           break;
  264.  
  265.         case 1: // Change to OFF state
  266.           if(millis() >= lastUpdateTime + flashOnTime){
  267.             neopixelStrip.fill(off,1,lastLedAddress); // Turn off strip
  268.             lastUpdateTime = millis(); // Mark the update time
  269.             currentState = 2; // Update State
  270.             flashCount ++; // Incrament flash count
  271.           }
  272.           break;
  273.  
  274.         case 2: // Loop back to ON state
  275.           if(flashCount == multiFlash){
  276.             // Wait for full time if we have flashed the proper amount of times.
  277.             if(millis() >= lastUpdateTime + FLASH_ON_TIME_SETTING){
  278.               neopixelStrip.fill(colorOne,1,lastLedAddress); // Set the color
  279.               lastUpdateTime = millis(); // Mark the update time
  280.               currentState = 1; // Update State
  281.               flashCount = 0; //update flash count
  282.               numOfPatternCycles ++; // Incrament pattern cycle count
  283.             }
  284.           } else {
  285.             if(millis() >= lastUpdateTime + FLASH_OFF_TIME_SETTING){
  286.               if(flashCount == 1 and colorTwo != off){
  287.                 neopixelStrip.fill(colorTwo,1,lastLedAddress); // Set the color
  288.               } else if(flashCount == 2 and colorThree != off){
  289.                 neopixelStrip.fill(colorThree,1,lastLedAddress); // Set the color
  290.               } else {
  291.                 neopixelStrip.fill(colorOne,1,lastLedAddress); // Set the color
  292.               }
  293.               lastUpdateTime = millis(); // Mark the update time
  294.               currentState = 1; // Update State
  295.             }
  296.           }
  297.           break;
  298.       }
  299.  
  300.       if(numOfPatternCycles == patternCyclesToRun){
  301.         return true; // Return True when we have run the requested amount of pattern cycles
  302.       } else {
  303.         return false;
  304.       }
  305.     }
  306.  
  307.     // Flash top strip then bottom strip
  308.     boolean upAndDown(byte multiFlash, uint32_t colorOne, uint32_t colorTwo = off, uint32_t colorThree = off, byte patternCyclesToRun = 1){
  309.  
  310.       //Definitions
  311.       //#define FLASH_ON_TIME_SETTING 900
  312.       //#define FLASH_OFF_TIME_SETTING 50
  313.  
  314.       // Caculate actual flash time
  315.       if(multiFlash > 1){
  316.         flashOnTime = (FLASH_ON_TIME_SETTING / multiFlash) - (FLASH_OFF_TIME_SETTING * multiFlash);
  317.       } else {
  318.         flashOnTime = FLASH_ON_TIME_SETTING;
  319.       }
  320.  
  321.       // Check if this pattern has just been activated - Pattern #3
  322.       if(currentPattern != 3){
  323.         currentPattern = 3; // Set to new current patern
  324.         currentState = 0; // Reset state to initial value
  325.         numOfPatternCycles = 0; // Reset number of pattern cycles
  326.       }
  327.  
  328.       switch(currentState){
  329.  
  330.         case 0: // Reset to initial UP ON state
  331.           neopixelStrip.fill(off,1,lastLedAddress); // clear all values
  332.           neopixelStrip.fill(colorOne,upperLeftCorner(), width()) ; // turn on upper light
  333.           lastUpdateTime = millis(); // Mark the update time
  334.           currentState = 1; // Update State
  335.           flashCount = 0; //update flash count
  336.           break;
  337.  
  338.         case 1: // Change to either UP OFF or DOWN ON state
  339.           if(millis() > lastUpdateTime + flashOnTime){
  340.             flashCount ++; // Incrament flash count
  341.             if(multiFlash > 1 and multiFlash != flashCount){
  342.               neopixelStrip.fill(off,upperLeftCorner(),width()); // turn off upper light
  343.               currentState = 2; // Update State
  344.             } else {
  345.               neopixelStrip.fill(colorOne,lowerRightCorner(),width()); // Set lowwer color
  346.               neopixelStrip.fill(off,upperLeftCorner(),width()); // Set Upper color
  347.               currentState = 3; // Update State
  348.               flashCount = 0; // reset flash counter
  349.             }
  350.             lastUpdateTime = millis(); // Mark the update time
  351.           }
  352.           break;
  353.  
  354.         case 2: // Change to UP ON state
  355.           if(millis() > lastUpdateTime + FLASH_OFF_TIME_SETTING){
  356.             if(flashCount == 1 and colorTwo != off){
  357.               neopixelStrip.fill(colorTwo,upperLeftCorner(),width()); // Set the color
  358.             } else if(flashCount == 2 and colorThree != off){
  359.               neopixelStrip.fill(colorThree,upperLeftCorner(),width()); // Set the color
  360.             } else {
  361.               neopixelStrip.fill(colorOne,upperLeftCorner(),width()); // Set the color
  362.             }
  363.             currentState = 1; // Update State
  364.             lastUpdateTime = millis(); // Mark the update time
  365.           }
  366.           break;
  367.  
  368.         case 3: // Change to either DOWN OFF or UP ON state
  369.           if(millis() > lastUpdateTime + flashOnTime){
  370.             flashCount ++; // Incrament flash count
  371.             if(multiFlash > 1 and multiFlash != flashCount){
  372.               neopixelStrip.fill(off,lowerRightCorner(),width()); // turn off lower light
  373.               currentState = 4; // Update State
  374.             } else {
  375.               neopixelStrip.fill(colorOne,upperLeftCorner(),width()); // Set upper color
  376.               neopixelStrip.fill(off,lowerRightCorner(),width()); // Set lower color
  377.               currentState = 1; // Update State
  378.               flashCount = 0; // reset flash counter
  379.               numOfPatternCycles ++; // Incrament pattern cycle count
  380.             }
  381.             lastUpdateTime = millis(); // Mark the update time
  382.           }
  383.           break;
  384.  
  385.         case 4: // Change to DOWN ON state
  386.           if(millis() > lastUpdateTime + FLASH_OFF_TIME_SETTING){
  387.             if(flashCount == 1 and colorTwo != off){
  388.               neopixelStrip.fill(colorTwo,lowerRightCorner(),width()); // Set the color
  389.             } else if(flashCount == 2 and colorThree != off){
  390.               neopixelStrip.fill(colorThree,lowerRightCorner(),width()); // Set the color
  391.             } else {
  392.               neopixelStrip.fill(colorOne,lowerRightCorner(),width()); // Set the color
  393.             }
  394.             currentState = 3; // Update State
  395.             lastUpdateTime = millis(); // Mark the update time
  396.           }
  397.           break;
  398.  
  399.       }
  400.  
  401.       if(numOfPatternCycles == patternCyclesToRun){
  402.         return true; // Return True when we have run the requested amount of pattern cycles
  403.       } else {
  404.         return false;
  405.       }
  406.  
  407.     }
  408.  
  409.     // Flash Left side then Right side
  410.     boolean leftAndRight(byte multiFlash, uint32_t colorOne, uint32_t colorTwo = off, uint32_t colorThree = off, byte patternCyclesToRun = 1){
  411.  
  412.       //Definitions
  413.       //#define FLASH_ON_TIME_SETTING 900
  414.       //#define FLASH_OFF_TIME_SETTING 50
  415.  
  416.       // Caculate actual flash time
  417.       if(multiFlash > 1){
  418.         flashOnTime = (FLASH_ON_TIME_SETTING / multiFlash) - (FLASH_OFF_TIME_SETTING * multiFlash);
  419.       } else {
  420.         flashOnTime = FLASH_ON_TIME_SETTING;
  421.       }
  422.  
  423.       // Check if this pattern has just been activated - Pattern #4
  424.       if(currentPattern != 4){
  425.         currentPattern = 4; // Set to new current patern
  426.         currentState = 0; // Reset state to initial value
  427.         numOfPatternCycles = 0; // Reset number of pattern cycles
  428.       }
  429.  
  430.       switch(currentState){
  431.  
  432.         case 0: // Reset to initial LEFT ON state
  433.           neopixelStrip.fill(off,1,lastLedAddress); // clear all values
  434.           neopixelStrip.fill(colorOne,lowerMidPoint(),leftWrap()); // turn on LEFT light
  435.           lastUpdateTime = millis(); // Mark the update time
  436.           currentState = 1; // Update State
  437.           flashCount = 0; //update flash count
  438.           break;
  439.  
  440.         case 1: // Change to either LEFT OFF or RIGHT ON state
  441.           if(millis() > lastUpdateTime + flashOnTime){
  442.             flashCount ++; // Incrament flash count
  443.             if(multiFlash > 1 and multiFlash != flashCount){
  444.               neopixelStrip.fill(off,lowerMidPoint(),leftWrap()); // turn off LEFT light
  445.               currentState = 2; // Update State
  446.             } else {
  447.               neopixelStrip.fill(off,lowerMidPoint(),leftWrap()); // Set Left color
  448.               neopixelStrip.fill(colorOne,1,rightWraps()); // Set lowwer RIGHT color
  449.               neopixelStrip.fill(colorOne,upperMidPoint(),rightWraps()); // Set Upper RIGHT color
  450.               currentState = 3; // Update State
  451.               flashCount = 0; // reset flash counter
  452.             }
  453.             lastUpdateTime = millis(); // Mark the update time
  454.           }
  455.           break;
  456.  
  457.         case 2: // Change to LEFT ON state
  458.           if(millis() > lastUpdateTime + FLASH_OFF_TIME_SETTING){
  459.             if(flashCount == 1 and colorTwo != off){
  460.               neopixelStrip.fill(colorTwo,lowerMidPoint(),leftWrap()); // Set the color
  461.             } else if(flashCount == 2 and colorThree != off){
  462.               neopixelStrip.fill(colorThree,lowerMidPoint(),leftWrap()); // Set the color
  463.             } else {
  464.               neopixelStrip.fill(colorOne,lowerMidPoint(),leftWrap()); // Set the color
  465.             }
  466.             currentState = 1; // Update State
  467.             lastUpdateTime = millis(); // Mark the update time
  468.           }
  469.           break;
  470.  
  471.         case 3: // Change to either RIGHT OFF or LEFT ON state
  472.           if(millis() > lastUpdateTime + flashOnTime){
  473.             flashCount ++; // Incrament flash count
  474.             if(multiFlash > 1 and multiFlash != flashCount){
  475.               neopixelStrip.fill(off,1,rightWraps()); // turn off lower RIGHT light
  476.               neopixelStrip.fill(off,upperMidPoint(),rightWraps()); // turn off Upper RIGHT light
  477.               currentState = 4; // Update State
  478.             } else {
  479.               neopixelStrip.fill(off,1,rightWraps()); // turn off lower RIGHT light
  480.               neopixelStrip.fill(off,upperMidPoint(),rightWraps()); // turn off Upper RIGHT light
  481.               neopixelStrip.fill(colorOne,lowerMidPoint(),leftWrap()); // Set upper color
  482.               currentState = 1; // Update State
  483.               flashCount = 0; // reset flash counter
  484.               numOfPatternCycles ++; // Incrament pattern cycle count
  485.             }
  486.             lastUpdateTime = millis(); // Mark the update time
  487.           }
  488.           break;
  489.  
  490.         case 4: // Change to RIGHT ON state
  491.           if(millis() > lastUpdateTime + FLASH_OFF_TIME_SETTING){
  492.             if(flashCount == 1 and colorTwo != off){
  493.               neopixelStrip.fill(colorTwo,1,rightWraps()); // turn off lower RIGHT light
  494.               neopixelStrip.fill(colorTwo,upperMidPoint(),rightWraps()); // turn off Upper RIGHT light
  495.             } else if(flashCount == 2 and colorThree != off){
  496.               neopixelStrip.fill(colorThree,1,rightWraps()); // turn off lower RIGHT light
  497.               neopixelStrip.fill(colorThree,upperMidPoint(),rightWraps()); // turn off Upper RIGHT light
  498.             } else {
  499.               neopixelStrip.fill(colorOne,1,rightWraps()); // turn off lower RIGHT light
  500.               neopixelStrip.fill(colorOne,upperMidPoint(),rightWraps()); // turn off Upper RIGHT light
  501.             }
  502.             currentState = 3; // Update State
  503.             lastUpdateTime = millis(); // Mark the update time
  504.           }
  505.           break;
  506.  
  507.       }
  508.  
  509.       if(numOfPatternCycles == patternCyclesToRun){
  510.         return true; // Return True when we have run the requested amount of pattern cycles
  511.       } else {
  512.         return false;
  513.       }
  514.  
  515.     }
  516.  
  517.     // Flash strips kiddy corner to each other
  518.     boolean crissCross(byte multiFlash, uint32_t colorOne, uint32_t colorTwo = off, uint32_t colorThree = off, byte patternCyclesToRun = 1){
  519.  
  520.       //Definitions
  521.       //#define FLASH_ON_TIME_SETTING 900
  522.       //#define FLASH_OFF_TIME_SETTING 50
  523.  
  524.       // Caculate actual flash time
  525.       if(multiFlash > 1){
  526.         flashOnTime = (FLASH_ON_TIME_SETTING / multiFlash) - (FLASH_OFF_TIME_SETTING * multiFlash);
  527.       } else {
  528.         flashOnTime = FLASH_ON_TIME_SETTING;
  529.       }
  530.  
  531.       // Check if this pattern has just been activated - Pattern #3
  532.       if(currentPattern != 5){
  533.         currentPattern = 5; // Set to new current patern
  534.         currentState = 0; // Reset state to initial value
  535.         numOfPatternCycles = 0; // Reset number of pattern cycles
  536.       }
  537.  
  538.       switch(currentState){
  539.  
  540.         case 0: // Reset to initial UP RIGHT/DOWN LEFT ON state
  541.           neopixelStrip.fill(off,1,lastLedAddress); // clear all values
  542.           neopixelStrip.fill(colorOne,lowerMidPoint(),halfWidth()); // turn ON lower left light
  543.           neopixelStrip.fill(colorOne,upperMidPoint(),halfWidth()); // turn ON upper right light
  544.           lastUpdateTime = millis(); // Mark the update time
  545.           currentState = 1; // Update State
  546.           flashCount = 0; //update flash count
  547.           break;
  548.  
  549.         case 1: // Change to either UP RIGHT/DOWN LEFT OFF or UP LEFT/DOWN RIGHT ON state
  550.           if(millis() > lastUpdateTime + flashOnTime){
  551.             flashCount ++; // Incrament flash count
  552.             if(multiFlash > 1 and multiFlash != flashCount){
  553.               neopixelStrip.fill(off,1,lastLedAddress); // clear all values
  554.               currentState = 2; // Update State
  555.             } else {
  556.               neopixelStrip.fill(off,1,lastLedAddress); // clear all values
  557.               neopixelStrip.fill(colorOne,lowerRightCorner(),halfWidth()); // turn ON lower right light
  558.               neopixelStrip.fill(colorOne,upperLeftCorner(),halfWidth()); // turn ON upper left light
  559.               currentState = 3; // Update State
  560.               flashCount = 0; // reset flash counter
  561.             }
  562.             lastUpdateTime = millis(); // Mark the update time
  563.           }
  564.           break;
  565.  
  566.         case 2: // Change to UP RIGHT/DOWN LEFT ON state
  567.           if(millis() > lastUpdateTime + FLASH_OFF_TIME_SETTING){
  568.             if(flashCount == 1 and colorTwo != off){
  569.               neopixelStrip.fill(colorTwo,lowerMidPoint(),halfWidth()); // turn ON lower left light with 2nd color
  570.               neopixelStrip.fill(colorTwo,upperMidPoint(),halfWidth()); // turn ON upper right light with 2nd color
  571.             } else if(flashCount == 2 and colorThree != off){
  572.               neopixelStrip.fill(colorThree,lowerMidPoint(),halfWidth()); // turn ON lower left light with 3rd color
  573.               neopixelStrip.fill(colorThree,upperMidPoint(),halfWidth()); // turn ON upper right light with 3rd color
  574.             } else {
  575.               neopixelStrip.fill(colorOne,lowerMidPoint(),halfWidth()); // turn ON lower left light with 1st color
  576.               neopixelStrip.fill(colorOne,upperMidPoint(),halfWidth()); // turn ON upper right light with 1st color
  577.             }
  578.             currentState = 1; // Update State
  579.             lastUpdateTime = millis(); // Mark the update time
  580.           }
  581.           break;
  582.  
  583.         case 3: // Change to either UP LEFT/DOWN RIGHT OFF or UP RIGHT/DOWN LEFT ON state
  584.           if(millis() > lastUpdateTime + flashOnTime){
  585.             flashCount ++; // Incrament flash count
  586.             if(multiFlash > 1 and multiFlash != flashCount){
  587.               neopixelStrip.fill(off,1,lastLedAddress); // clear all values
  588.               currentState = 4; // Update State
  589.             } else {
  590.               neopixelStrip.fill(off,1,lastLedAddress); // clear all values
  591.               neopixelStrip.fill(colorOne,lowerMidPoint(),halfWidth()); // turn ON lower left light
  592.               neopixelStrip.fill(colorOne,upperMidPoint(),halfWidth()); // turn ON upper right ligt
  593.               currentState = 1; // Update State
  594.               flashCount = 0; // reset flash counter
  595.               numOfPatternCycles ++; // Incrament pattern cycle count
  596.             }
  597.             lastUpdateTime = millis(); // Mark the update time
  598.           }
  599.           break;
  600.  
  601.         case 4: // Change to UP LEFT/DOWN RIGHT ON state
  602.           if(millis() > lastUpdateTime + FLASH_OFF_TIME_SETTING){
  603.             if(flashCount == 1 and colorTwo != off){
  604.               neopixelStrip.fill(colorTwo,lowerRightCorner(),halfWidth()); // turn ON lower right light with 2nd color
  605.               neopixelStrip.fill(colorTwo,upperLeftCorner(),halfWidth()); // turn ON upper left light with 2nd color
  606.             } else if(flashCount == 2 and colorThree != off){
  607.               neopixelStrip.fill(colorThree,lowerRightCorner(),halfWidth()); // turn ON lower right light with 3rd color
  608.               neopixelStrip.fill(colorThree,upperLeftCorner(),halfWidth()); // turn ON upper left light with 3rd color
  609.             } else {
  610.               neopixelStrip.fill(colorOne,lowerRightCorner(),halfWidth()); // turn ON lower right light with 1st color
  611.               neopixelStrip.fill(colorOne,upperLeftCorner(),halfWidth()); // turn ON upper left light with 1st color
  612.             }
  613.             currentState = 3; // Update State
  614.             lastUpdateTime = millis(); // Mark the update time
  615.           }
  616.           break;
  617.  
  618.       }
  619.  
  620.       if(numOfPatternCycles == patternCyclesToRun){
  621.         return true; // Return True when we have run the requested amount of pattern cycles
  622.       } else {
  623.         return false;
  624.       }
  625.  
  626.     }
  627. };
  628.  
  629. class RelayDevice {
  630.   private:
  631.     byte pinNumber;
  632.  
  633.   public:
  634.     // Constructor
  635.     RelayDevice(byte pinNumber){
  636.       this->pinNumber = pinNumber;
  637.     }
  638.  
  639.     // Methods
  640.     void on(){
  641.       digitalWrite(pinNumber, HIGH);
  642.     }
  643.  
  644.     void off(){
  645.       digitalWrite(pinNumber, LOW);
  646.     }
  647. };
  648.  
  649.  
  650. // Build Objects
  651. RGBLightBar sideLightFrontLeftBar(12, sideLightFrontLeftStrip);
  652. RGBLightBar sideLightRearLeftBar(12, sideLightRearLeftStrip);
  653. RGBLightBar sideLightFrontRightBar(12, sideLightFrontRightStrip);
  654. RGBLightBar sideLightRearRightBar(12, sideLightRearRightStrip);
  655.  
  656. RelayDevice RGBLightsRelay(RGB_LIGHTS_RELAY_PIN);
  657. RelayDevice leftFloodLightsRelay(LEFT_FLOOD_LIGHTS_RELAY_PIN);
  658. RelayDevice rightFloodLightsRelay(RIGHT_FLOOD_LIGHTS_RELAY_PIN);
  659. RelayDevice leftChaseLightRelay(LEFT_CHASE_LIGHT_RELAY_PIN);
  660. RelayDevice rightChaseLightRelay(RIGHT_CHASE_LIGHT_RELAY_PIN);
  661.  
  662.  
  663.  
  664. void setup() {
  665.   // put your setup code here, to run once:
  666.  
  667.   // ---I2C Setup---
  668.   Wire.begin(10);           // join I2C bus as Slave with address 10
  669.  
  670.   // event handler initializations
  671.   Wire.onReceive(dataRcv);    // register an event handler for received data
  672.  
  673.   // initialize global variables
  674.   i2c_pattern = 255;
  675.   i2c_option = 255;
  676.   currentPattern = 255;
  677.   currentOption = 255;
  678.   i2c_message_LED_flash_start_timer = millis();
  679.   i2c_message_LED_status = 0;
  680.  
  681.   // Initialize Strips
  682.   sideLightFrontLeftStrip.setBrightness(RGB_LIGHTS_INITIAL_BRIGHNESS);
  683.   sideLightRearLeftStrip.setBrightness(RGB_LIGHTS_INITIAL_BRIGHNESS);
  684.   sideLightFrontRightStrip.setBrightness(RGB_LIGHTS_INITIAL_BRIGHNESS);
  685.   sideLightRearRightStrip.setBrightness(RGB_LIGHTS_INITIAL_BRIGHNESS);
  686.  
  687.   sideLightFrontLeftStrip.begin();
  688.   sideLightRearLeftStrip.begin();
  689.   sideLightFrontRightStrip.begin();
  690.   sideLightRearRightStrip.begin();
  691.  
  692.   sideLightFrontLeftBar.mainLightOff(); // Shut off main light after setup
  693.   sideLightRearLeftBar.mainLightOff(); // Shut off main light after setup
  694.   sideLightFrontRightBar.mainLightOff(); // Shut off main light after setup
  695.   sideLightRearRightBar.mainLightOff(); // Shut off main light after setup
  696.  
  697.   sideLightFrontLeftBar.solidColor(off); // Shut off RGB ring after setup
  698.   sideLightRearLeftBar.solidColor(off); // Shut off RGB ring after setup
  699.   sideLightFrontRightBar.solidColor(off); // Shut off RGB ring after setup
  700.   sideLightRearRightBar.solidColor(off); // Shut off RGB ring after setup
  701.  
  702.   // Initialize other outputs
  703.   pinMode(RGB_LIGHTS_RELAY_PIN,OUTPUT);  // Setup RGB light bars relay pin
  704.   pinMode(LEFT_FLOOD_LIGHTS_RELAY_PIN,OUTPUT);  // Setup left side flood lights relay pin
  705.   pinMode(RIGHT_FLOOD_LIGHTS_RELAY_PIN,OUTPUT);  // Setup right side flood lights relay pin
  706.   pinMode(LEFT_CHASE_LIGHT_RELAY_PIN,OUTPUT);  // Setup left side chase lights relay pin
  707.   pinMode(RIGHT_CHASE_LIGHT_RELAY_PIN,OUTPUT);  // Setup right side chase lights relay pin
  708.  
  709. }
  710.  
  711. void loop() {
  712.   // put your main code here, to run repeatedly:
  713.  
  714.   RGBLightsRelay.on(); // Turn on Relay to power RGB light bars
  715.  
  716.   // I2C message testing
  717.   //i2c_pattern = 255;
  718.   //i2c_option = 255;
  719.  
  720.   updateOutputs();
  721.  
  722.   // Turn off message LED
  723.   if((millis() - i2c_message_LED_flash_start_timer) > I2C_MESSAGE_RECEIVED_LED_FLASH_LENGTH){
  724.     i2c_message_LED_status = 0; // Turn off Light
  725.   }
  726.  
  727.   digitalWrite(13, i2c_message_LED_status); // Update LED State
  728.  
  729.   // Update all light strips constantly
  730.   sideLightFrontLeftStrip.show();
  731.   sideLightRearLeftStrip.show();
  732.   sideLightFrontRightStrip.show();
  733.   sideLightRearRightStrip.show();
  734. }
  735.  
  736.  
  737. // --Functions--
  738.  
  739. // received data handler function
  740. void dataRcv(int numBytes){
  741.   while(Wire.available()) { // read all bytes received
  742.     i2c_pattern = Wire.read();
  743.     i2c_option = Wire.read();
  744.   }
  745.   i2c_message_LED_status = 1; // Turn on the LED
  746.   i2c_message_LED_flash_start_timer = millis();
  747. }
  748.  
  749. // Make decisions based on the newly received I2C messages
  750. void updateOutputs(){
  751.  
  752.   // Process incoming I2C messages
  753.   switch(i2c_pattern){
  754.  
  755.     case 0: // RGB Light Bar(s) Power OFF
  756.  
  757.       // Program for this later.  This should kill the relay to all RGB light bars
  758.  
  759.       break;
  760.  
  761.     case 1: // RGB Light Bar(s) Power On
  762.  
  763.       // Program for this later.  This should energize the relay to all RGB light bars
  764.       // and set all lights to off
  765.  
  766.       break;
  767.  
  768.     case 2: // All lights OFF
  769.  
  770.       // Reset the current Pattern/Option to the default of 255
  771.       currentPattern = 255;
  772.       currentOption = 255;
  773.  
  774.       sideLightFrontLeftBar.mainLightOff();
  775.       sideLightRearLeftBar.mainLightOff();
  776.       sideLightFrontRightBar.mainLightOff();
  777.       sideLightRearRightBar.mainLightOff();
  778.  
  779.       sideLightFrontLeftBar.solidColor(off);
  780.       sideLightRearLeftBar.solidColor(off);
  781.       sideLightFrontRightBar.solidColor(off);
  782.       sideLightRearRightBar.solidColor(off);
  783.  
  784.       leftFloodLightsRelay.off();
  785.       rightFloodLightsRelay.off();
  786.       leftChaseLightRelay.off();
  787.       rightChaseLightRelay.off();
  788.  
  789.       break;
  790.  
  791.     case 3: // All main lights OFF
  792.  
  793.       sideLightFrontLeftBar.mainLightOff();
  794.       sideLightRearLeftBar.mainLightOff();
  795.       sideLightFrontRightBar.mainLightOff();
  796.       sideLightRearRightBar.mainLightOff();
  797.  
  798.       leftFloodLightsRelay.off();
  799.       rightFloodLightsRelay.off();
  800.  
  801.       break;
  802.  
  803.     case 4: // All main lights ON
  804.  
  805.       sideLightFrontLeftBar.mainLightOn();
  806.       sideLightRearLeftBar.mainLightOn();
  807.       sideLightFrontRightBar.mainLightOn();
  808.       sideLightRearRightBar.mainLightOn();
  809.  
  810.       leftFloodLightsRelay.on();
  811.       rightFloodLightsRelay.on();
  812.  
  813.       break;
  814.  
  815.     case 5: // Left main lights OFF
  816.  
  817.       sideLightFrontLeftBar.mainLightOff();
  818.       sideLightRearLeftBar.mainLightOff();
  819.  
  820.       leftFloodLightsRelay.off();
  821.  
  822.       break;
  823.  
  824.     case 6: // Left main lights ON
  825.  
  826.       sideLightFrontLeftBar.mainLightOn();
  827.       sideLightRearLeftBar.mainLightOn();
  828.  
  829.       leftFloodLightsRelay.on();
  830.  
  831.       break;
  832.  
  833.     case 7: // Right main lights OFF
  834.  
  835.       sideLightFrontRightBar.mainLightOff();
  836.       sideLightRearRightBar.mainLightOff();
  837.  
  838.       rightFloodLightsRelay.off();
  839.  
  840.       break;
  841.  
  842.     case 8: //Right main lights ON
  843.  
  844.       sideLightFrontRightBar.mainLightOn();
  845.       sideLightRearRightBar.mainLightOn();
  846.  
  847.       rightFloodLightsRelay.on();
  848.  
  849.       break;
  850.  
  851.     case 9: // Chase lights OFF
  852.  
  853.       leftChaseLightRelay.off();
  854.       rightChaseLightRelay.off();
  855.  
  856.       break;
  857.  
  858.     case 10: // Chase lights ON
  859.  
  860.       leftChaseLightRelay.on();
  861.       rightChaseLightRelay.on();
  862.  
  863.       break;
  864.  
  865.     case 11: // RGB (All) OFF
  866.  
  867.       // Reset the Current Patern and Option to the default of 255
  868.       currentPattern = 255;
  869.       currentOption = 255;
  870.  
  871.       sideLightFrontLeftBar.solidColor(off);
  872.       sideLightRearLeftBar.solidColor(off);
  873.       sideLightFrontRightBar.solidColor(off);
  874.       sideLightRearRightBar.solidColor(off);
  875.  
  876.       break;
  877.  
  878.     case 12: // RGB (All) Red
  879.  
  880.       // Add this functionality later
  881.  
  882.       break;
  883.  
  884.     case 13: // RGB left Red
  885.  
  886.       // Add this functionality later
  887.  
  888.       break;
  889.  
  890.     case 14: // RGB right Red
  891.  
  892.       // Add this functionality later
  893.  
  894.       break;
  895.  
  896.     case 15: // Turn signal left OFF
  897.  
  898.       // The board does not do anything with turn signals
  899.  
  900.       break;
  901.  
  902.     case 16: // Turn signal left ON
  903.  
  904.       // The board does not do anything with turn signals
  905.  
  906.       break;
  907.  
  908.     case 17: // Turn signal right OFF
  909.  
  910.       // The board does not do anything with turn signals
  911.  
  912.       break;
  913.  
  914.     case 18: // Turn signal right ON
  915.  
  916.       // The board does not do anything with turn signals
  917.  
  918.       break;
  919.  
  920.     case 19: // Brake OFF
  921.  
  922.       // The board does not do anything with brakes
  923.  
  924.       break;
  925.  
  926.     case 20: // Brake ON
  927.  
  928.       // The board does not do anything with brakes
  929.  
  930.       break;
  931.  
  932.     case 21: // RGB caution pattern cycle
  933.  
  934.       // Save the Current Pattern and Option.  Continu Processing below
  935.       currentPattern = i2c_pattern;
  936.       currentOption = i2c_option;
  937.  
  938.       break;
  939.  
  940.     case 22: // RBG hazard left
  941.  
  942.       // The board does not do anything with hazard left signals
  943.  
  944.       break;
  945.  
  946.     case 23: // RGB hazard right
  947.  
  948.       // The board does not do anything with hazard right signals
  949.  
  950.       break;
  951.  
  952.     case 24: // RGB hazard center
  953.  
  954.       // The board does not do anything with hazard center signals
  955.  
  956.       break;
  957.  
  958.     case 100: // RGB (all) Solid
  959.  
  960.       // Save the Current Pattern and Option.  Continue processing below
  961.       currentPattern = i2c_pattern;
  962.       currentOption = i2c_option;
  963.  
  964.       break;
  965.  
  966.     case 101: // RGB (all) Full Jump Change Colors
  967.  
  968.       // Program this later
  969.  
  970.       break;
  971.  
  972.     case 102: // RGB (all) Full fade change color
  973.  
  974.       // Program this later
  975.  
  976.       break;
  977.  
  978.     case 103: // RGB (all) Full fade out change colors
  979.  
  980.       // Program this later
  981.  
  982.       break;
  983.  
  984.     case 104: // RGB (all) Chase Fade
  985.  
  986.       // Program this later
  987.  
  988.       break;
  989.  
  990.     case 105: // RGB (all) chase fade out
  991.  
  992.       // Program this later
  993.  
  994.       break;
  995.  
  996.     case 106: // Rainbow Road- 80 Full Speed | 81 Fast | 82 Moderate | 83 Slow
  997.  
  998.       // Save the Current Pattern and Option.  Continue processing below
  999.       currentPattern = i2c_pattern;
  1000.       currentOption = i2c_option;
  1001.  
  1002.       break;
  1003.  
  1004.     default:
  1005.       // If I2C message is set to the defualt 255 then do nothing
  1006.       break;
  1007.   }
  1008.  
  1009.   // Reset I2C variables
  1010.   i2c_pattern = 255;
  1011.   i2c_option = 255;
  1012.  
  1013.   // Process constant patterns
  1014.   switch(currentPattern){
  1015.     case 21: // RGB caution pattern cycle
  1016.       sideLightFrontLeftBar.cautionPatternCycle();
  1017.       sideLightRearLeftBar.cautionPatternCycle();
  1018.       sideLightFrontRightBar.cautionPatternCycle();
  1019.       sideLightRearRightBar.cautionPatternCycle();
  1020.       break;
  1021.  
  1022.     case 100: // RGB (all) Solid
  1023.  
  1024.       switch(currentOption){
  1025.  
  1026.         case 0: // OFF
  1027.           sideLightFrontLeftBar.solidColor(off);
  1028.           sideLightRearLeftBar.solidColor(off);
  1029.           sideLightFrontRightBar.solidColor(off);
  1030.           sideLightRearRightBar.solidColor(off);
  1031.           break;
  1032.  
  1033.         case 1: // White
  1034.           sideLightFrontLeftBar.solidColor(white);
  1035.           sideLightRearLeftBar.solidColor(white);
  1036.           sideLightFrontRightBar.solidColor(white);
  1037.           sideLightRearRightBar.solidColor(white);
  1038.           break;
  1039.  
  1040.         case 2: // Red
  1041.           sideLightFrontLeftBar.solidColor(red);
  1042.           sideLightRearLeftBar.solidColor(red);
  1043.           sideLightFrontRightBar.solidColor(red);
  1044.           sideLightRearRightBar.solidColor(red);
  1045.           break;
  1046.  
  1047.         case 3: // Green
  1048.           sideLightFrontLeftBar.solidColor(green);
  1049.           sideLightRearLeftBar.solidColor(green);
  1050.           sideLightFrontRightBar.solidColor(green);
  1051.           sideLightRearRightBar.solidColor(green);
  1052.           break;
  1053.  
  1054.         case 4: // Blue
  1055.           sideLightFrontLeftBar.solidColor(blue);
  1056.           sideLightRearLeftBar.solidColor(blue);
  1057.           sideLightFrontRightBar.solidColor(blue);
  1058.           sideLightRearRightBar.solidColor(blue);
  1059.           break;
  1060.  
  1061.         case 5: // Orange
  1062.           sideLightFrontLeftBar.solidColor(orange);
  1063.           sideLightRearLeftBar.solidColor(orange);
  1064.           sideLightFrontRightBar.solidColor(orange);
  1065.           sideLightRearRightBar.solidColor(orange);
  1066.           break;
  1067.  
  1068.         case 6: // Yellow
  1069.           sideLightFrontLeftBar.solidColor(yellow);
  1070.           sideLightRearLeftBar.solidColor(yellow);
  1071.           sideLightFrontRightBar.solidColor(yellow);
  1072.           sideLightRearRightBar.solidColor(yellow);
  1073.           break;
  1074.  
  1075.         case 7: // Purple
  1076.           sideLightFrontLeftBar.solidColor(purple);
  1077.           sideLightRearLeftBar.solidColor(purple);
  1078.           sideLightFrontRightBar.solidColor(purple);
  1079.           sideLightRearRightBar.solidColor(purple);
  1080.           break;
  1081.       }
  1082.  
  1083.       break;
  1084.  
  1085.     case 106: // Rainbow Road- 80 Full Speed | 81 Fast | 82 Moderate | 83 Slow
  1086.       switch(currentOption){
  1087.         case 80: // Full Speed
  1088.           sideLightFrontLeftBar.rainbow(0);
  1089.           sideLightRearLeftBar.rainbow(0);
  1090.           sideLightFrontRightBar.rainbow(0);
  1091.           sideLightRearRightBar.rainbow(0);
  1092.           break;
  1093.         case 81: // Fast
  1094.           sideLightFrontLeftBar.rainbow(1);
  1095.           sideLightRearLeftBar.rainbow(1);
  1096.           sideLightFrontRightBar.rainbow(1);
  1097.           sideLightRearRightBar.rainbow(1);
  1098.           break;
  1099.         case 82: // Moderate
  1100.           sideLightFrontLeftBar.rainbow(2);
  1101.           sideLightRearLeftBar.rainbow(2);
  1102.           sideLightFrontRightBar.rainbow(2);
  1103.           sideLightRearRightBar.rainbow(2);
  1104.           break;
  1105.         case 83: // Slow
  1106.           sideLightFrontLeftBar.rainbow(3);
  1107.           sideLightRearLeftBar.rainbow(3);
  1108.           sideLightFrontRightBar.rainbow(3);
  1109.           sideLightRearRightBar.rainbow(3);
  1110.           break;
  1111.       }
  1112.  
  1113.       break;
  1114.   }
  1115. }
  1116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement