Building Lighting  

  RSS

Photo Bud
(@photo-bud)
Eminent Member
Joined: 9 months ago
Posts: 39
2020-01-14 4:41 am  

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.

00:18:22.618 -> Turn on Priority Ship
00:18:23.634 -> Turn on Warehouse Front
00:18:23.939 -> Turn on Warehouse Rear
00:18:24.313 -> Turn off Office
00:18:27.091 -> Turn on Office
00:18:28.309 -> Turn off Warehouse Front
00:18:30.140 -> Turn off Warehouse Rear
00:18:30.379 -> Turn off Priority Ship
00:18:31.322 -> Turn on Warehouse Front
00:18:33.618 -> Turn on Warehouse Rear
00:18:34.767 -> Turn off Office
00:18:36.086 -> Turn on Priority Ship
00:18:37.034 -> Turn on Office
00:18:37.742 -> Turn off Warehouse Front
00:18:38.180 -> Turn off Warehouse Rear
00:18:38.787 -> Turn off Priority Ship
00:18:41.128 -> Turn on Warehouse Front
00:18:42.074 -> Turn on Warehouse Rear
00:18:42.176 -> Turn off Office
00:18:44.441 -> Turn on Office
00:18:45.151 -> Turn on Priority Ship
00:18:46.296 -> Turn off Warehouse Front
00:18:47.787 -> Turn off Warehouse Rear
00:18:49.479 -> Turn on Warehouse Front
00:18:50.936 -> Turn off Priority Ship
00:18:51.174 -> Turn on Warehouse Rear
00:18:52.499 -> Turn off Office
00:18:54.743 -> Turn on Office
00:18:54.777 -> Turn off Warehouse Front
00:18:55.189 -> Turn off Warehouse Rear
00:18:56.977 -> Turn on Priority Ship
00:18:57.958 -> Turn off Office
00:18:58.026 -> Turn on Warehouse Front
00:18:58.500 -> Turn off Priority Ship
00:18:59.073 -> Turn on Warehouse Rear
00:19:00.634 -> Turn on Office
00:19:02.117 -> Turn off Warehouse Front
00:19:03.033 -> Turn off Warehouse Rear
00:19:04.280 -> Turn on Priority Ship
00:19:05.366 -> Turn on Warehouse Front
00:19:06.955 -> Turn on Warehouse Rear
00:19:10.039 -> Turn off Office
00:19:12.033 -> Turn off Warehouse Front
00:19:12.827 -> Turn on Office
00:19:12.861 -> Turn off Warehouse Rear
00:19:12.861 -> Turn off Priority Ship
00:19:13.925 -> Turn on Priority Ship
00:19:15.763 -> Turn on Warehouse Front
00:19:16.607 -> Turn on Warehouse Rear
00:19:18.430 -> Turn off Office
00:19:18.842 -> Turn off Warehouse Front
00:19:19.967 -> Turn off Warehouse Rear
00:19:20.611 -> Turn on Office
00:19:21.926 -> Turn on Warehouse Front
00:19:22.465 -> Turn off Priority Ship
00:19:23.449 -> Turn on Warehouse Rear
00:19:23.820 -> Turn on Priority Ship
00:19:24.572 -> Turn off Office
00:19:25.458 -> Turn off Warehouse Front
00:19:27.041 -> Turn on Office
00:19:28.857 -> Turn off Warehouse Rear
00:19:28.957 -> Turn off Priority Ship
00:19:29.024 -> Turn on Warehouse Front
00:19:32.034 -> Turn off Office
00:19:32.375 -> Turn off Warehouse Front
00:19:32.846 -> Turn on Warehouse Rear
00:19:34.393 -> Turn on Office
00:19:35.001 -> Turn on Priority Ship
00:19:35.813 -> Turn on Warehouse Front
00:19:37.032 -> Turn off Priority Ship
00:19:37.302 -> Turn off Warehouse Rear
00:19:40.426 -> Turn on Priority Ship
00:19:40.732 -> Turn on Warehouse Rear
00:19:40.968 -> Turn off Warehouse Front
00:19:42.628 -> Turn off Priority Ship
00:19:44.010 -> Turn on Warehouse Front
00:19:44.214 -> Turn off Office
00:19:46.617 -> Turn on Priority Ship
00:19:46.819 -> Turn off Warehouse Rear
00:19:47.194 -> Turn on Office
00:19:48.040 -> Turn off Priority Ship
00:19:50.083 -> Turn on Warehouse Rear
00:19:50.114 -> Turn off Warehouse Front
00:19:53.149 -> Turn on Warehouse Front
00:19:54.566 -> Turn on Priority Ship
00:19:54.733 -> Turn off Warehouse Rear
00:19:56.197 -> Turn off Office
00:19:58.267 -> Turn off Priority Ship
00:19:58.506 -> Turn off Warehouse Front
00:19:58.674 -> Turn on Warehouse Rear
00:19:58.843 -> Turn on Office
00:19:59.826 -> Turn on Priority Ship
00:20:01.982 -> Turn on Warehouse Front
00:20:03.339 -> Turn off Warehouse Rear
00:20:05.059 -> Turn off Office
00:20:05.473 -> Turn off Priority Ship
00:20:07.235 -> Turn off Warehouse Front
00:20:07.337 -> Turn on Warehouse Rear
00:20:07.472 -> Turn on Office
00:20:10.731 -> Turn off Warehouse Rear
00:20:10.936 -> Turn on Priority Ship
00:20:11.006 -> Turn on Warehouse Front
00:20:14.513 -> Turn on Warehouse Rear
00:20:15.160 -> Turn off Warehouse Front
00:20:17.145 -> Turn off Priority Ship
00:20:18.329 -> Turn off Office
00:20:18.801 -> Turn on Warehouse Front
00:20:19.378 -> Turn off Warehouse Rear
00:20:20.658 -> Turn on Office
00:20:22.481 -> Turn on Warehouse Rear
00:20:22.514 -> Turn off Warehouse Front
00:20:22.548 -> Turn on Priority Ship
00:20:25.318 -> Turn off Office
00:20:25.895 -> Turn off Warehouse Rear
00:20:26.336 -> Turn on Warehouse Front
00:20:27.921 -> Turn on Office
00:20:28.292 -> Turn off Priority Ship
00:20:29.313 -> Turn on Warehouse Rear
00:20:32.253 -> Turn off Warehouse Front
00:20:33.912 -> Turn on Priority Ship
00:20:34.317 -> Turn off Warehouse Rear
00:20:34.757 -> Turn off Office
00:20:35.400 -> Turn on Warehouse Front
00:20:37.384 -> Turn on Office
00:20:37.756 -> Turn on Warehouse Rear
00:20:39.272 -> Turn off Priority Ship
00:20:40.758 -> Turn off Warehouse Front
00:20:41.676 -> Turn off Office
00:20:41.917 -> Turn off Warehouse Rear
00:20:43.072 -> Turn on Priority Ship
00:20:44.292 -> Turn on Office
00:20:44.393 -> Turn on Warehouse Front
00:20:44.970 -> Turn on Warehouse Rear
00:20:48.917 -> Turn off Warehouse Rear
00:20:50.549 -> Turn off Warehouse Front
00:20:52.005 -> Turn off Priority Ship
00:20:52.375 -> Turn on Warehouse Rear
00:20:53.792 -> Turn on Priority Ship
00:20:54.170 -> Turn off Office
00:20:54.511 -> Turn on Warehouse Front
00:20:54.915 -> Turn off Priority Ship
00:20:56.723 -> Turn on Office
00:20:57.669 -> Turn off Warehouse Rear
00:21:00.205 -> Turn on Priority Ship
00:21:01.117 -> Turn on Warehouse Rear
00:21:01.457 -> Turn off Warehouse Front
00:21:04.129 -> Turn off Office
00:21:04.164 -> Turn off Warehouse Rear
00:21:04.774 -> Turn on Warehouse Front
00:21:06.224 -> Turn on Office
00:21:08.079 -> Turn on Warehouse Rear
00:21:08.551 -> Turn off Priority Ship
00:21:09.740 -> Turn on Priority Ship
00:21:10.976 -> Turn off Warehouse Front
00:21:12.628 -> Turn off Warehouse Rear
00:21:13.248 -> Turn off Office
00:21:14.007 -> Turn on Warehouse Front
00:21:14.109 -> Turn off Priority Ship
00:21:16.008 -> Turn on Office
00:21:16.614 -> Turn on Warehouse Rear
00:21:18.277 -> Turn off Warehouse Front
00:21:20.512 -> Turn on Priority Ship
00:21:21.561 -> Turn off Warehouse Rear
00:21:22.133 -> Turn on Warehouse Front
00:21:23.921 -> Turn off Office
00:21:25.279 -> Turn on Warehouse Rear
00:21:26.059 -> Turn off Warehouse Front
00:21:26.633 -> Turn on Office
00:21:29.172 -> Turn on Warehouse Front
00:21:29.481 -> Turn off Priority Ship
00:21:32.143 -> Turn off Warehouse Rear
00:21:33.022 -> Turn off Warehouse Front
00:21:34.209 -> Turn off Office
00:21:35.023 -> Turn on Priority Ship
00:21:36.041 -> Turn on Warehouse Rear
00:21:36.579 -> Turn on Office
00:21:36.953 -> Turn on Warehouse Front
00:21:40.034 -> Turn off Warehouse Rear
00:21:40.337 -> Turn off Priority Ship
00:21:40.709 -> Turn off Warehouse Front
00:21:43.069 -> Turn on Warehouse Rear
00:21:44.016 -> Turn on Warehouse Front
00:21:45.740 -> Turn on Priority Ship
00:21:46.482 -> Turn off Office
00:21:48.081 -> Turn off Warehouse Rear
00:21:48.895 -> Turn on Office
00:21:49.844 -> Turn off Warehouse Front
00:21:51.503 -> Turn on Warehouse Rear
00:21:53.121 -> Turn on Warehouse Front
00:21:54.439 -> Turn off Priority Ship
00:21:56.146 -> Turn off Warehouse Rear
00:21:57.698 -> Turn off Warehouse Front
00:21:57.935 -> Turn on Priority Ship
00:21:59.430 -> Turn on Warehouse Rear
00:21:59.601 -> Turn off Office
00:22:01.595 -> Turn on Warehouse Front
00:22:02.405 -> Turn on Office
00:22:04.245 -> Turn off Priority Ship
00:22:04.615 -> Turn off Warehouse Rear
00:22:06.172 -> Turn off Warehouse Front
00:22:08.197 -> Turn on Warehouse Rear
00:22:09.790 -> Turn off Office
00:22:09.858 -> Turn on Warehouse Front
00:22:10.682 -> Turn on Priority Ship
00:22:12.540 -> Turn off Priority Ship
00:22:12.679 -> Turn on Office
00:22:13.194 -> Turn off Warehouse Rear
00:22:14.232 -> Turn off Warehouse Front
00:22:16.246 -> Turn on Priority Ship
00:22:16.618 -> Turn on Warehouse Rear
00:22:17.323 -> Turn on Warehouse Front
00:22:19.043 -> Turn off Office
00:22:20.847 -> Turn off Warehouse Front
00:22:20.982 -> Turn off Warehouse Rear
00:22:21.692 -> Turn on Office
00:22:24.381 -> Turn on Warehouse Front
00:22:24.722 -> Turn on Warehouse Rear
00:22:25.027 -> Turn off Priority Ship
00:22:27.652 -> Turn off Office
00:22:27.754 -> Turn off Warehouse Rear
00:22:27.959 -> Turn off Warehouse Front
00:22:29.859 -> Turn on Office
00:22:30.876 -> Turn on Warehouse Rear
00:22:31.416 -> Turn on Warehouse Front
00:22:31.552 -> Turn on Priority Ship
00:22:33.215 -> Turn off Office
00:22:35.722 -> Turn on Office
00:22:37.145 -> Turn off Warehouse Rear
00:22:37.315 -> Turn off Warehouse Front
00:22:37.551 -> Turn off Priority Ship
00:22:39.178 -> Turn off Office
00:22:40.429 -> Turn on Warehouse Rear
00:22:40.634 -> Turn on Warehouse Front
00:22:41.539 -> Turn on Office
00:22:43.588 -> Turn on Priority Ship
00:22:45.943 -> Turn off Warehouse Rear
00:22:46.079 -> Turn off Warehouse Front
00:22:48.959 -> Turn on Warehouse Rear
00:22:49.505 -> Turn on Warehouse Front
00:22:49.842 -> Turn off Office
00:22:50.993 -> Turn off Priority Ship
00:22:52.176 -> Turn on Office
00:22:54.342 -> Turn off Warehouse Rear
00:22:56.031 -> Turn off Warehouse Front
00:22:56.605 -> Turn on Priority Ship
00:22:57.413 -> Turn on Warehouse Rear
00:22:59.413 -> Turn off Office
00:22:59.581 -> Turn on Warehouse Front
00:23:01.644 -> Turn on Office
00:23:01.677 -> Turn off Warehouse Rear
00:23:03.024 -> Turn off Priority Ship
00:23:03.467 -> Turn off Warehouse Front
00:23:05.230 -> Turn on Warehouse Rear
00:23:05.298 -> Turn off Office
00:23:06.277 -> Turn on Priority Ship
00:23:06.785 -> Turn on Warehouse Front
00:23:07.768 -> Turn on Office
00:23:09.124 -> Turn off Priority Ship
00:23:10.009 -> Turn off Warehouse Front
00:23:10.765 -> Turn off Warehouse Rear
00:23:12.866 -> Turn off Office
00:23:13.142 -> Turn on Warehouse Front
00:23:14.204 -> Turn on Warehouse Rear
00:23:14.343 -> Turn on Priority Ship
00:23:15.022 -> Turn on Office
00:23:19.235 -> Turn off Warehouse Front
00:23:19.409 -> Turn off Office
00:23:20.759 -> Turn off Priority Ship
00:23:20.994 -> Turn off Warehouse Rear
00:23:21.466 -> Turn on Office
00:23:22.309 -> Turn on Warehouse Front
00:23:22.754 -> Turn on Priority Ship
00:23:24.374 -> Turn on Warehouse Rear
00:23:27.985 -> Turn off Priority Ship
00:23:28.695 -> Turn off Warehouse Front
00:23:28.865 -> Turn off Office
00:23:29.032 -> Turn off Warehouse Rear
00:23:31.238 -> Turn on Office
00:23:31.884 -> Turn on Priority Ship
00:23:32.288 -> Turn on Warehouse Rear
00:23:32.523 -> Turn on Warehouse Front
00:23:37.522 -> Turn off Warehouse Rear
00:23:38.165 -> Turn off Warehouse Front
00:23:39.926 -> Turn off Office

Photo Bud (aka John)
The Old Curmudgeon!


Quote
Robo Pi
(@robo-pi)
Noble Member
Joined: 9 months ago
Posts: 1462
2020-01-14 7:26 am  

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

Please Login or Register