On to my software comment. If you only want to turn one light on, then off, your approach works fine. But if you want to simultaneously control, say 15 lights, the use of delay() will not work. delay() basically stops all processing on the Arduino and you end up losing a HUGE amount of computing power. The solution is to create a class - name it LED - which supports a number of methods, and which monitors whether the LED is currently on or off. At the start of the loop() function, you must refresh the led state by calling, for example, Green.refresh(); where Green is a LED object and refresh() is a method provided by the LED class. Then the rest of the loop() function allows you to modify the state of the LEDs. This makes your sketch look like this (for a simple example): #include LED Red = LED(6); LED Green = LED(A0); LED Yellow = LED(13); void setup() { Green.blink(100,200); Red.blink(250,250); Yellow.blink(500,100); } void loop() { Green.refresh(); Red.refresh(); Yellow.refresh(); } In this example, a red LED is connected to digital pin 6, a green LED is connected to analog pin 0, and a yellow LED is connected to digital pin 13. The blink method (provided by the LED class) turns the LED on for the first specified interval, then off for the second specified interval, then repeats. Here is the important part: there are NO delay() function calls - not in the sketch above, nor in the LED class code in LED.h. Each LED object maintains its own timer, and decides whether the LED should be on or off. Every call to refresh causes the corresponding output pin to be set HIGH or LOW, as required. This frees the Arduino to do other tasks simultaneously. Bottom line: If I could give just one piece of advice to new Arduino programmers, it would be this: find a way NOT to use delay(). It is a very effective way to paint yourself into a corner!
Jimmy, this is a great intro and I hope that Arduino beginners are encouraged to start an Arduino project. I have two comments - one on the hardware and one on the software. Regarding the hardware, I use Nano clones for all of my light and sound effects. You can get Nano clones for about 10 bucks each; name brand Nanos are about 15 bucks if you buy in bulk. (These prices have gone up a LOT due to Covid supply chain issues!) By contrast, an Uno costs over $20, and an Uno clone is between 15 and 20. Since you'll be mounting the Arduino under your layout, each Arduino project becomes a significant cost - say, equal to a ready-to-run freight car. Once I've tested my circuit and software, I mount my Arduinos on a circuit board (or bread board) along with all resistors and connectors. You can get an assortment of 100 circuit boards for $15, and 60 2-pin screw terminals for another 15$ on Amazon. The completed board can be powered by a 15 VDC power bus; no USB cable required. BUT, be sure you can access the USB connector on your Arduino so you can modify the programming later!
Hi Jimmy. Nice video - one comment. For the random lights project - the Arduino random(x,y) will return a value between x and y-1, so the sketch needs to be modified to use randomlight = random(3,7), instead of randomlight = random(3,6) - or the LED attached to DIO 6 will never come on. -Rick
Great job Jimmy! Love the new format of writing the code then explaining it. I must confess, I watch all your videos but sometimes got bogged down with the line by line explanations.
Thanks for another nice Arduino video!! The Arduino ability to introduce randomness ... using Random(x,y) function calls ... for lighting, sound, and motion controls ... is it's highlight for model railroad applications in my opinion. As you pointed out, it can make a building look more realistic with random room lighting. It can make a city scene look more realistic with buildings being randomly or (semi-randomly) lit depending on the time of day you wish to simulate. I'd like to add this realism with a fast clock so that I can watch my city scene change from one day into another. While each day is similar to the previous day ... each day slightly different from each other as in real life. Suggestion. Maybe you could discuss fast clocks in a video. JMRI has some fast clock features but I don't know how to use them.
Another nice one Jimmy. Beautifully explained. I have used someone else's example for my Outland Models buildings which makes such a difference. I have more than a dozen lights with the randomness projected to allow more than one light on at any one time. The whole lot could therefore be on at once. I like the simplicity of your presentation and especially the police lights.
Thank you very much, these arduino projects are exactly what the doctor ordered. BTW I don't need another mug but I have to get the one with the coffee and the train.
Hi from the UK Jimmy and thank you for your great video. Could you please consider a follow up video explaing how you would get the lighting effects actually onto the layout and the wiring involved.... I am imagining a police car sitting on top of the breadboard on the layout! Cheers Chris P
I get lost in the writing of programs. I would like to find some one in my local club who can teach me. I just need to join and go. Hahaha 😂 This time I think I learned a little bit from this. GOD BLESS 🚂💖🚂💖🚂💖
Hi Jimmy. I really enjoy your arduino videos (which I’ve purchased and have started trialling) - thanks for the great work. I question or suggestion for future videos. I have a load of RC plane parts in my garage from when my son was younger - esc, receivers, transmitters etc. This technology seems a lot simpler than dcc to use in rail modelling (for a start, no wiring), with all the same functionality. But there is so little uptake of RC In HO,OO, N gauge. I found a company called DelTang who made a start but seem to be out of business. Is there any chance RC could be an area for future videos?
how does one put these projects in the actual model railway layout, you have shown us using the breadboard etc but how is this wired up in an actual place on the model layout?
ok so i am new to this programming stuff, i just purchased my first arduino from amazon via your site. now in addition to this arduino down load do i need to also install the GITHUB you talk about or is that automatically installed when i down loaded arduino.
Not so simple for those of us who don’t know an arduino from an aardvark. Is there an introduction to how to do all this? It looks cool but do you need a PC specifically to program one of these projects?
Quincy: If you would like to get a better overall understanding of all things Arduino may I suggest checking out Paul McWhorter's RUclips channel and looking for the playlist titled "New Arduino Tutorials". Paul's series has nothing specific to do with model railroading but deals directly with understanding and programming the Arduino and is very concise and aimed at pure beginners, like I was when I found his channel. I found it incredibly helpful and it helped me get the maximum benefit from Jimmy's model railroad specific videos.
Hey Jimmy, I have watched this video countless times. I do have a very serious question. In the arduino, can you program it to power down a section of track right at the signal block so that if you are running multiple trains and don't have to scramble to your dcc to power down the locomotive entering that red block
@@DIYDigitalRailroad Jimmy I just checked the description and I'm not seeing the github link for the sketches & schematics. Could you please check & confirm they got uploaded? Thanks!
I'm not seeing the code or any of the links mentioned in the video either. There's a lot of links, but none specific to this video. Like the video a lot, by the way.
Out of curiosity, for the first one (police flashing lights) do you need to write the low state to an LED that is already in the low state from the prior iteration? Or is it more just for seeing what's going on in the code at each iteration?
hey jimmy; hope this message finds you well. so i purchased my first arduino kit and started playing with it. when i ran your chasing lights and random programs i found an issue with chasing lights #6. it comes on at the same time as #3 and does not seem to chase well, second, with the random light #6 does not work at all. now being i am extremely novice to this coding, i was wonder if there is a fix. please advise thank you
I'm new at this and already I have problems!! I built the project but writing the code is the issue... The word "setup" , on my IDE console is RED . How do I get it to Black ? When I try to run the example on my Arduino UNO it gives me an error that pinmode is not defined? How do I correct this? Sorry for the rookie questions.
Nice video but again I don't like the code. Don't take that personally I don't like any arduino code. Always the same beginner mistakes. 1. You should never have to say think of this as that while explaining code you should make your code read ON | OFF by making constants for ON | OFF. 2. NOTICE HOW HIGH AND LOW ARE ALL CAPS THOSE ARE CORRECTLY DEFINED CONSTANTS ALL CAPS. Timedelay should be all caps because it is a constant. Constants are all caps.
I support the code. Don't lose sight of the fact that this code is aimed at beginners. It is simple and easy to understand without the constraints placed on professional programmers. Having taught computer programming for 18 years, understanding the code is more important than industry imposed rules.
On to my software comment. If you only want to turn one light on, then off, your approach works fine. But if you want to simultaneously control, say 15 lights, the use of delay() will not work. delay() basically stops all processing on the Arduino and you end up losing a HUGE amount of computing power. The solution is to create a class - name it LED - which supports a number of methods, and which monitors whether the LED is currently on or off. At the start of the loop() function, you must refresh the led state by calling, for example, Green.refresh(); where Green is a LED object and refresh() is a method provided by the LED class. Then the rest of the loop() function allows you to modify the state of the LEDs. This makes your sketch look like this (for a simple example):
#include
LED Red = LED(6);
LED Green = LED(A0);
LED Yellow = LED(13);
void setup() {
Green.blink(100,200);
Red.blink(250,250);
Yellow.blink(500,100);
}
void loop() {
Green.refresh();
Red.refresh();
Yellow.refresh();
}
In this example, a red LED is connected to digital pin 6, a green LED is connected to analog pin 0, and a yellow LED is connected to digital pin 13. The blink method (provided by the LED class) turns the LED on for the first specified interval, then off for the second specified interval, then repeats.
Here is the important part: there are NO delay() function calls - not in the sketch above, nor in the LED class code in LED.h. Each LED object maintains its own timer, and decides whether the LED should be on or off. Every call to refresh causes the corresponding output pin to be set HIGH or LOW, as required. This frees the Arduino to do other tasks simultaneously.
Bottom line: If I could give just one piece of advice to new Arduino programmers, it would be this: find a way NOT to use delay(). It is a very effective way to paint yourself into a corner!
Thank you for your input!!
I am one of those beginner programmers and this info was very helpful!
Jimmy, this is a great intro and I hope that Arduino beginners are encouraged to start an Arduino project. I have two comments - one on the hardware and one on the software.
Regarding the hardware, I use Nano clones for all of my light and sound effects. You can get Nano clones for about 10 bucks each; name brand Nanos are about 15 bucks if you buy in bulk. (These prices have gone up a LOT due to Covid supply chain issues!) By contrast, an Uno costs over $20, and an Uno clone is between 15 and 20. Since you'll be mounting the Arduino under your layout, each Arduino project becomes a significant cost - say, equal to a ready-to-run freight car. Once I've tested my circuit and software, I mount my Arduinos on a circuit board (or bread board) along with all resistors and connectors. You can get an assortment of 100 circuit boards for $15, and 60 2-pin screw terminals for another 15$ on Amazon.
The completed board can be powered by a 15 VDC power bus; no USB cable required. BUT, be sure you can access the USB connector on your Arduino so you can modify the programming later!
I use Nanos too for nearly everything on my railroad. I typically only use UNOs for prototyping. If I need a ton of I/O, I step up to a Mega.
Hi Jimmy. Nice video - one comment. For the random lights project - the Arduino random(x,y) will return a value between x and y-1, so the sketch needs to be modified to use randomlight = random(3,7), instead of randomlight = random(3,6) - or the LED attached to DIO 6 will never come on. -Rick
good call.
Great job Jimmy! Love the new format of writing the code then explaining it. I must confess, I watch all your videos but sometimes got bogged down with the line by line explanations.
Thanks for another nice Arduino video!! The Arduino ability to introduce randomness ... using Random(x,y) function calls ... for lighting, sound, and motion controls ... is it's highlight for model railroad applications in my opinion. As you pointed out, it can make a building look more realistic with random room lighting. It can make a city scene look more realistic with buildings being randomly or (semi-randomly) lit depending on the time of day you wish to simulate. I'd like to add this realism with a fast clock so that I can watch my city scene change from one day into another. While each day is similar to the previous day ... each day slightly different from each other as in real life.
Suggestion. Maybe you could discuss fast clocks in a video. JMRI has some fast clock features but I don't know how to use them.
I'm definitely saving this one to come back to!
Very cool 😎 vid Jimmy. Love how you make the Arduino Uno so intricately accessible. Thanks 🙏 for sharing!
Jimmy: Very good presentation. My eyes didn’t roll back.
Always the goal haha!
Another nice one Jimmy. Beautifully explained. I have used someone else's example for my Outland Models buildings which makes such a difference. I have more than a dozen lights with the randomness projected to allow more than one light on at any one time. The whole lot could therefore be on at once. I like the simplicity of your presentation and especially the police lights.
Thank you for the kind words!
FANTASTIC intro video!
Very illuminating video !!
I see what you did there.
Thank you very much, these arduino projects are exactly what the doctor ordered. BTW I don't need another mug but I have to get the one with the coffee and the train.
Hi from the UK Jimmy and thank you for your great video. Could you please consider a follow up video explaing how you would get the lighting effects actually onto the layout and the wiring involved.... I am imagining a police car sitting on top of the breadboard on the layout! Cheers Chris P
On the last example you should include a time off=rand() as well so they’re not on immediately after one goes off
Thanks for sharing!! Love Arduino projects!!
Hi Jimmy, what do we need to use to power the arduino and can it be done with batteries for these projects?
You can use a short connecters , we can see easy cuircuit
I do like the insight ya bring to this aspect of the hobby. Who knows the dinosaur might evolve on a future shelf gig🚂🇨🇦🇺🇲
I get lost in the writing of programs. I would like to find some one in my local club who can teach me. I just need to join and go. Hahaha 😂
This time I think I learned a little bit from this.
GOD BLESS 🚂💖🚂💖🚂💖
Heck yeah, I love it!!!
Nice tutorial! I can almost understand! How do you get the lights from the bread board to a police car?
Just thinking past learning beginning arduino I have questions
Stationary steam engine smoke in layout?
Belt drive machines in saw mill?
Hi Jimmy. I really enjoy your arduino videos (which I’ve purchased and have started trialling) - thanks for the great work. I question or suggestion for future videos. I have a load of RC plane parts in my garage from when my son was younger - esc, receivers, transmitters etc. This technology seems a lot simpler than dcc to use in rail modelling (for a start, no wiring), with all the same functionality. But there is so little uptake of RC In HO,OO, N gauge. I found a company called DelTang who made a start but seem to be out of business. Is there any chance RC could be an area for future videos?
Thanks for the good explanation.
I am not seeing the links you mentioned in the description.
One thing.
Take a look at the raspberry pi pico (rp2040)
It's different but like arduino. Nano RP2040 for example.
Jimmy enjoyed your latest video thanks for sharing DD.
how does one put these projects in the actual model railway layout, you have shown us using the breadboard etc but how is this wired up in an actual place on the model layout?
ok so i am new to this programming stuff, i just purchased my first arduino from amazon via your site. now in addition to this arduino down load do i need to also install the GITHUB you talk about or is that automatically installed when i down loaded arduino.
Do you need a separate Arduino for each of the programs or could they run together on one Arduino?
Not so simple for those of us who don’t know an arduino from an aardvark. Is there an introduction to how to do all this? It looks cool but do you need a PC specifically to program one of these projects?
Quincy: If you would like to get a better overall understanding of all things Arduino may I suggest checking out Paul McWhorter's RUclips channel and looking for the playlist titled "New Arduino Tutorials". Paul's series has nothing specific to do with model railroading but deals directly with understanding and programming the Arduino and is very concise and aimed at pure beginners, like I was when I found his channel. I found it incredibly helpful and it helped me get the maximum benefit from Jimmy's model railroad specific videos.
@@garrettswoodworx1873 thanks for the tip
@@qwincyq6412 You're welcome! I think it is a great series, it helped me a bunch.
Hey Jimmy, I have watched this video countless times. I do have a very serious question. In the arduino, can you program it to power down a section of track right at the signal block so that if you are running multiple trains and don't have to scramble to your dcc to power down the locomotive entering that red block
Yes you can isolate that section and use a relay to power it down.
Great video. Where r the schmatics and arduino code?
The code is linked in the description as well as the schematic. They are all available on github.
I also do not see the link for the schematic and codes.
@@DIYDigitalRailroad Jimmy I just checked the description and I'm not seeing the github link for the sketches & schematics. Could you please check & confirm they got uploaded? Thanks!
I'm not seeing the code or any of the links mentioned in the video either. There's a lot of links, but none specific to this video. Like the video a lot, by the way.
@@davelandry646 Try it now, it should be fixed.
Thanks!
Out of curiosity, for the first one (police flashing lights) do you need to write the low state to an LED that is already in the low state from the prior iteration? Or is it more just for seeing what's going on in the code at each iteration?
hey jimmy; hope this message finds you well. so i purchased my first arduino kit and started playing with it. when i ran your chasing lights and random programs i found an issue with chasing lights #6. it comes on at the same time as #3 and does not seem to chase well, second, with the random light #6 does not work at all. now being i am extremely novice to this coding, i was wonder if there is a fix. please advise thank you
Bread boards are just for testing right? All those wires etc. Id hate to have deal with that in a small space.
Yes breadboards are for testing. I will sometimes design a PCB for my permanent wiring.
I'm new at this and already I have problems!!
I built the project but writing the code is the issue...
The word "setup" , on my IDE console is RED . How do I get it to Black ?
When I try to run the example on my Arduino UNO it gives me an error that pinmode is not defined? How do I correct this?
Sorry for the rookie questions.
Thank you 🙏
How about that !
Nice video but again I don't like the code. Don't take that personally I don't like any arduino code. Always the same beginner mistakes. 1. You should never have to say think of this as that while explaining code you should make your code read ON | OFF by making constants for ON | OFF. 2. NOTICE HOW HIGH AND LOW ARE ALL CAPS THOSE ARE CORRECTLY DEFINED CONSTANTS ALL CAPS. Timedelay should be all caps because it is a constant. Constants are all caps.
I support the code. Don't lose sight of the fact that this code is aimed at beginners. It is simple and easy to understand without the constraints placed on professional programmers. Having taught computer programming for 18 years, understanding the code is more important than industry imposed rules.
It's a good video. Would've been easier to understand with [ON|OFF]
Sorry it reads 'pinmode not declared."
oooooooooooolllllllllllll
Informative tutorial!