Unwanted Flicker. How do I correct?

First let me state that I am a beginner so please keep your answers simple. Thanks

I have created 3 different projects for my Model Railroad. These are all on a ELEGO UNO

  1. One project contains 5 LED's at different rates controlled by Millis.

  2. Second project contains 2 LED's at different rates controlled by Millis.

At this point everything worked perfectly.

  1. Added 3rd project which contains only one LED.

When I added this project I started to get a flicker from all the LED'S. I brought out my computer and reloaded the program. Everything worked fine. Flicker was almost gone. Computer was still hooked up.

When I switched to a New 9v battery the flicker returned.

How can I correct this?

Stay Safe

Post the code, using code tags, as described in "How to use the forum".

Also, take the time to explain what the three projects have to do with each other.

#define INTERVAL_1 600   // ms Traffic Light
#define INTERVAL_2 50  // ms White LED TV
#define INTERVAL_3 70  // ms Blue LED TV
#define INTERVAL_4 70  // ms yellow LED Fire in Barrel
#define INTERVAL_5 30  // ms red LED    Fire in Barrel
#define INTERVAL_6 35000// ms yellow LED-Master Bedroom
#define INTERVAL_7 25000// ms yellow LED-Bottom Bedroom
#define INTERVAL_8 10000// ms yellow LED-Top Floor Bedroom

byte led_1;            //  Yellow Traffic Light
byte led_2;            // White TV
byte led_3;            // Blue TV
byte led_4;             // Yellow Fire in Barrel
byte led_5;             // Red    Fire in Barrel
byte led_6;             // yellow Master Bedroom
byte led_7;             // yellow Bottom Bedroom
byte led_8;              // yellow Top Floor Bedroom

unsigned long time_for_action_1;
unsigned long time_for_action_2;
unsigned long time_for_action_3;
unsigned long time_for_action_4;
unsigned long time_for_action_5;
unsigned long time_for_action_6;
unsigned long time_for_action_7;
unsigned long time_for_action_8;
void setup() {
  // put your setup code here, to run once:
pinMode(13,OUTPUT);// Traffic Light
pinMode(6,OUTPUT); // TV
pinMode(2,OUTPUT); //  TV
pinMode(8,OUTPUT); // Fire Barrel
pinMode(10,OUTPUT); // Fire Barrel
pinMode(3,OUTPUT);  // Master Bedroom
pinMode(11,OUTPUT); // Bottom Bedroom
pinMode(5,OUTPUT);  // Top Floor Bedroom   

void loop() {                     
if (millis() >= time_for_action_1) {
  time_for_action_1 = millis() + (unsigned long)INTERVAL_1;
  led_1 = !led_1;}                                               // Yellow Blicking Traffic Light
if (millis() >= time_for_action_2) {
  time_for_action_2 = millis() + (unsigned long)INTERVAL_2;
  led_2 = !led_2;}                                             //TV
if (millis() >= time_for_action_3){                            
  time_for_action_3 = millis() + (unsigned long)INTERVAL_3;
  led_3 = !led_3;}                                            // TV

if (millis() >= time_for_action_4){                            
  time_for_action_4 = millis() + (unsigned long)INTERVAL_4;   // Fire Barrel
  led_4 = !led_4;}  

  if (millis() >= time_for_action_5){                            
  time_for_action_5 = millis() + (unsigned long)INTERVAL_5;   //Fire Barrel
  led_5 = !led_5;}  
if (millis() >= time_for_action_6) {
  time_for_action_6 = millis() + (unsigned long)INTERVAL_6;   // Master Bedroom
  led_6 = !led_6;} 
  if (millis() >= time_for_action_7) {
  time_for_action_7 = millis() + (unsigned long)INTERVAL_7;   // Bottom Floor Bedroom
  led_7 = !led_7;} 
  if (millis() >= time_for_action_8) {
  time_for_action_8 = millis() + (unsigned long)INTERVAL_8;   // Top Floor Bedroom
  led_8 = !led_8;} 

You are using millis() incorrectly. A few minutes spent studying this tutorial should help!

Now, what were the three projects?

"When I switched to a New 9v battery the flicker returned.

How can I correct this?"

Use a wall-wart for power. The flickering is likely the Uno shutting down, the battery recharging enough to restart, then shutting down.
If you put a simple Serial.print("."); in setup() when running off a battery, you should see a . every the Uno restarts.

Do you have the required current limiting resistors (~200 Ohms minimum) on each LED?

The three projects are all on the same UNO

  1. This is a Farm house with a TV flickering in one room and different bedroom and living room lights going on at off at different times.

  2. This is a warming barrel with a fire in it. Red and Yellow LED’s Flickering to create the fire.

  3. The last one is a traffic light with a blinking yellow LED.

In a previous thread someone checked the code and told me even though is was not the standard
millis it would work fine. As I stated above the projects worked perfectly using this code. If I am on my computer everything works fine. I don’t want to have to hook up a computer every time I want to run.

The problem occured when I connected to the 9v Battery.

I will be happy to check out the tutorial.

All LED’s have 330 ohm resistors.

Where do I add the Serial Print on the Script?

The problem occured when I connected to the 9v Battery.

9V block batteries are for smoke alarms, and can’t supply enough power for the Uno and a bunch of LEDs.

Doesn't the Wall Wart run 12v. If so will it not hurt the UNO?

I am confused. Isn't the UNO running on 5v?

9V block batteries are for smoke alarms, and can't supply enough power for the Uno and a bunch of LEDs.

In addition to the lack of capacity of the battery itself, the 5v regulator on the UNO was intended mainly to power the UNO itself, and has very little capacity for external loads.

If you must run off battery power, try a USB power bank connected to the USB connector on the UNO, there should be sufficient current draw from the LEDs to avoid the common problem of the power bank shutting off from too small of a load.

In reference to your question about a wall wart - use a USB charger, those are usually rated around an amp or more these days, supply 5 volts, and everyone seems to have spare ones lying around.

+1 for the USB or other 5V charger. You can connect it to the Arduino either through a USB connector, or through the 5V pin and GND, but not through RAW or the barrel jack.

OK I got lucky. I found and old computer laying around. I programed the arduino into it and copied the script. I installed it under my layout to be permanent. Everything works fine.

1.As I add more projects to this Arduino am I going to create the same problem?

  1. Someone above mentioned I am using the wrong code for millis but as I stated It runs everything perfectly.

Should I be looking to change the Code as I move forward?

Stay Safe

as I stated It runs everything perfectly.

Until the millisecond counter overflows. You have about 50 days.

When the ms overflows won't it just go back to zero and start the scripts again?

if (millis() >= time_for_action_1) {
  time_for_action_1 = millis() + (unsigned long)INTERVAL_1;
  led_1 = !led_1;}                                               // Yellow Blicking Traffic Light

As soon as millis() + INTERVAL_1 overflows, time_for_action_1 becomes a very small number, and the if statement will be true every time through the loop until millis() itself overflows. As a practical matter, you will never encounter the problem since a model railroad layout is unlikely to be left running for fifty days, but it is best to do it properly so you will not implement it this way when it does matter.

...the scripts...

The programs that run on Arduino are not scripts. Typically, a script is a text file containing command language instructions, that is processed in some operating system by a real time interpreter. The correct word for Arduino code is "sketch", you can also say "program" or "source code".