Anthony Komar
Published

Shed Lighting System and Project Box

I wanted a nice box to complement my Arduino based lighting system for a backyard He/She Shed.

BeginnerShowcase (no instructions)20 hours497
Shed Lighting System and Project Box

Story

Read more

Code

NeoPixel Lighting System for She/He Shed

Arduino
This code uses Arduino 101 to control NeoPixel String of 80 LEDs.
#include <Adafruit_NeoPixel.h>
#include <CurieBLE.h>

#define PIN 6

const int ledPin = 13; // set ledPin to use on-board LED
const char A = '1';//1   ColorWipeRedGreenBlue
const char B = '2';//2    theaterChaseWithRedBlue
const char C = '3';//3    rainbow
const char D = '4';//4    rainbowCycle
const char E = '5';//5    starsandstripes
const char F = '6';//6    theaterChaseRainbow
const char S = '0';//Stop  colorWipeOff
const char V = '7';//Dim   
const char P = '9';//Play  colorWipeWhite

char event = S;   // variable to hold a transmitted byte set to 0 
String state = "starsandstripes"; 

float brightness = 1.0; // Value can be 0 to 1.0



BLEPeripheral blePeripheral;  // BLE Peripheral Device (the board you're programming)

// ====  create Nordic Semiconductor UART service =========
BLEService uartService = BLEService("6E400001B5A3F393E0A9E50E24DCCA9E");
// create characteristics
BLECharacteristic rxCharacteristic = BLECharacteristic("6E400002B5A3F393E0A9E50E24DCCA9E", BLEWriteWithoutResponse, 20);  // == TX on central (android app)
BLECharacteristic txCharacteristic = BLECharacteristic("6E400003B5A3F393E0A9E50E24DCCA9E", BLENotify , 20); // == RX on central (android app)


// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(70, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

void setup() {

  
  Serial.begin(19200);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

   
  // set advertised local name and service UUID:
  blePeripheral.setLocalName("BLE_ROV");
  blePeripheral.setAdvertisedServiceUuid(uartService.uuid());

  // add service, rx and tx characteristics:
  blePeripheral.addAttribute(uartService);
  blePeripheral.addAttribute(rxCharacteristic);
  blePeripheral.addAttribute(txCharacteristic);

  // assign event handlers for connected, disconnected to periphera  blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
  blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);

  // assign event handler for characteristic
  rxCharacteristic.setEventHandler(BLEWritten, rxCharacteristicWritten);

  // advertise the service
  blePeripheral.begin();
  event = D;
}

void loop() { 

 
  // poll ble peripheral
  blePeripheral.poll();  

  
 


 if (state == "ColorWipeRedGreenBlue")
    {  
             Serial.println("ColorWipeRedGreenBlue");
            // Some example procedures showing how to display to the pixels:
            colorWipe(strip.Color(255, 0, 0), 50); // Red
            colorWipe(strip.Color(0, 255, 0), 50); // Green
            colorWipe(strip.Color(0, 0, 255), 50); // Blue
          //colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW     
           colorWipe(strip.Color(0, 0, 0), 50); // off
              
              event = S; 
    }  else if (state == "theaterChaseRedBlue"  )
    {
            Serial.println("theaterChaseRedBlue");
          theaterChase(strip.Color(127, 127, 127), 50); // White
          theaterChase(strip.Color(127, 0, 0), 50); // Red
          theaterChase(strip.Color(0, 0, 127), 50); // Blue
          colorWipe(strip.Color(0, 0, 0), 50); // off
          event = S;      
        
    }  else if (state == "rainbow"  )    {
          Serial.println("rainbow");
         rainbow(20);
        colorWipe(strip.Color(0, 0, 0), 50); // off
            event = S;   
               
    } else if (state == "rainbowCycle" )   {
               Serial.println("rainbowCycle");
            rainbowCycle(50);
      
               
    }  else if (state == "starsandstripes"  )    {
             Serial.println("starsandstripes");
         // theaterChase(strip.Color(127, 127, 127), 50); // White
        //  theaterChase(strip.Color(127, 0, 0), 50); // Red
        //theaterChase(strip.Color(0, 0, 127), 50); // Blue
         StarsStripeCycle(50);
         
       
    }  else if (state == "theaterChaseRainbow" )  {
             Serial.println("theaterChaseRainbow");
            theaterChaseRainbow(50);
            colorWipe(strip.Color(0, 0, 0), 50); // off
          
            event = S;     
        
    }  else if (state == "colorWipeOff"  )
    {
         //_mStop();
         Serial.println("colorWipeOff");
        colorWipe(strip.Color(0, 0, 0), 50); // off
         event = S;      
     
    }  else if (state == "colorWipeWhite"  )
    {
         //_mStop();
         Serial.println("colorWipeWhite");
        colorWipe(strip.Color(255, 255, 255), 50); // off
         event = S;     
    }
      
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}




void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, DimmerWheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void StarsStripeCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, StarsStripeWheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}



// Input a value 0 to 255 to get a color value.
//Use global variable to dim the wheel brighness
// The colours are a transition r - g - b - back to r.
uint32_t StarsStripeWheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color( int(brightness *(255 - WheelPos * 3)), 0, 0);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(int(brightness *(127 - WheelPos * 3)), int(brightness *(127 - WheelPos * 3)), int(brightness *(127 - WheelPos * 3)));
  }
  WheelPos -= 170;
  return strip.Color( 0,0 , int(brightness *(255 - WheelPos * 3)));
}

// Input a value 0 to 255 to get a color value.
//Use global variable to dim the wheel brighness
// The colours are a transition r - g - b - back to r.
uint32_t ItailianWheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color( int(brightness *(255 - WheelPos * 3)), 0, 0);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(int(brightness *(127 - WheelPos * 3)), int(brightness *(127 - WheelPos * 3)), int(brightness *(127 - WheelPos * 3)));
  }
  WheelPos -= 170;
  return strip.Color( 0, int(brightness *(255 - WheelPos * 3)), 0);
}

// Input a value 0 to 255 to get a color value.
//Use global variable to dim the wheel brighness
// The colours are a transition r - g - b - back to r.
uint32_t DimmerWheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color( int(brightness *(255 - WheelPos * 3)), 0, int(brightness* (WheelPos * 3)));
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, int(brightness *(WheelPos * 3)), int(brightness *(255 - WheelPos * 3)));
  }
  WheelPos -= 170;
  return strip.Color(int(brightness * (WheelPos * 3)), int(brightness *(255 - WheelPos * 3)), 0);
}

void blePeripheralConnectHandler(BLECentral& central) {
  // central connected event handler
  Serial.print("Connected event, central: ");
  Serial.println(central.address());
  //Serial.println("LED on");
  digitalWrite(ledPin, HIGH);
}

void blePeripheralDisconnectHandler(BLECentral& central) {
  // central disconnected event handler
  Serial.print("Disconnected event, central: ");
  Serial.println(central.address());
  //Serial.println("LED off");
  digitalWrite(ledPin, LOW);
}

void rxCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
  // central wrote new value to characteristic, update LED
  Serial.print("Characteristic event, written: ");
  
  if (characteristic.value()) {       //null pointer check
    event = *characteristic.value();  //set state to be the value written from the phone/tablet to the Arduino 101
    Serial.println(char(event));      //print out the character to the serial monitor
  if (event == A)
    {  
            state = "ColorWipeRedGreenBlue";
 
    }  else if (event == B  )
    {
          state = "theaterChaseRedBlue";
             
        
    }  else if (event == C  )    {
        state = "rainbow"; 
               
    } else if (event == D )   {
             state = "rainbowCycle";
         
    }  else if (event == E  )    {
           state = "starsandstripes";
 
    }  else if (event == F )  {
             state = "theaterChaseRainbow";
   
    }  else if (event == S  ){
         
        state = "colorWipeOff" ;
         
     
    }  else if (event == P  ){
         
         state = "colorWipeWhite";
            
     }  else if (event == V  ){
         // Dim all the lights.
         if ( brightness <= 0) {
          brightness = 1.0;
          }
          else { 
            brightness = brightness - .20;
          }       
         
    }    
  } 
}

Credits

Anthony Komar
0 projects • 0 followers

Comments