Skip to main content

I wanted to use multiple LEDs to simulate water level warning lights around Lionel swing bridge. My solution was to use an Arduino (I used a NANO, but same code will work on UNO) as it has 6 output pins that have Pulse Width Modulation (PWM) control. Each pin can source/sink 20ma, so can drive one LED directly. I used standard 2.0VDC drop Red LED in series with 150ohm resistor. LED connected to Arduino output such that a 1 on the output turns the LED on.

First, I played with trying to blink one LED so that it looks like it is an incandescent bulb, i.e., rather quick turn on, slow turn off. I came up will 11 PWM values between 0% and 100% modulation, and 11 delay values between 15milliseconds and 1 second. Once I liked the way blinking of a single LED looked, I attacked the problem of getting all 6 PWM outputs follow my on/off ramps, but added the requirement that the 6 LEDs would not be in sync, and in fact, keep "rolling" their individual flashes past each other, giving the look not being in sync with each other.

I decided using arrays was the way to go. For you non-programmers, an array is like a drawer you divide into sections and name each section 0 through X (so if you have 6 sections, they would be 0 through 5). You can then ask someone to go to the drawer and get (or put) something into a section of the drawer based on its name (number).

Reading the code is probably clearer than me trying to explain in words every step. But in general, first everything is initialized. You read the present time, and compare it (on an LED by LED basis) to when the next change to an LED is required. If change is required, write out the next PWM value, setup the time when the next change is required. If the next change for a LED is to be off, the next change time is influenced by the addition of a "prime number" number of milliseconds to achieve the not in sync look.

.jpg of code listing and Arduino .ino file included with this post.

Arduino MultiLED Part 1Arduino MultiLED Part 2

Attachments

Last edited by MED
Original Post

Replies sorted oldest to newest

I wanted three flashing lights on a bridge that were not in sync.

I described the desired results that I wanted and let ChatGPT generate the Arduino Sketch.

================== Input to ChatGPT =====================

create an Arduino sketch
comment THREE FLASHING LIGHTS ver 1.0
comment Lights not to be in sync
comment David Nissen  Nov 6,2023
comment This sketch generated by ChatGPT

three outputs on pins 8, 10, 12, lt1, lt2, lt3
all outputs to have a 50 percent dute cycle
the period for lt1 to be 2.00 sec
the period for lt2 to be 1.93 sec
the period for lt3 to be 1.84 sec

==================== Output from ChatGPT =================

Here's an Arduino sketch that implements the three flashing lights as per your requirements:

// THREE FLASHING LIGHTS ver 1.0
// Lights not in sync
// David Nissen Nov 6, 2023
// This sketch generated by ChatGPT

// Define the pins for the three lights
const int lt1Pin = 8;
const int lt2Pin = 10;
const int lt3Pin = 12;

// Define the periods for each light in milliseconds
const unsigned long lt1Period = 2000;  // 2.00 seconds
const unsigned long lt2Period = 1930;  // 1.93 seconds
const unsigned long lt3Period = 1840;  // 1.84 seconds

// Variables to keep track of the last state change time for each light
unsigned long lt1LastChangeTime = 0;
unsigned long lt2LastChangeTime = 0;
unsigned long lt3LastChangeTime = 0;

// Variables to keep track of the current state of each light
int lt1State = LOW;
int lt2State = LOW;
int lt3State = LOW;

void setup() {
  // Initialize the pins as outputs
  pinMode(lt1Pin, OUTPUT);
  pinMode(lt2Pin, OUTPUT);
  pinMode(lt3Pin, OUTPUT);
}

void loop() {
  // Get the current time
  unsigned long currentTime = millis();

  // Check if it's time to change the state of lt1
  if (currentTime - lt1LastChangeTime >= lt1Period / 2) {
    lt1State = !lt1State;  // Toggle the state
    digitalWrite(lt1Pin, lt1State);  // Update the LED
    lt1LastChangeTime = currentTime;  // Update the last change time
  }

  // Check if it's time to change the state of lt2
  if (currentTime - lt2LastChangeTime >= lt2Period / 2) {
    lt2State = !lt2State;  // Toggle the state
    digitalWrite(lt2Pin, lt2State);  // Update the LED
    lt2LastChangeTime = currentTime;  // Update the last change time
  }

  // Check if it's time to change the state of lt3
  if (currentTime - lt3LastChangeTime >= lt3Period / 2) {
    lt3State = !lt3State;  // Toggle the state
    digitalWrite(lt3Pin, lt3State);  // Update the LED
    lt3LastChangeTime = currentTime;  // Update the last change time
  }
}

This sketch will make three LEDs connected to pins 8, 10, and 12 flash with a 50% duty cycle, and their periods will be as specified. The lights are not in sync, as requested.

================ Load the Sketch into the Arduino IDE ============

Verify and then upload

========================== Result =======================



Attachments

Videos (1)
Flashing LEDs

gunrunnerjohn:

I am working on another project using ChatGPT to generate Arduino code. I have posted this on another thread.

https://ogrforum.ogaugerr.com/...ch-for-train-control

I have also tested ChatGPT writing Python code with good results. I think it would be worthwhile for you to try writing code for a Microchip PIC processor.

I had previously done a project with the out of sync flashing lights and spent many hours writing and debugging code. The example posted here from beginning to end including testing took about one half hour.

@MED posted:

@RRDOC Three videos. First just a couple seconds after power on. Second about 25 seconds after the first completed. Third about 50 seconds after end of second video.

bridges: the lights around the bridge do not flash the “fender” lights are a steady red. The lights on top of a bridge “range lights” Flash.

the fender is the center part around the bridge that protects it from boat traffic, also the side fenders on the or near the shoreline are marked as well solid red only. Center of channel hanging off the center of the bridge if too low for boat traffic will also be red. When the bridge swings open the center channel light will now be over the fender and a green light over red will show. On a lift span the light will show green when the bridge is fully raised and locked in the open position.

Attachments

Images (2)
  • IMG_0312: This is one of the bridges I used to work on over the Harlem river. It would be similar to the bridge you’re showing your LED lights on. If you look closely, you can see the center fender with the red lights marking the fender and channel.
  • IMG_0313: This is a great shot of my bridges on the Harlem River. You can clearly see the Bridge fenders in this shot
Last edited by ThatGuy

Add Reply

Post

OGR Publishing, Inc., 1310 Eastside Centre Ct, Suite 6, Mountain Home, AR 72653
800-980-OGRR (6477)
www.ogaugerr.com

×
×
×
×
Link copied to your clipboard.
×
×