Notifications
Clear all

Building Lighting


Photo Bud
(@photo-bud)
Estimable Member
Joined: 3 years ago
Posts: 111
Topic starter  

This is a test project to learn how to use both the Adafruit Trinket and NeoPixel lighting.

This is a DPM Freight Depot with two NeoPixel leds in main room (Warehouse Front and Rear), one in second room (Priority Ship), and one in the Office. I have added print statements to ascertain validity of parameters. The approach is to assign a random duration of both on and off status, but within certain limits. This is much shorter than would be used on the layout to better see the effect. For example, the office on duration is from 3 to 11 seconds and the off duration is only 2 to 3 seconds. By varying the duration of each, an overall random distribution is achieved. After the code, there is a listing of the test results.

I realize a LOT of optimization could be achieved, but am not interested in that at this time. A video (not reflecting the test results shown below) is included. Windows to left are Priority Ship, next to the right is the Office, and window to far right is the Warehouse.

Thanks for looking!

[code]
// DPM Freight Depot - NeoPixel test program.

// NEOPIXEL BEST PRACTICES for most reliable operation:
// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
//   connect GROUND (-) first, then +, then data.
// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
//   a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
// (Skipping these may work OK on your workbench but can fail in the field)

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    3

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 4

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 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)

long randNumber;

  // Time Variables
  unsigned long currtime;
  int newdur;

  // LED Info
  int warehouserear = 0;
  int warehousefrnt = 1;
  int priorityship  = 2;
  int office        = 3;

  // Duration min/max constants
  int whrOffmin = 3000; int whrOffmax = 4000;
  int whfOffmin = 3000; int whfOffmax = 4000;
  int priOffmin = 1000; int priOffmax = 7000;
  int ofcOffmin = 2000; int ofcOffmax = 3000;
 
  int whrOnmin = 3000; int whrOnmax = 7000;
  int whfOnmin = 3000; int whfOnmax = 7000;
  int priOnmin = 1000; int priOnmax = 9000;
  int ofcOnmin = 3000; int ofcOnmax = 11000;

  int whrlightstat;
  unsigned long  whrswaplim;
  int whflightstat;
  unsigned long  whfswaplim;
  int prilightstat;
  unsigned long  priswaplim;
  int ofclightstat;
  unsigned long  ofcswaplim;

  unsigned long nxtchg;
 

// other constants
 
// setup() function -- runs once at startup --------------------------------

void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.

  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(125); // Set BRIGHTNESS to about 1/2 (max = 255)

  whrlightstat = 1;
  whflightstat = 1;
  prilightstat = 1;
  ofclightstat = 0;

  currtime = millis();  

  Serial.begin(9600);

  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
 
  randomSeed(analogRead(0));

  // Syntax
  // random(max)
  // random(min, max)
 
}

// loop() function -- runs repeatedly as long as board is on ---------------

void loop() {

  currtime = millis();
        
  if (currtime >= whrswaplim) {
    if (whrlightstat == 1) {
      swaplight(warehouserear, 0);
      getnextchg(whrOffmin, whrOffmax);
      whrlightstat = 0;
      Serial.print ("Turn off Warehouse Rear");
      Serial.println();
    }
    else {
      swaplight(warehouserear, 255);
      getnextchg(whrOnmin, whrOnmax);
      whrlightstat = 1;
      Serial.print("Turn on Warehouse Rear");
      Serial.println();

    }
    whrswaplim = nxtchg;
  }
  if (currtime >= whfswaplim) {
    if (whflightstat == 1) {
      swaplight(warehousefrnt, 0);
      getnextchg(whfOffmin, whfOffmax);
      whflightstat = 0;
      Serial.print("Turn off Warehouse Front");
      Serial.println();
    }
    else {
      swaplight(warehousefrnt, 255);
      getnextchg(whfOnmin, whfOnmax);
      whflightstat = 1;
      Serial.print("Turn on Warehouse Front");
      Serial.println();
    }
    whfswaplim = nxtchg;
  }
  if (currtime >= priswaplim) {
    if (prilightstat == 1) {
      swaplight(priorityship, 0);
      getnextchg(priOffmin, priOffmax);
      prilightstat = 0;
      Serial.print("Turn off Priority Ship");
      Serial.println();
    }
    else {
      swaplight(priorityship, 255);
      getnextchg(priOnmin, priOnmax);
      prilightstat = 1;
      Serial.print("Turn on Priority Ship");
      Serial.println();
    }
    priswaplim = nxtchg;
  }
  if (currtime >= ofcswaplim) {
    if (ofclightstat == 1) {
      swaplight(office, 0);
      getnextchg(ofcOffmin, ofcOffmax);
      ofclightstat = 0;
      Serial.print("Turn off Office");
      Serial.println();
    }
    else {
      swaplight(office, 255);
      getnextchg(ofcOnmin, ofcOnmax);
      ofclightstat = 1;
      Serial.print("Turn on Office");
      Serial.println();
    }
    ofcswaplim = nxtchg;
  }
}

void swaplight(int lightnum, int color){
  strip.setPixelColor(lightnum, color, color, color);
  strip.show();
}

void getnextchg(int minamt, int maxamt) {
  newdur = random(minamt, maxamt);
  nxtchg = currtime + newdur;
}
[/code]

 

Example of test run. Note that Office is turned on when board powers up.

0022.618 -> Turn on Priority Ship
0023.634 -> Turn on Warehouse Front
0023.939 -> Turn on Warehouse Rear
0024.313 -> Turn off Office
0027.091 -> Turn on Office
0028.309 -> Turn off Warehouse Front
0030.140 -> Turn off Warehouse Rear
0030.379 -> Turn off Priority Ship
0031.322 -> Turn on Warehouse Front
0033.618 -> Turn on Warehouse Rear
0034.767 -> Turn off Office
0036.086 -> Turn on Priority Ship
0037.034 -> Turn on Office
0037.742 -> Turn off Warehouse Front
0038.180 -> Turn off Warehouse Rear
0038.787 -> Turn off Priority Ship
0041.128 -> Turn on Warehouse Front
0042.074 -> Turn on Warehouse Rear
0042.176 -> Turn off Office
0044.441 -> Turn on Office
0045.151 -> Turn on Priority Ship
0046.296 -> Turn off Warehouse Front
0047.787 -> Turn off Warehouse Rear
0049.479 -> Turn on Warehouse Front
0050.936 -> Turn off Priority Ship
0051.174 -> Turn on Warehouse Rear
0052.499 -> Turn off Office
0054.743 -> Turn on Office
0054.777 -> Turn off Warehouse Front
0055.189 -> Turn off Warehouse Rear
0056.977 -> Turn on Priority Ship
0057.958 -> Turn off Office
0058.026 -> Turn on Warehouse Front
0058.500 -> Turn off Priority Ship
0059.073 -> Turn on Warehouse Rear
0000.634 -> Turn on Office
0002.117 -> Turn off Warehouse Front
0003.033 -> Turn off Warehouse Rear
0004.280 -> Turn on Priority Ship
0005.366 -> Turn on Warehouse Front
0006.955 -> Turn on Warehouse Rear
0010.039 -> Turn off Office
0012.033 -> Turn off Warehouse Front
0012.827 -> Turn on Office
0012.861 -> Turn off Warehouse Rear
0012.861 -> Turn off Priority Ship
0013.925 -> Turn on Priority Ship
0015.763 -> Turn on Warehouse Front
0016.607 -> Turn on Warehouse Rear
0018.430 -> Turn off Office
0018.842 -> Turn off Warehouse Front
0019.967 -> Turn off Warehouse Rear
0020.611 -> Turn on Office
0021.926 -> Turn on Warehouse Front
0022.465 -> Turn off Priority Ship
0023.449 -> Turn on Warehouse Rear
0023.820 -> Turn on Priority Ship
0024.572 -> Turn off Office
0025.458 -> Turn off Warehouse Front
0027.041 -> Turn on Office
0028.857 -> Turn off Warehouse Rear
0028.957 -> Turn off Priority Ship
0029.024 -> Turn on Warehouse Front
0032.034 -> Turn off Office
0032.375 -> Turn off Warehouse Front
0032.846 -> Turn on Warehouse Rear
0034.393 -> Turn on Office
0035.001 -> Turn on Priority Ship
0035.813 -> Turn on Warehouse Front
0037.032 -> Turn off Priority Ship
0037.302 -> Turn off Warehouse Rear
0040.426 -> Turn on Priority Ship
0040.732 -> Turn on Warehouse Rear
0040.968 -> Turn off Warehouse Front
0042.628 -> Turn off Priority Ship
0044.010 -> Turn on Warehouse Front
0044.214 -> Turn off Office
0046.617 -> Turn on Priority Ship
0046.819 -> Turn off Warehouse Rear
0047.194 -> Turn on Office
0048.040 -> Turn off Priority Ship
0050.083 -> Turn on Warehouse Rear
0050.114 -> Turn off Warehouse Front
0053.149 -> Turn on Warehouse Front
0054.566 -> Turn on Priority Ship
0054.733 -> Turn off Warehouse Rear
0056.197 -> Turn off Office
0058.267 -> Turn off Priority Ship
0058.506 -> Turn off Warehouse Front
0058.674 -> Turn on Warehouse Rear
0058.843 -> Turn on Office
0059.826 -> Turn on Priority Ship
0001.982 -> Turn on Warehouse Front
0003.339 -> Turn off Warehouse Rear
0005.059 -> Turn off Office
0005.473 -> Turn off Priority Ship
0007.235 -> Turn off Warehouse Front
0007.337 -> Turn on Warehouse Rear
0007.472 -> Turn on Office
0010.731 -> Turn off Warehouse Rear
0010.936 -> Turn on Priority Ship
0011.006 -> Turn on Warehouse Front
0014.513 -> Turn on Warehouse Rear
0015.160 -> Turn off Warehouse Front
0017.145 -> Turn off Priority Ship
0018.329 -> Turn off Office
0018.801 -> Turn on Warehouse Front
0019.378 -> Turn off Warehouse Rear
0020.658 -> Turn on Office
0022.481 -> Turn on Warehouse Rear
0022.514 -> Turn off Warehouse Front
0022.548 -> Turn on Priority Ship
0025.318 -> Turn off Office
0025.895 -> Turn off Warehouse Rear
0026.336 -> Turn on Warehouse Front
0027.921 -> Turn on Office
0028.292 -> Turn off Priority Ship
0029.313 -> Turn on Warehouse Rear
0032.253 -> Turn off Warehouse Front
0033.912 -> Turn on Priority Ship
0034.317 -> Turn off Warehouse Rear
0034.757 -> Turn off Office
0035.400 -> Turn on Warehouse Front
0037.384 -> Turn on Office
0037.756 -> Turn on Warehouse Rear
0039.272 -> Turn off Priority Ship
0040.758 -> Turn off Warehouse Front
0041.676 -> Turn off Office
0041.917 -> Turn off Warehouse Rear
0043.072 -> Turn on Priority Ship
0044.292 -> Turn on Office
0044.393 -> Turn on Warehouse Front
0044.970 -> Turn on Warehouse Rear
0048.917 -> Turn off Warehouse Rear
0050.549 -> Turn off Warehouse Front
0052.005 -> Turn off Priority Ship
0052.375 -> Turn on Warehouse Rear
0053.792 -> Turn on Priority Ship
0054.170 -> Turn off Office
0054.511 -> Turn on Warehouse Front
0054.915 -> Turn off Priority Ship
0056.723 -> Turn on Office
0057.669 -> Turn off Warehouse Rear
0000.205 -> Turn on Priority Ship
0001.117 -> Turn on Warehouse Rear
0001.457 -> Turn off Warehouse Front
0004.129 -> Turn off Office
0004.164 -> Turn off Warehouse Rear
0004.774 -> Turn on Warehouse Front
0006.224 -> Turn on Office
0008.079 -> Turn on Warehouse Rear
0008.551 -> Turn off Priority Ship
0009.740 -> Turn on Priority Ship
0010.976 -> Turn off Warehouse Front
0012.628 -> Turn off Warehouse Rear
0013.248 -> Turn off Office
0014.007 -> Turn on Warehouse Front
0014.109 -> Turn off Priority Ship
0016.008 -> Turn on Office
0016.614 -> Turn on Warehouse Rear
0018.277 -> Turn off Warehouse Front
0020.512 -> Turn on Priority Ship
0021.561 -> Turn off Warehouse Rear
0022.133 -> Turn on Warehouse Front
0023.921 -> Turn off Office
0025.279 -> Turn on Warehouse Rear
0026.059 -> Turn off Warehouse Front
0026.633 -> Turn on Office
0029.172 -> Turn on Warehouse Front
0029.481 -> Turn off Priority Ship
0032.143 -> Turn off Warehouse Rear
0033.022 -> Turn off Warehouse Front
0034.209 -> Turn off Office
0035.023 -> Turn on Priority Ship
0036.041 -> Turn on Warehouse Rear
0036.579 -> Turn on Office
0036.953 -> Turn on Warehouse Front
0040.034 -> Turn off Warehouse Rear
0040.337 -> Turn off Priority Ship
0040.709 -> Turn off Warehouse Front
0043.069 -> Turn on Warehouse Rear
0044.016 -> Turn on Warehouse Front
0045.740 -> Turn on Priority Ship
0046.482 -> Turn off Office
0048.081 -> Turn off Warehouse Rear
0048.895 -> Turn on Office
0049.844 -> Turn off Warehouse Front
0051.503 -> Turn on Warehouse Rear
0053.121 -> Turn on Warehouse Front
0054.439 -> Turn off Priority Ship
0056.146 -> Turn off Warehouse Rear
0057.698 -> Turn off Warehouse Front
0057.935 -> Turn on Priority Ship
0059.430 -> Turn on Warehouse Rear
0059.601 -> Turn off Office
0001.595 -> Turn on Warehouse Front
0002.405 -> Turn on Office
0004.245 -> Turn off Priority Ship
0004.615 -> Turn off Warehouse Rear
0006.172 -> Turn off Warehouse Front
0008.197 -> Turn on Warehouse Rear
0009.790 -> Turn off Office
0009.858 -> Turn on Warehouse Front
0010.682 -> Turn on Priority Ship
0012.540 -> Turn off Priority Ship
0012.679 -> Turn on Office
0013.194 -> Turn off Warehouse Rear
0014.232 -> Turn off Warehouse Front
0016.246 -> Turn on Priority Ship
0016.618 -> Turn on Warehouse Rear
0017.323 -> Turn on Warehouse Front
0019.043 -> Turn off Office
0020.847 -> Turn off Warehouse Front
0020.982 -> Turn off Warehouse Rear
0021.692 -> Turn on Office
0024.381 -> Turn on Warehouse Front
0024.722 -> Turn on Warehouse Rear
0025.027 -> Turn off Priority Ship
0027.652 -> Turn off Office
0027.754 -> Turn off Warehouse Rear
0027.959 -> Turn off Warehouse Front
0029.859 -> Turn on Office
0030.876 -> Turn on Warehouse Rear
0031.416 -> Turn on Warehouse Front
0031.552 -> Turn on Priority Ship
0033.215 -> Turn off Office
0035.722 -> Turn on Office
0037.145 -> Turn off Warehouse Rear
0037.315 -> Turn off Warehouse Front
0037.551 -> Turn off Priority Ship
0039.178 -> Turn off Office
0040.429 -> Turn on Warehouse Rear
0040.634 -> Turn on Warehouse Front
0041.539 -> Turn on Office
0043.588 -> Turn on Priority Ship
0045.943 -> Turn off Warehouse Rear
0046.079 -> Turn off Warehouse Front
0048.959 -> Turn on Warehouse Rear
0049.505 -> Turn on Warehouse Front
0049.842 -> Turn off Office
0050.993 -> Turn off Priority Ship
0052.176 -> Turn on Office
0054.342 -> Turn off Warehouse Rear
0056.031 -> Turn off Warehouse Front
0056.605 -> Turn on Priority Ship
0057.413 -> Turn on Warehouse Rear
0059.413 -> Turn off Office
0059.581 -> Turn on Warehouse Front
0001.644 -> Turn on Office
0001.677 -> Turn off Warehouse Rear
0003.024 -> Turn off Priority Ship
0003.467 -> Turn off Warehouse Front
0005.230 -> Turn on Warehouse Rear
0005.298 -> Turn off Office
0006.277 -> Turn on Priority Ship
0006.785 -> Turn on Warehouse Front
0007.768 -> Turn on Office
0009.124 -> Turn off Priority Ship
0010.009 -> Turn off Warehouse Front
0010.765 -> Turn off Warehouse Rear
0012.866 -> Turn off Office
0013.142 -> Turn on Warehouse Front
0014.204 -> Turn on Warehouse Rear
0014.343 -> Turn on Priority Ship
0015.022 -> Turn on Office
0019.235 -> Turn off Warehouse Front
0019.409 -> Turn off Office
0020.759 -> Turn off Priority Ship
0020.994 -> Turn off Warehouse Rear
0021.466 -> Turn on Office
0022.309 -> Turn on Warehouse Front
0022.754 -> Turn on Priority Ship
0024.374 -> Turn on Warehouse Rear
0027.985 -> Turn off Priority Ship
0028.695 -> Turn off Warehouse Front
0028.865 -> Turn off Office
0029.032 -> Turn off Warehouse Rear
0031.238 -> Turn on Office
0031.884 -> Turn on Priority Ship
0032.288 -> Turn on Warehouse Rear
0032.523 -> Turn on Warehouse Front
0037.522 -> Turn off Warehouse Rear
0038.165 -> Turn off Warehouse Front
0039.926 -> Turn off Office

Photo Bud (aka John)
The Old Curmudgeon!


Quote
Robo Pi
(@robo-pi)
Noble Member
Joined: 3 years ago
Posts: 1909
 

Nice work John!   It's always nice to see progress on a project.    Glad to see things are working out for you.

DroneBot Workshop Robotics Engineer
James


Photo Bud liked
ReplyQuote
SolitaryLeaf
(@solitaryleaf)
Active Member
Joined: 2 years ago
Posts: 11
 

Thats pretty cool!

 


ReplyQuote