Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ********************************************
- * *
- * Project: Wrexus Carduino Controls *
- * Board: Nano 3 *
- * Devices Served: Side RGB Light Bars *
- * Side Flood Lights *
- * Chase Lights *
- * Version: 0.001 *
- * Last Updated: 2021.12.29 *
- * *
- ********************************************
- */
- // Include libraries
- #include <Adafruit_NeoPixel.h>
- #include <Wire.h>
- // Definitions
- #define SIDE_LIGHT_FRONT_LEFT_DATA_PIN 12
- #define SIDE_LIGHT_REAR_LEFT_DATA_PIN 11
- #define SIDE_LIGHT_FRONT_RIGHT_DATA_PIN 10
- #define SIDE_LIGHT_REAR_RIGHT_DATA_PIN 8
- #define SIDE_LIGHT_NUM_LEDS 13
- #define RGB_LIGHTS_RELAY_PIN 7
- #define LEFT_FLOOD_LIGHTS_RELAY_PIN 6
- #define RIGHT_FLOOD_LIGHTS_RELAY_PIN 5
- #define LEFT_CHASE_LIGHT_RELAY_PIN 4
- #define RIGHT_CHASE_LIGHT_RELAY_PIN 3
- #define RGB_LIGHTS_INITIAL_BRIGHNESS 255
- #define PATTERN_CYCLE_TIME 5000
- #define FLASH_ON_TIME_SETTING 400
- #define FLASH_OFF_TIME_SETTING 20
- #define I2C_MESSAGE_RECEIVED_LED_FLASH_LENGTH 250 // The milliseconds time that the light will flash for
- // I2C Variables
- int i2c_pattern; // data received from I2C bus
- int i2c_option; // last data received from I2C bus
- unsigned long i2c_message_LED_flash_start_timer; // start time in milliseconds for flash
- int i2c_message_LED_status; // status of LED: 1 = ON, 0 = OFF
- // Currenly running pattern variables
- int currentPattern;
- int currentOption;
- // Pattern Variables
- unsigned long lastPatternChange = millis();
- byte currentLightPattern = 2;
- byte randomPatternFlair = random(1,3);
- // Setup Strips
- Adafruit_NeoPixel sideLightFrontLeftStrip = Adafruit_NeoPixel(SIDE_LIGHT_NUM_LEDS, SIDE_LIGHT_FRONT_LEFT_DATA_PIN, NEO_GRB + NEO_KHZ800);
- Adafruit_NeoPixel sideLightRearLeftStrip = Adafruit_NeoPixel(SIDE_LIGHT_NUM_LEDS, SIDE_LIGHT_REAR_LEFT_DATA_PIN, NEO_GRB + NEO_KHZ800);
- Adafruit_NeoPixel sideLightFrontRightStrip = Adafruit_NeoPixel(SIDE_LIGHT_NUM_LEDS, SIDE_LIGHT_FRONT_RIGHT_DATA_PIN, NEO_GRB + NEO_KHZ800);
- Adafruit_NeoPixel sideLightRearRightStrip = Adafruit_NeoPixel(SIDE_LIGHT_NUM_LEDS, SIDE_LIGHT_REAR_RIGHT_DATA_PIN, NEO_GRB + NEO_KHZ800);
- // Define named colors
- //uint32_t hudColor = strip.Color(0, 0, 0, 255); // Current hud color
- uint32_t white = sideLightFrontLeftStrip.Color(255,255,255); // Daytime indicator hud state
- uint32_t red = sideLightFrontLeftStrip.Color(255,0,0); // Nightime indicator hud state
- uint32_t green = sideLightFrontLeftStrip.Color(0,255,0); // On indicator state
- uint32_t blue = sideLightFrontLeftStrip.Color(0,0,255);
- uint32_t orange = sideLightFrontLeftStrip.Color(255,80,0); // Auto indicator state
- uint32_t yellow = sideLightFrontLeftStrip.Color(255,180,10);
- uint32_t purple = sideLightFrontLeftStrip.Color(128,0,128);
- uint32_t off = sideLightFrontLeftStrip.Color(0,0,0);
- // Build Classes
- class RGBLightBar {
- private:
- // Stip that will be associated with this Light Bar
- Adafruit_NeoPixel neopixelStrip;
- // Variables
- byte lastLedAddress;
- byte currentCautionPatternCall;
- byte currentPattern;
- byte currentState;
- byte numOfPatternCycles;
- unsigned long lastUpdateTime;
- int flashOnTime;
- byte flashCount;
- long firstPixelHue;
- // Light segment calculations
- byte lowerRightCorner(){
- return 2;
- }
- byte upperLeftCorner(){
- byte toReturn = (lastLedAddress / 2) + 2;
- return toReturn;
- }
- byte width(){
- byte toReturn = lastLedAddress - upperLeftCorner();
- return toReturn;
- }
- byte lowerMidPoint(){
- byte toReturn = (width() / 2) + 2;
- return toReturn;
- }
- byte upperMidPoint(){
- byte toReturn = upperLeftCorner() + (width() / 2);
- return toReturn;
- }
- byte halfWidth(){
- byte toReturn;
- if(lastLedAddress == 12){
- toReturn = (width() / 2);
- } else {
- toReturn = (width() / 2) + 1;
- }
- return toReturn;
- }
- byte leftWrap(){
- byte toReturn;
- if(lastLedAddress == 12){
- toReturn = upperMidPoint() - lowerMidPoint();
- } else {
- toReturn = upperMidPoint() - lowerMidPoint() + 1;
- }
- return toReturn;
- }
- byte rightWraps(){
- byte toReturn;
- if(lastLedAddress == 12){
- toReturn = (upperMidPoint() - lowerMidPoint()) / 2;
- } else {
- toReturn = (upperMidPoint() - lowerMidPoint()) / 2 + 1;
- }
- return toReturn;
- }
- public:
- // Constructor
- RGBLightBar(byte lastLedAddress, Adafruit_NeoPixel &neopixelStrip){
- this->lastLedAddress = lastLedAddress;
- this->neopixelStrip = neopixelStrip;
- }
- // Methods
- void mainLightOn(){
- neopixelStrip.setPixelColor(0,red);
- }
- void mainLightOff(){
- neopixelStrip.setPixelColor(0,green);
- }
- void solidColor(uint32_t color){
- neopixelStrip.fill(color,1,lastLedAddress);
- }
- void cautionPatternCycle(){
- if(currentCautionPatternCall < 2 or currentCautionPatternCall > 5){
- currentCautionPatternCall = 2;
- }
- if(currentCautionPatternCall == 2){
- if(flashFull(randomPatternFlair,yellow,white,yellow,5)){
- currentCautionPatternCall ++;
- randomPatternFlair = random(1,4);
- }
- } else if(currentCautionPatternCall == 3){
- if(upAndDown(randomPatternFlair,yellow,white,yellow,5)){
- currentCautionPatternCall ++;
- randomPatternFlair = random(1,4);
- }
- } else if(currentCautionPatternCall == 4){
- if(leftAndRight(randomPatternFlair,yellow,white,yellow,5)){
- currentCautionPatternCall ++;
- randomPatternFlair = random(1,4);
- }
- } else if(currentCautionPatternCall == 5){
- if(crissCross(randomPatternFlair,yellow,white,yellow,5)){
- currentCautionPatternCall ++;
- randomPatternFlair = random(1,4);
- }
- }
- }
- // Rainbow cycle along whole strip. Pass speed 0 = Full Speed | 1 = Fast | 2 = Moderate | 3 = Slow
- void rainbow(int wait) {
- // Check if this pattern has just been activated - Pattern #4
- if(currentPattern != 1){
- currentPattern = 1; // Set to new current patern
- firstPixelHue = 0; // Reset state to initial value
- lastUpdateTime = millis() + wait * 10; // Set lastUpdateTime so code will run first time
- }
- if(millis() > lastUpdateTime + wait * 10){
- // Reset pixel hue if we have gone over the amount
- if(firstPixelHue > 5*65536){
- firstPixelHue = 0;
- }
- // Fill light with the current ranbow patern
- for(int i=1; i<neopixelStrip.numPixels(); i++) { // For each pixel in strip...
- // Offset pixel hue by an amount to make one full revolution of the
- // color wheel (range of 65536) along the length of the strip
- // (strip.numPixels() steps):
- int pixelHue = firstPixelHue + (i * 65536L / neopixelStrip.numPixels());
- // neopixelStrip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
- // optionally add saturation and value (brightness) (each 0 to 255).
- // Here we're using just the single-argument hue variant. The result
- // is passed through neopixelStrip.gamma32() to provide 'truer' colors
- // before assigning to each pixel:
- neopixelStrip.setPixelColor(i, neopixelStrip.gamma32(neopixelStrip.ColorHSV(pixelHue)));
- }
- // Incrament Hue value
- firstPixelHue += 256;
- // Set last update time
- lastUpdateTime = millis();
- }
- }
- // Flash whole strip at same time
- boolean flashFull(byte multiFlash, uint32_t colorOne, uint32_t colorTwo = off, uint32_t colorThree = off, byte patternCyclesToRun = 1){
- //Definitions
- //#define FLASH_ON_TIME_SETTING 900
- //#define FLASH_OFF_TIME_SETTING 50
- // Caculate actual flash time
- if(multiFlash > 1){
- flashOnTime = (FLASH_ON_TIME_SETTING / multiFlash) - (FLASH_OFF_TIME_SETTING * multiFlash);
- } else {
- flashOnTime = FLASH_ON_TIME_SETTING;
- }
- // Check if this pattern has just been activated - Pattern #2
- if(currentPattern != 2){
- currentPattern = 2; // Set to new current patern
- currentState = 0; // Reset state to initial value
- numOfPatternCycles = 0; // Reset number of pattern cycles
- }
- switch(currentState){
- case 0: // Reset to initial ON state
- neopixelStrip.fill(colorOne,1,lastLedAddress); // Set the color
- lastUpdateTime = millis(); // Mark the update time
- currentState = 1; // Update State
- flashCount = 0; //update flash count
- break;
- case 1: // Change to OFF state
- if(millis() >= lastUpdateTime + flashOnTime){
- neopixelStrip.fill(off,1,lastLedAddress); // Turn off strip
- lastUpdateTime = millis(); // Mark the update time
- currentState = 2; // Update State
- flashCount ++; // Incrament flash count
- }
- break;
- case 2: // Loop back to ON state
- if(flashCount == multiFlash){
- // Wait for full time if we have flashed the proper amount of times.
- if(millis() >= lastUpdateTime + FLASH_ON_TIME_SETTING){
- neopixelStrip.fill(colorOne,1,lastLedAddress); // Set the color
- lastUpdateTime = millis(); // Mark the update time
- currentState = 1; // Update State
- flashCount = 0; //update flash count
- numOfPatternCycles ++; // Incrament pattern cycle count
- }
- } else {
- if(millis() >= lastUpdateTime + FLASH_OFF_TIME_SETTING){
- if(flashCount == 1 and colorTwo != off){
- neopixelStrip.fill(colorTwo,1,lastLedAddress); // Set the color
- } else if(flashCount == 2 and colorThree != off){
- neopixelStrip.fill(colorThree,1,lastLedAddress); // Set the color
- } else {
- neopixelStrip.fill(colorOne,1,lastLedAddress); // Set the color
- }
- lastUpdateTime = millis(); // Mark the update time
- currentState = 1; // Update State
- }
- }
- break;
- }
- if(numOfPatternCycles == patternCyclesToRun){
- return true; // Return True when we have run the requested amount of pattern cycles
- } else {
- return false;
- }
- }
- // Flash top strip then bottom strip
- boolean upAndDown(byte multiFlash, uint32_t colorOne, uint32_t colorTwo = off, uint32_t colorThree = off, byte patternCyclesToRun = 1){
- //Definitions
- //#define FLASH_ON_TIME_SETTING 900
- //#define FLASH_OFF_TIME_SETTING 50
- // Caculate actual flash time
- if(multiFlash > 1){
- flashOnTime = (FLASH_ON_TIME_SETTING / multiFlash) - (FLASH_OFF_TIME_SETTING * multiFlash);
- } else {
- flashOnTime = FLASH_ON_TIME_SETTING;
- }
- // Check if this pattern has just been activated - Pattern #3
- if(currentPattern != 3){
- currentPattern = 3; // Set to new current patern
- currentState = 0; // Reset state to initial value
- numOfPatternCycles = 0; // Reset number of pattern cycles
- }
- switch(currentState){
- case 0: // Reset to initial UP ON state
- neopixelStrip.fill(off,1,lastLedAddress); // clear all values
- neopixelStrip.fill(colorOne,upperLeftCorner(), width()) ; // turn on upper light
- lastUpdateTime = millis(); // Mark the update time
- currentState = 1; // Update State
- flashCount = 0; //update flash count
- break;
- case 1: // Change to either UP OFF or DOWN ON state
- if(millis() > lastUpdateTime + flashOnTime){
- flashCount ++; // Incrament flash count
- if(multiFlash > 1 and multiFlash != flashCount){
- neopixelStrip.fill(off,upperLeftCorner(),width()); // turn off upper light
- currentState = 2; // Update State
- } else {
- neopixelStrip.fill(colorOne,lowerRightCorner(),width()); // Set lowwer color
- neopixelStrip.fill(off,upperLeftCorner(),width()); // Set Upper color
- currentState = 3; // Update State
- flashCount = 0; // reset flash counter
- }
- lastUpdateTime = millis(); // Mark the update time
- }
- break;
- case 2: // Change to UP ON state
- if(millis() > lastUpdateTime + FLASH_OFF_TIME_SETTING){
- if(flashCount == 1 and colorTwo != off){
- neopixelStrip.fill(colorTwo,upperLeftCorner(),width()); // Set the color
- } else if(flashCount == 2 and colorThree != off){
- neopixelStrip.fill(colorThree,upperLeftCorner(),width()); // Set the color
- } else {
- neopixelStrip.fill(colorOne,upperLeftCorner(),width()); // Set the color
- }
- currentState = 1; // Update State
- lastUpdateTime = millis(); // Mark the update time
- }
- break;
- case 3: // Change to either DOWN OFF or UP ON state
- if(millis() > lastUpdateTime + flashOnTime){
- flashCount ++; // Incrament flash count
- if(multiFlash > 1 and multiFlash != flashCount){
- neopixelStrip.fill(off,lowerRightCorner(),width()); // turn off lower light
- currentState = 4; // Update State
- } else {
- neopixelStrip.fill(colorOne,upperLeftCorner(),width()); // Set upper color
- neopixelStrip.fill(off,lowerRightCorner(),width()); // Set lower color
- currentState = 1; // Update State
- flashCount = 0; // reset flash counter
- numOfPatternCycles ++; // Incrament pattern cycle count
- }
- lastUpdateTime = millis(); // Mark the update time
- }
- break;
- case 4: // Change to DOWN ON state
- if(millis() > lastUpdateTime + FLASH_OFF_TIME_SETTING){
- if(flashCount == 1 and colorTwo != off){
- neopixelStrip.fill(colorTwo,lowerRightCorner(),width()); // Set the color
- } else if(flashCount == 2 and colorThree != off){
- neopixelStrip.fill(colorThree,lowerRightCorner(),width()); // Set the color
- } else {
- neopixelStrip.fill(colorOne,lowerRightCorner(),width()); // Set the color
- }
- currentState = 3; // Update State
- lastUpdateTime = millis(); // Mark the update time
- }
- break;
- }
- if(numOfPatternCycles == patternCyclesToRun){
- return true; // Return True when we have run the requested amount of pattern cycles
- } else {
- return false;
- }
- }
- // Flash Left side then Right side
- boolean leftAndRight(byte multiFlash, uint32_t colorOne, uint32_t colorTwo = off, uint32_t colorThree = off, byte patternCyclesToRun = 1){
- //Definitions
- //#define FLASH_ON_TIME_SETTING 900
- //#define FLASH_OFF_TIME_SETTING 50
- // Caculate actual flash time
- if(multiFlash > 1){
- flashOnTime = (FLASH_ON_TIME_SETTING / multiFlash) - (FLASH_OFF_TIME_SETTING * multiFlash);
- } else {
- flashOnTime = FLASH_ON_TIME_SETTING;
- }
- // Check if this pattern has just been activated - Pattern #4
- if(currentPattern != 4){
- currentPattern = 4; // Set to new current patern
- currentState = 0; // Reset state to initial value
- numOfPatternCycles = 0; // Reset number of pattern cycles
- }
- switch(currentState){
- case 0: // Reset to initial LEFT ON state
- neopixelStrip.fill(off,1,lastLedAddress); // clear all values
- neopixelStrip.fill(colorOne,lowerMidPoint(),leftWrap()); // turn on LEFT light
- lastUpdateTime = millis(); // Mark the update time
- currentState = 1; // Update State
- flashCount = 0; //update flash count
- break;
- case 1: // Change to either LEFT OFF or RIGHT ON state
- if(millis() > lastUpdateTime + flashOnTime){
- flashCount ++; // Incrament flash count
- if(multiFlash > 1 and multiFlash != flashCount){
- neopixelStrip.fill(off,lowerMidPoint(),leftWrap()); // turn off LEFT light
- currentState = 2; // Update State
- } else {
- neopixelStrip.fill(off,lowerMidPoint(),leftWrap()); // Set Left color
- neopixelStrip.fill(colorOne,1,rightWraps()); // Set lowwer RIGHT color
- neopixelStrip.fill(colorOne,upperMidPoint(),rightWraps()); // Set Upper RIGHT color
- currentState = 3; // Update State
- flashCount = 0; // reset flash counter
- }
- lastUpdateTime = millis(); // Mark the update time
- }
- break;
- case 2: // Change to LEFT ON state
- if(millis() > lastUpdateTime + FLASH_OFF_TIME_SETTING){
- if(flashCount == 1 and colorTwo != off){
- neopixelStrip.fill(colorTwo,lowerMidPoint(),leftWrap()); // Set the color
- } else if(flashCount == 2 and colorThree != off){
- neopixelStrip.fill(colorThree,lowerMidPoint(),leftWrap()); // Set the color
- } else {
- neopixelStrip.fill(colorOne,lowerMidPoint(),leftWrap()); // Set the color
- }
- currentState = 1; // Update State
- lastUpdateTime = millis(); // Mark the update time
- }
- break;
- case 3: // Change to either RIGHT OFF or LEFT ON state
- if(millis() > lastUpdateTime + flashOnTime){
- flashCount ++; // Incrament flash count
- if(multiFlash > 1 and multiFlash != flashCount){
- neopixelStrip.fill(off,1,rightWraps()); // turn off lower RIGHT light
- neopixelStrip.fill(off,upperMidPoint(),rightWraps()); // turn off Upper RIGHT light
- currentState = 4; // Update State
- } else {
- neopixelStrip.fill(off,1,rightWraps()); // turn off lower RIGHT light
- neopixelStrip.fill(off,upperMidPoint(),rightWraps()); // turn off Upper RIGHT light
- neopixelStrip.fill(colorOne,lowerMidPoint(),leftWrap()); // Set upper color
- currentState = 1; // Update State
- flashCount = 0; // reset flash counter
- numOfPatternCycles ++; // Incrament pattern cycle count
- }
- lastUpdateTime = millis(); // Mark the update time
- }
- break;
- case 4: // Change to RIGHT ON state
- if(millis() > lastUpdateTime + FLASH_OFF_TIME_SETTING){
- if(flashCount == 1 and colorTwo != off){
- neopixelStrip.fill(colorTwo,1,rightWraps()); // turn off lower RIGHT light
- neopixelStrip.fill(colorTwo,upperMidPoint(),rightWraps()); // turn off Upper RIGHT light
- } else if(flashCount == 2 and colorThree != off){
- neopixelStrip.fill(colorThree,1,rightWraps()); // turn off lower RIGHT light
- neopixelStrip.fill(colorThree,upperMidPoint(),rightWraps()); // turn off Upper RIGHT light
- } else {
- neopixelStrip.fill(colorOne,1,rightWraps()); // turn off lower RIGHT light
- neopixelStrip.fill(colorOne,upperMidPoint(),rightWraps()); // turn off Upper RIGHT light
- }
- currentState = 3; // Update State
- lastUpdateTime = millis(); // Mark the update time
- }
- break;
- }
- if(numOfPatternCycles == patternCyclesToRun){
- return true; // Return True when we have run the requested amount of pattern cycles
- } else {
- return false;
- }
- }
- // Flash strips kiddy corner to each other
- boolean crissCross(byte multiFlash, uint32_t colorOne, uint32_t colorTwo = off, uint32_t colorThree = off, byte patternCyclesToRun = 1){
- //Definitions
- //#define FLASH_ON_TIME_SETTING 900
- //#define FLASH_OFF_TIME_SETTING 50
- // Caculate actual flash time
- if(multiFlash > 1){
- flashOnTime = (FLASH_ON_TIME_SETTING / multiFlash) - (FLASH_OFF_TIME_SETTING * multiFlash);
- } else {
- flashOnTime = FLASH_ON_TIME_SETTING;
- }
- // Check if this pattern has just been activated - Pattern #3
- if(currentPattern != 5){
- currentPattern = 5; // Set to new current patern
- currentState = 0; // Reset state to initial value
- numOfPatternCycles = 0; // Reset number of pattern cycles
- }
- switch(currentState){
- case 0: // Reset to initial UP RIGHT/DOWN LEFT ON state
- neopixelStrip.fill(off,1,lastLedAddress); // clear all values
- neopixelStrip.fill(colorOne,lowerMidPoint(),halfWidth()); // turn ON lower left light
- neopixelStrip.fill(colorOne,upperMidPoint(),halfWidth()); // turn ON upper right light
- lastUpdateTime = millis(); // Mark the update time
- currentState = 1; // Update State
- flashCount = 0; //update flash count
- break;
- case 1: // Change to either UP RIGHT/DOWN LEFT OFF or UP LEFT/DOWN RIGHT ON state
- if(millis() > lastUpdateTime + flashOnTime){
- flashCount ++; // Incrament flash count
- if(multiFlash > 1 and multiFlash != flashCount){
- neopixelStrip.fill(off,1,lastLedAddress); // clear all values
- currentState = 2; // Update State
- } else {
- neopixelStrip.fill(off,1,lastLedAddress); // clear all values
- neopixelStrip.fill(colorOne,lowerRightCorner(),halfWidth()); // turn ON lower right light
- neopixelStrip.fill(colorOne,upperLeftCorner(),halfWidth()); // turn ON upper left light
- currentState = 3; // Update State
- flashCount = 0; // reset flash counter
- }
- lastUpdateTime = millis(); // Mark the update time
- }
- break;
- case 2: // Change to UP RIGHT/DOWN LEFT ON state
- if(millis() > lastUpdateTime + FLASH_OFF_TIME_SETTING){
- if(flashCount == 1 and colorTwo != off){
- neopixelStrip.fill(colorTwo,lowerMidPoint(),halfWidth()); // turn ON lower left light with 2nd color
- neopixelStrip.fill(colorTwo,upperMidPoint(),halfWidth()); // turn ON upper right light with 2nd color
- } else if(flashCount == 2 and colorThree != off){
- neopixelStrip.fill(colorThree,lowerMidPoint(),halfWidth()); // turn ON lower left light with 3rd color
- neopixelStrip.fill(colorThree,upperMidPoint(),halfWidth()); // turn ON upper right light with 3rd color
- } else {
- neopixelStrip.fill(colorOne,lowerMidPoint(),halfWidth()); // turn ON lower left light with 1st color
- neopixelStrip.fill(colorOne,upperMidPoint(),halfWidth()); // turn ON upper right light with 1st color
- }
- currentState = 1; // Update State
- lastUpdateTime = millis(); // Mark the update time
- }
- break;
- case 3: // Change to either UP LEFT/DOWN RIGHT OFF or UP RIGHT/DOWN LEFT ON state
- if(millis() > lastUpdateTime + flashOnTime){
- flashCount ++; // Incrament flash count
- if(multiFlash > 1 and multiFlash != flashCount){
- neopixelStrip.fill(off,1,lastLedAddress); // clear all values
- currentState = 4; // Update State
- } else {
- neopixelStrip.fill(off,1,lastLedAddress); // clear all values
- neopixelStrip.fill(colorOne,lowerMidPoint(),halfWidth()); // turn ON lower left light
- neopixelStrip.fill(colorOne,upperMidPoint(),halfWidth()); // turn ON upper right ligt
- currentState = 1; // Update State
- flashCount = 0; // reset flash counter
- numOfPatternCycles ++; // Incrament pattern cycle count
- }
- lastUpdateTime = millis(); // Mark the update time
- }
- break;
- case 4: // Change to UP LEFT/DOWN RIGHT ON state
- if(millis() > lastUpdateTime + FLASH_OFF_TIME_SETTING){
- if(flashCount == 1 and colorTwo != off){
- neopixelStrip.fill(colorTwo,lowerRightCorner(),halfWidth()); // turn ON lower right light with 2nd color
- neopixelStrip.fill(colorTwo,upperLeftCorner(),halfWidth()); // turn ON upper left light with 2nd color
- } else if(flashCount == 2 and colorThree != off){
- neopixelStrip.fill(colorThree,lowerRightCorner(),halfWidth()); // turn ON lower right light with 3rd color
- neopixelStrip.fill(colorThree,upperLeftCorner(),halfWidth()); // turn ON upper left light with 3rd color
- } else {
- neopixelStrip.fill(colorOne,lowerRightCorner(),halfWidth()); // turn ON lower right light with 1st color
- neopixelStrip.fill(colorOne,upperLeftCorner(),halfWidth()); // turn ON upper left light with 1st color
- }
- currentState = 3; // Update State
- lastUpdateTime = millis(); // Mark the update time
- }
- break;
- }
- if(numOfPatternCycles == patternCyclesToRun){
- return true; // Return True when we have run the requested amount of pattern cycles
- } else {
- return false;
- }
- }
- };
- class RelayDevice {
- private:
- byte pinNumber;
- public:
- // Constructor
- RelayDevice(byte pinNumber){
- this->pinNumber = pinNumber;
- }
- // Methods
- void on(){
- digitalWrite(pinNumber, HIGH);
- }
- void off(){
- digitalWrite(pinNumber, LOW);
- }
- };
- // Build Objects
- RGBLightBar sideLightFrontLeftBar(12, sideLightFrontLeftStrip);
- RGBLightBar sideLightRearLeftBar(12, sideLightRearLeftStrip);
- RGBLightBar sideLightFrontRightBar(12, sideLightFrontRightStrip);
- RGBLightBar sideLightRearRightBar(12, sideLightRearRightStrip);
- RelayDevice RGBLightsRelay(RGB_LIGHTS_RELAY_PIN);
- RelayDevice leftFloodLightsRelay(LEFT_FLOOD_LIGHTS_RELAY_PIN);
- RelayDevice rightFloodLightsRelay(RIGHT_FLOOD_LIGHTS_RELAY_PIN);
- RelayDevice leftChaseLightRelay(LEFT_CHASE_LIGHT_RELAY_PIN);
- RelayDevice rightChaseLightRelay(RIGHT_CHASE_LIGHT_RELAY_PIN);
- void setup() {
- // put your setup code here, to run once:
- // ---I2C Setup---
- Wire.begin(10); // join I2C bus as Slave with address 10
- // event handler initializations
- Wire.onReceive(dataRcv); // register an event handler for received data
- // initialize global variables
- i2c_pattern = 255;
- i2c_option = 255;
- currentPattern = 255;
- currentOption = 255;
- i2c_message_LED_flash_start_timer = millis();
- i2c_message_LED_status = 0;
- // Initialize Strips
- sideLightFrontLeftStrip.setBrightness(RGB_LIGHTS_INITIAL_BRIGHNESS);
- sideLightRearLeftStrip.setBrightness(RGB_LIGHTS_INITIAL_BRIGHNESS);
- sideLightFrontRightStrip.setBrightness(RGB_LIGHTS_INITIAL_BRIGHNESS);
- sideLightRearRightStrip.setBrightness(RGB_LIGHTS_INITIAL_BRIGHNESS);
- sideLightFrontLeftStrip.begin();
- sideLightRearLeftStrip.begin();
- sideLightFrontRightStrip.begin();
- sideLightRearRightStrip.begin();
- sideLightFrontLeftBar.mainLightOff(); // Shut off main light after setup
- sideLightRearLeftBar.mainLightOff(); // Shut off main light after setup
- sideLightFrontRightBar.mainLightOff(); // Shut off main light after setup
- sideLightRearRightBar.mainLightOff(); // Shut off main light after setup
- sideLightFrontLeftBar.solidColor(off); // Shut off RGB ring after setup
- sideLightRearLeftBar.solidColor(off); // Shut off RGB ring after setup
- sideLightFrontRightBar.solidColor(off); // Shut off RGB ring after setup
- sideLightRearRightBar.solidColor(off); // Shut off RGB ring after setup
- // Initialize other outputs
- pinMode(RGB_LIGHTS_RELAY_PIN,OUTPUT); // Setup RGB light bars relay pin
- pinMode(LEFT_FLOOD_LIGHTS_RELAY_PIN,OUTPUT); // Setup left side flood lights relay pin
- pinMode(RIGHT_FLOOD_LIGHTS_RELAY_PIN,OUTPUT); // Setup right side flood lights relay pin
- pinMode(LEFT_CHASE_LIGHT_RELAY_PIN,OUTPUT); // Setup left side chase lights relay pin
- pinMode(RIGHT_CHASE_LIGHT_RELAY_PIN,OUTPUT); // Setup right side chase lights relay pin
- }
- void loop() {
- // put your main code here, to run repeatedly:
- RGBLightsRelay.on(); // Turn on Relay to power RGB light bars
- // I2C message testing
- //i2c_pattern = 255;
- //i2c_option = 255;
- updateOutputs();
- // Turn off message LED
- if((millis() - i2c_message_LED_flash_start_timer) > I2C_MESSAGE_RECEIVED_LED_FLASH_LENGTH){
- i2c_message_LED_status = 0; // Turn off Light
- }
- digitalWrite(13, i2c_message_LED_status); // Update LED State
- // Update all light strips constantly
- sideLightFrontLeftStrip.show();
- sideLightRearLeftStrip.show();
- sideLightFrontRightStrip.show();
- sideLightRearRightStrip.show();
- }
- // --Functions--
- // received data handler function
- void dataRcv(int numBytes){
- while(Wire.available()) { // read all bytes received
- i2c_pattern = Wire.read();
- i2c_option = Wire.read();
- }
- i2c_message_LED_status = 1; // Turn on the LED
- i2c_message_LED_flash_start_timer = millis();
- }
- // Make decisions based on the newly received I2C messages
- void updateOutputs(){
- // Process incoming I2C messages
- switch(i2c_pattern){
- case 0: // RGB Light Bar(s) Power OFF
- // Program for this later. This should kill the relay to all RGB light bars
- break;
- case 1: // RGB Light Bar(s) Power On
- // Program for this later. This should energize the relay to all RGB light bars
- // and set all lights to off
- break;
- case 2: // All lights OFF
- // Reset the current Pattern/Option to the default of 255
- currentPattern = 255;
- currentOption = 255;
- sideLightFrontLeftBar.mainLightOff();
- sideLightRearLeftBar.mainLightOff();
- sideLightFrontRightBar.mainLightOff();
- sideLightRearRightBar.mainLightOff();
- sideLightFrontLeftBar.solidColor(off);
- sideLightRearLeftBar.solidColor(off);
- sideLightFrontRightBar.solidColor(off);
- sideLightRearRightBar.solidColor(off);
- leftFloodLightsRelay.off();
- rightFloodLightsRelay.off();
- leftChaseLightRelay.off();
- rightChaseLightRelay.off();
- break;
- case 3: // All main lights OFF
- sideLightFrontLeftBar.mainLightOff();
- sideLightRearLeftBar.mainLightOff();
- sideLightFrontRightBar.mainLightOff();
- sideLightRearRightBar.mainLightOff();
- leftFloodLightsRelay.off();
- rightFloodLightsRelay.off();
- break;
- case 4: // All main lights ON
- sideLightFrontLeftBar.mainLightOn();
- sideLightRearLeftBar.mainLightOn();
- sideLightFrontRightBar.mainLightOn();
- sideLightRearRightBar.mainLightOn();
- leftFloodLightsRelay.on();
- rightFloodLightsRelay.on();
- break;
- case 5: // Left main lights OFF
- sideLightFrontLeftBar.mainLightOff();
- sideLightRearLeftBar.mainLightOff();
- leftFloodLightsRelay.off();
- break;
- case 6: // Left main lights ON
- sideLightFrontLeftBar.mainLightOn();
- sideLightRearLeftBar.mainLightOn();
- leftFloodLightsRelay.on();
- break;
- case 7: // Right main lights OFF
- sideLightFrontRightBar.mainLightOff();
- sideLightRearRightBar.mainLightOff();
- rightFloodLightsRelay.off();
- break;
- case 8: //Right main lights ON
- sideLightFrontRightBar.mainLightOn();
- sideLightRearRightBar.mainLightOn();
- rightFloodLightsRelay.on();
- break;
- case 9: // Chase lights OFF
- leftChaseLightRelay.off();
- rightChaseLightRelay.off();
- break;
- case 10: // Chase lights ON
- leftChaseLightRelay.on();
- rightChaseLightRelay.on();
- break;
- case 11: // RGB (All) OFF
- // Reset the Current Patern and Option to the default of 255
- currentPattern = 255;
- currentOption = 255;
- sideLightFrontLeftBar.solidColor(off);
- sideLightRearLeftBar.solidColor(off);
- sideLightFrontRightBar.solidColor(off);
- sideLightRearRightBar.solidColor(off);
- break;
- case 12: // RGB (All) Red
- // Add this functionality later
- break;
- case 13: // RGB left Red
- // Add this functionality later
- break;
- case 14: // RGB right Red
- // Add this functionality later
- break;
- case 15: // Turn signal left OFF
- // The board does not do anything with turn signals
- break;
- case 16: // Turn signal left ON
- // The board does not do anything with turn signals
- break;
- case 17: // Turn signal right OFF
- // The board does not do anything with turn signals
- break;
- case 18: // Turn signal right ON
- // The board does not do anything with turn signals
- break;
- case 19: // Brake OFF
- // The board does not do anything with brakes
- break;
- case 20: // Brake ON
- // The board does not do anything with brakes
- break;
- case 21: // RGB caution pattern cycle
- // Save the Current Pattern and Option. Continu Processing below
- currentPattern = i2c_pattern;
- currentOption = i2c_option;
- break;
- case 22: // RBG hazard left
- // The board does not do anything with hazard left signals
- break;
- case 23: // RGB hazard right
- // The board does not do anything with hazard right signals
- break;
- case 24: // RGB hazard center
- // The board does not do anything with hazard center signals
- break;
- case 100: // RGB (all) Solid
- // Save the Current Pattern and Option. Continue processing below
- currentPattern = i2c_pattern;
- currentOption = i2c_option;
- break;
- case 101: // RGB (all) Full Jump Change Colors
- // Program this later
- break;
- case 102: // RGB (all) Full fade change color
- // Program this later
- break;
- case 103: // RGB (all) Full fade out change colors
- // Program this later
- break;
- case 104: // RGB (all) Chase Fade
- // Program this later
- break;
- case 105: // RGB (all) chase fade out
- // Program this later
- break;
- case 106: // Rainbow Road- 80 Full Speed | 81 Fast | 82 Moderate | 83 Slow
- // Save the Current Pattern and Option. Continue processing below
- currentPattern = i2c_pattern;
- currentOption = i2c_option;
- break;
- default:
- // If I2C message is set to the defualt 255 then do nothing
- break;
- }
- // Reset I2C variables
- i2c_pattern = 255;
- i2c_option = 255;
- // Process constant patterns
- switch(currentPattern){
- case 21: // RGB caution pattern cycle
- sideLightFrontLeftBar.cautionPatternCycle();
- sideLightRearLeftBar.cautionPatternCycle();
- sideLightFrontRightBar.cautionPatternCycle();
- sideLightRearRightBar.cautionPatternCycle();
- break;
- case 100: // RGB (all) Solid
- switch(currentOption){
- case 0: // OFF
- sideLightFrontLeftBar.solidColor(off);
- sideLightRearLeftBar.solidColor(off);
- sideLightFrontRightBar.solidColor(off);
- sideLightRearRightBar.solidColor(off);
- break;
- case 1: // White
- sideLightFrontLeftBar.solidColor(white);
- sideLightRearLeftBar.solidColor(white);
- sideLightFrontRightBar.solidColor(white);
- sideLightRearRightBar.solidColor(white);
- break;
- case 2: // Red
- sideLightFrontLeftBar.solidColor(red);
- sideLightRearLeftBar.solidColor(red);
- sideLightFrontRightBar.solidColor(red);
- sideLightRearRightBar.solidColor(red);
- break;
- case 3: // Green
- sideLightFrontLeftBar.solidColor(green);
- sideLightRearLeftBar.solidColor(green);
- sideLightFrontRightBar.solidColor(green);
- sideLightRearRightBar.solidColor(green);
- break;
- case 4: // Blue
- sideLightFrontLeftBar.solidColor(blue);
- sideLightRearLeftBar.solidColor(blue);
- sideLightFrontRightBar.solidColor(blue);
- sideLightRearRightBar.solidColor(blue);
- break;
- case 5: // Orange
- sideLightFrontLeftBar.solidColor(orange);
- sideLightRearLeftBar.solidColor(orange);
- sideLightFrontRightBar.solidColor(orange);
- sideLightRearRightBar.solidColor(orange);
- break;
- case 6: // Yellow
- sideLightFrontLeftBar.solidColor(yellow);
- sideLightRearLeftBar.solidColor(yellow);
- sideLightFrontRightBar.solidColor(yellow);
- sideLightRearRightBar.solidColor(yellow);
- break;
- case 7: // Purple
- sideLightFrontLeftBar.solidColor(purple);
- sideLightRearLeftBar.solidColor(purple);
- sideLightFrontRightBar.solidColor(purple);
- sideLightRearRightBar.solidColor(purple);
- break;
- }
- break;
- case 106: // Rainbow Road- 80 Full Speed | 81 Fast | 82 Moderate | 83 Slow
- switch(currentOption){
- case 80: // Full Speed
- sideLightFrontLeftBar.rainbow(0);
- sideLightRearLeftBar.rainbow(0);
- sideLightFrontRightBar.rainbow(0);
- sideLightRearRightBar.rainbow(0);
- break;
- case 81: // Fast
- sideLightFrontLeftBar.rainbow(1);
- sideLightRearLeftBar.rainbow(1);
- sideLightFrontRightBar.rainbow(1);
- sideLightRearRightBar.rainbow(1);
- break;
- case 82: // Moderate
- sideLightFrontLeftBar.rainbow(2);
- sideLightRearLeftBar.rainbow(2);
- sideLightFrontRightBar.rainbow(2);
- sideLightRearRightBar.rainbow(2);
- break;
- case 83: // Slow
- sideLightFrontLeftBar.rainbow(3);
- sideLightRearLeftBar.rainbow(3);
- sideLightFrontRightBar.rainbow(3);
- sideLightRearRightBar.rainbow(3);
- break;
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement