Notifications
Clear all

A New Building Lighting for My HO Layout  


Photo Bud
(@photo-bud)
Trusted Member
Joined: 2 years ago
Posts: 71
Topic starter  

So, a year ago I posted a test for a non-existent building. Tonight, I modified the code for an actual building that I am still working on. The momentous thing about this is that after modifying it, it compiled on the first try! Then I prepared a breadboard test and it worked on the first try! I should quit while I'm ahead! lol

Eventually hard coded "times" will be replaced with randomized start/stop within constraints. Also hoping to have a centralized Mega with RTC broadcasting the fast clock times to all Arduino Nanos on the layout. But in the meantime, this will work. There are seven locations within this building, each with two LEDs. A freight room, a priority freight/office (1st floor), an office (2nd floor), attic, external lights on the back, and on the dock.

Here is a picture of the building under construction.

 

IMG 20210112 170451152

Here is the breadboard circuit. Each pin is controlling two LEDs.

IMG 20210113 220256833

And here is the code.

 

/*
  Large_Frt_House_Lighting
  Want to set up time spans for lights on by hour/minute format (24hr clock)
  Want fast clock capability both as testing function and normal running. Usually 4:1 or 5:1
  Include start of sequence flash of all lights 5 times
  Watch push button to restart sequence
  Initial lights to include
    Freight Room (x2)
    1st Floor (x2)
    2nd Floor (x2)
    Attic
    Outside Back Door (x2)
    Outside Freight Dock Main (x2)
    Outside Freight Dock Office (x2)
    
*/
// Time Variables
  unsigned long currtime;
  unsigned long starttime;
  unsigned long elapsetime;
  float floatelapse;
  float hourconv;
  float hourmsecs = 3600000;
  float minmsecs  =   60000;
  float daymsecs  = hourmsecs * 24;
  float elapseoffset = 16.95 * hourmsecs;   // On startup, start the clock at approx 16:47 
  float flmin;
  float fastratio = 12;
  int ihour;
  int imin;
  int iprevmin = 99;                        // integer dummy value of previous minute for proper start up
  
// LED Info
  int frtroom = 9;            //red
  int firstfloor = 3;         //yellow
  int secondfloor = 5;        //green
  int attic = 6;              //blue
  int outbackdoor = 7;        //yellow
  int outdockfreight = 2;     //white
  int outdockoffice = 4;      //white
  
// Room Arrays
  int onhrArray[] = {17, 17, 17, 17, 17, 17, 17};       // Time on hour
  int onmiArray[] = {00, 05, 10, 25, 00, 12, 12};       // Time on minute  
  int offhArray[] = {19, 19, 18, 17, 19, 19, 19};       // Time off hour  
  int offmArray[] = {29, 28, 20, 40, 29, 28, 28};       // Time off minute  
  int statArray[] = {false, false, false, false, false, false, false}; //Current on/off status of LED
  int ledArray[]  = {frtroom, firstfloor, secondfloor, attic, outbackdoor, outdockfreight, outdockoffice}; //Pin numbers of corresponding rooms
  int allarraysize = sizeof(ledArray)/sizeof(ledArray[0]);
  int outdockfreightIdx = 5;
  int justRoomLEDs[] = {frtroom, firstfloor, secondfloor, attic, outbackdoor, outdockfreight, outdockoffice};
  int justroomLEDsize = sizeof(justRoomLEDs)/sizeof(justRoomLEDs[0]);

// Misc Variables
  int i = 0;
  int j = 0;
  int longdelay = 300;
  int shortdelay = 200;

// Start of Code
void setup() {
  starttime = millis();
  Serial.begin(9600);
  pinMode(frtroom, OUTPUT);
  pinMode(firstfloor, OUTPUT);
  pinMode(secondfloor, OUTPUT);
  pinMode(attic, OUTPUT);
  pinMode(outbackdoor, OUTPUT);
  pinMode(outdockfreight, OUTPUT);
  pinMode(outdockoffice, OUTPUT);

  for (i=0; i<5; i = i + 1) {            // Flash all LEDs 5 times
    for (j=0; j<justroomLEDsize; j = j + 1) {
      digitalWrite(justRoomLEDs[j], HIGH);   // turn the LED on (HIGH is the voltage level)
    }
    delay(longdelay);                   // wait for long delay
    for (j=0; j<justroomLEDsize; j = j + 1) {
      digitalWrite(justRoomLEDs[j], LOW);    // turn the LED off by making the voltage LOW
    }
    delay(shortdelay);                   // wait for short delay
  }
}

void print2digits(int number) {          // Print with leading zero
  if (number >=0 && number < 10) {
    Serial.print('0');
  }
  Serial.print(number);
}

void loop() {                            // Main Loop
  currtime = millis();
  elapsetime = currtime - starttime;
  elapsetime = (elapsetime * fastratio) + elapseoffset;
  floatelapse = float (elapsetime);
  hourconv = floatelapse / hourmsecs;
  ihour = int(hourconv);
  flmin = hourconv - ihour;
  imin  = 60 * flmin;
  if (imin != iprevmin) {
    Serial.print("           Time: ");
    if (ihour > 23) {
      ihour = 0;
      starttime = currtime;
      elapseoffset = 0;
    }
    print2digits(ihour);
    Serial.print (":");
    print2digits(imin);    
    Serial.println();
    iprevmin = imin;
    
    for (j=0; j<allarraysize; j = j + 1) {
      if (statArray[j] == false && ihour == onhrArray[j] && imin == onmiArray[j]) {
        statArray[j] = true;
        digitalWrite (ledArray[j], HIGH);
      }
      else if (statArray[j] == true && ihour == offhArray[j] && imin == offmArray[j]) {
        statArray[j] = false;
        digitalWrite (ledArray[j], LOW);
      }
    }
  }
  
  delay (75);
}

Photo Bud (aka John)The Old Curmudgeon!


hayttom liked
Quote
Photo Bud
(@photo-bud)
Trusted Member
Joined: 2 years ago
Posts: 71
Topic starter  

Need some help. As can be seen from code and description, 14 LEDs will be used, with two on each of seven pins. Assuming 18-20 mA each with a 470 ohm resistor per pair, this would exceed the 200 mA total for board. As I am anything but an electronic whiz, what would be the minimum resistor I would need to drop total amperage below that total?

AND would using PWM to reduce brightness also reduce the amperage or do I need to use higher ohm resistors anyway?

Photo Bud (aka John)The Old Curmudgeon!


ReplyQuote
Photo Bud
(@photo-bud)
Trusted Member
Joined: 2 years ago
Posts: 71
Topic starter  

Here is Fritzing view of test breadboard.

Lrg Frt House Lighting bb

Photo Bud (aka John)The Old Curmudgeon!


ReplyQuote
frogandtoad
(@frogandtoad)
Honorable Member
Joined: 2 years ago
Posts: 673
 

@photo-bud

Posted by: @photo-bud

Need some help. As can be seen from code and description, 14 LEDs will be used, with two on each of seven pins. Assuming 18-20 mA each with a 470 ohm resistor per pair, this would exceed the 200 mA total for board. As I am anything but an electronic whiz, what would be the minimum resistor I would need to drop total amperage below that total?

AND would using PWM to reduce brightness also reduce the amperage or do I need to use higher ohm resistors anyway?

It's not recommended to use LED's in parallel with a single resistor, as due to inconsistencies in current draw, they won't all get the same current and will start to burn out, as one will hog more of the current - I have read about this in the past, and that is my recollection of it.

Wiring them up in series is recommended, that way they'll all get the same current going through them, but this also presents another problem... the voltage drop across the two LED's (the LED forward voltage * 2) may be greater than the 5V supply - You'll need to check the datasheet for your LED's.

Calculating the current using OHMS LAW is very simple - For example (2 LED's in series):

Resistance = (Supply Voltage - LED Forward Voltage * 2 LED's) / LED Current (in AMPS)
Resistance = (5 - (1.9 * 2)) / 0.015
Resistance = (5 - 3.8) / 0.015
Resistance = 1.2 / 0.015
Resistance = 80 ohms

Maybe looking at some cheap BJT's and a separate power supply is the way to go?

Cheers.


Photo Bud liked
ReplyQuote
Photo Bud
(@photo-bud)
Trusted Member
Joined: 2 years ago
Posts: 71
Topic starter  

@frogandtoad Thanks, I appreciate the heads up AND Ohms Law info.

Photo Bud (aka John)The Old Curmudgeon!


ReplyQuote