Code Not Running Correctly When Powered From External Source

I'm using an Arduino Nano, DS1307-Module-V03, and WS2812B 144 leds/m addressable leds.

So I've been coding with Arduino for a couple months and I ran into this problem with one of my projects. I'm making a 7 segment display clock and I'm using addressable leds for the numbers. I'm using the FastLED library for the leds, and I have the Wire library and the RTClib library for my RTC module. My code runs great when the Arduino is connected to my computer, but when I power it with a 9V adapter and a basic breadboard power supply module, the code doesn't run correctly. When the board is powered externally, the leds will turn on and show the current time, but the time won't change every minute like it's supposed to. When I press the button on top to restart the sketch, the leds will just turn off and I have to press the button a second time to get everything to work again. But when I press the restart button a second time, the leds will show the current time. My problem is just the leds not updating the time every minute. While the board is unplugged, the TX led blinks every second indicating serial output. I've been trying to figure this out for two days and it's driving me crazy.

In the code below I have all the different segments and the leds typed out. For example,
void segment7_2() would be the 7th segment in the 2nd digit place, the far left digit being number 1 and the far right digit being number 4. void seven2() is just the number 7 being displayed in the 2nd digit spot.

I've tried to type everything relevant to the situation, but if you need to know anything else or just clarification, just tell me. Also, just ignore all the commented lines of code from void segment1_1() to void zero1(). Thanks for any help.

#include <FastLED.h>
#include <Wire.h>
#include <RTClib.h>

#define NUM_LEDS 144
#define LED_PIN 2

int number = 0;
int a = 6;


CRGB leds[NUM_LEDS];

char DayoftheWeek[7][12] = {"sun", "mon", "tue", "wed", "thu", "fri", "sat"};
RTC_DS1307 rtc;  // declare rtc class

void setup() {


  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
  FastLED.clear();
  FastLED.show();
  FastLED.setBrightness(35);

  Serial.begin(9600);


  if (!rtc.begin())
  {
    Serial.println("Error RTC");
    while (1);
  }
  if (!rtc.isrunning())
  {
    Serial.println("RTC is not running");
  }


  pinMode(a, OUTPUT);


  digitalWrite(a, HIGH);
}




/*
  void segment1_1() {
  leds[15] = CRGB::Blue;
  leds[16] = CRGB::Blue;
  leds[17] = CRGB::Blue;
  FastLED.show();
  }
*/
void segment2_1() {
  leds[18] = CRGB::Blue;
  leds[19] = CRGB::Blue;
  leds[20] = CRGB::Blue;
  FastLED.show();
}
/*
  void segment3_1() {
  leds[8] = CRGB::Blue;
  leds[7] = CRGB::Blue;
  leds[6] = CRGB::Blue;
  FastLED.show();
  }

  void segment4_1() {
  leds[5] = CRGB::Blue;
  leds[4] = CRGB::Blue;
  leds[3] = CRGB::Blue;
  FastLED.show();
  }

  void segment5_1() {
  leds[2] = CRGB::Blue;
  leds[1] = CRGB::Blue;
  leds[0] = CRGB::Blue;
  FastLED.show();
  }

  void segment6_1() {
  leds[12] = CRGB::Blue;
  leds[13] = CRGB::Blue;
  leds[14] = CRGB::Blue;
  FastLED.show();
  }

  void segment7_1() {
  leds[11] = CRGB::Blue;
  leds[10] = CRGB::Blue;
  leds[9] = CRGB::Blue;
  FastLED.show();
  }
*/
void one1() {
  //  segment2_1();
  //segment3_1();
}
/*
  void two1() {
  segment1_1();
  segment2_1();
  segment7_1();
  segment5_1();
  segment4_1();
  }

  void three1() {
  segment1_1();
  segment2_1();
  segment3_1();
  segment7_1();
  segment4_1();
  }

  void four1() {
  segment6_1();
  segment7_1();
  segment2_1();
  segment3_1();
  }

  void five1() {
  segment1_1();
  segment6_1();
  segment7_1();
  segment3_1();
  segment4_1();
  }

  void six1() {
  segment1_1();
  segment6_1();
  segment7_1();
  segment5_1();
  segment4_1();
  segment3_1();
  }

  void seven1() {
  segment1_1();
  segment2_1();
  segment3_1();
  }

  void eight1() {
  segment1_1();
  segment2_1();
  segment3_1();
  segment4_1();
  segment5_1();
  segment6_1();
  segment7_1();
  }

  void nine1() {
  segment1_1();
  segment2_1();
  segment3_1();
  segment4_1();
  segment6_1();
  segment7_1();
  }

  void zero1() {
  segment1_1();
  segment2_1();
  segment3_1();
  segment4_1();
  segment5_1();
  segment6_1();
  }
*/


void segment1_2() {
  leds[36] = CRGB::Blue;
  leds[37] = CRGB::Blue;
  leds[38] = CRGB::Blue;
  FastLED.show();
}

void segment2_2() {
  leds[39] = CRGB::Blue;
  leds[40] = CRGB::Blue;
  leds[41] = CRGB::Blue;
  FastLED.show();
}

void segment3_2() {
  leds[29] = CRGB::Blue;
  leds[28] = CRGB::Blue;
  leds[27] = CRGB::Blue;
  FastLED.show();
}

void segment4_2() {
  leds[24] = CRGB::Blue;
  leds[25] = CRGB::Blue;
  leds[26] = CRGB::Blue;
  FastLED.show();
}

void segment5_2() {
  leds[21] = CRGB::Blue;
  leds[22] = CRGB::Blue;
  leds[23] = CRGB::Blue;
  FastLED.show();
}

void segment6_2() {
  leds[35] = CRGB::Blue;
  leds[34] = CRGB::Blue;
  leds[33] = CRGB::Blue;
  FastLED.show();
}

void segment7_2() {
  leds[32] = CRGB::Blue;
  leds[31] = CRGB::Blue;
  leds[30] = CRGB::Blue;
  FastLED.show();
}

void one2() {
  segment2_2();
  segment3_2();
}

void two2() {
  segment1_2();
  segment2_2();
  segment7_2();
  segment5_2();
  segment4_2();
}

void three2() {
  segment1_2();
  segment2_2();
  segment3_2();
  segment7_2();
  segment4_2();
}

void four2() {
  segment6_2();
  segment7_2();
  segment2_2();
  segment3_2();
}

void five2() {
  segment1_2();
  segment6_2();
  segment7_2();
  segment3_2();
  segment4_2();
}

void six2() {
  segment1_2();
  segment6_2();
  segment7_2();
  segment5_2();
  segment4_2();
  segment3_2();
}

void seven2() {
  segment1_2();
  segment2_2();
  segment3_2();
}

void eight2() {
  segment1_2();
  segment2_2();
  segment3_2();
  segment4_2();
  segment5_2();
  segment6_2();
  segment7_2();
}

void nine2() {
  segment1_2();
  segment2_2();
  segment3_2();
  segment4_2();
  segment6_2();
  segment7_2();
}

void zero2() {
  segment1_2();
  segment2_2();
  segment3_2();
  segment4_2();
  segment5_2();
  segment6_2();
}

void segment1_3()  {
  leds[59] = CRGB::Blue;
  leds[60] = CRGB::Blue;
  leds[61] = CRGB::Blue;
  FastLED.show();
}

void segment2_3()  {
  leds[62] = CRGB::Blue;
  leds[63] = CRGB::Blue;
  leds[64] = CRGB::Blue;
  FastLED.show();
}

void segment3_3()  {
  leds[52] = CRGB::Blue;
  leds[51] = CRGB::Blue;
  leds[50] = CRGB::Blue;
  FastLED.show();
}

void segment4_3()  {
  leds[47] = CRGB::Blue;
  leds[48] = CRGB::Blue;
  leds[49] = CRGB::Blue;
  FastLED.show();
}

void segment5_3()  {
  leds[44] = CRGB::Blue;
  leds[45] = CRGB::Blue;
  leds[46] = CRGB::Blue;
  FastLED.show();
}

void segment6_3()  {
  leds[58] = CRGB::Blue;
  leds[57] = CRGB::Blue;
  leds[56] = CRGB::Blue;
  FastLED.show();
}

void segment7_3()  {
  leds[55] = CRGB::Blue;
  leds[54] = CRGB::Blue;
  leds[53] = CRGB::Blue;
  FastLED.show();
}

void one3() {
  segment2_3();
  segment3_3();
}

void two3() {
  segment1_3();
  segment2_3();
  segment7_3();
  segment5_3();
  segment4_3();
}

void three3() {
  segment1_3();
  segment2_3();
  segment3_3();
  segment7_3();
  segment4_3();
}

void four3() {
  segment6_3();
  segment7_3();
  segment2_3();
  segment3_3();
}

void five3() {
  segment1_3();
  segment6_3();
  segment7_3();
  segment3_3();
  segment4_3();
}

void six3() {
  segment1_3();
  segment6_3();
  segment7_3();
  segment5_3();
  segment4_3();
  segment3_3();
}

void seven3() {
  segment1_3();
  segment2_3();
  segment3_3();
}

void eight3() {
  segment1_3();
  segment2_3();
  segment3_3();
  segment4_3();
  segment5_3();
  segment6_3();
  segment7_3();
}

void nine3() {
  segment1_3();
  segment2_3();
  segment3_3();
  segment4_3();
  segment6_3();
  segment7_3();
}

void zero3() {
  segment1_3();
  segment2_3();
  segment3_3();
  segment4_3();
  segment5_3();
  segment6_3();
}

void segment1_4()  {
  leds[80] = CRGB::Blue;
  leds[81] = CRGB::Blue;
  leds[82] = CRGB::Blue;
  FastLED.show();
}

void segment2_4()  {
  leds[83] = CRGB::Blue;
  leds[84] = CRGB::Blue;
  leds[85] = CRGB::Blue;
  FastLED.show();
}

void segment3_4()  {
  leds[73] = CRGB::Blue;
  leds[72] = CRGB::Blue;
  leds[71] = CRGB::Blue;
  FastLED.show();
}

void segment4_4()  {
  leds[70] = CRGB::Blue;
  leds[69] = CRGB::Blue;
  leds[68] = CRGB::Blue;
  FastLED.show();
}

void segment5_4()  {
  leds[67] = CRGB::Blue;
  leds[66] = CRGB::Blue;
  leds[65] = CRGB::Blue;
  FastLED.show();
}

void segment6_4()  {
  leds[79] = CRGB::Blue;
  leds[78] = CRGB::Blue;
  leds[77] = CRGB::Blue;
  FastLED.show();
}

void segment7_4()  {
  leds[76] = CRGB::Blue;
  leds[75] = CRGB::Blue;
  leds[74] = CRGB::Blue;
  FastLED.show();
}

void one4() {
  segment2_4();
  segment3_4();
}

void two4() {
  segment1_4();
  segment2_4();
  segment7_4();
  segment5_4();
  segment4_4();
}

void three4() {
  segment1_4();
  segment2_4();
  segment3_4();
  segment7_4();
  segment4_4();
}

void four4() {
  segment6_4();
  segment7_4();
  segment2_4();
  segment3_4();
}

void five4() {
  segment1_4();
  segment6_4();
  segment7_4();
  segment3_4();
  segment4_4();
}

void six4() {
  segment1_4();
  segment6_4();
  segment7_4();
  segment5_4();
  segment4_4();
  segment3_4();
}

void seven4() {
  segment1_4();
  segment2_4();
  segment3_4();
}

void eight4() {
  segment1_4();
  segment2_4();
  segment3_4();
  segment4_4();
  segment5_4();
  segment6_4();
  segment7_4();
}

void nine4() {
  segment1_4();
  segment2_4();
  segment3_4();
  segment4_4();
  segment6_4();
  segment7_4();
}

void zero4() {
  segment1_4();
  segment2_4();
  segment3_4();
  segment4_4();
  segment5_4();
  segment6_4();
}

void colon()  {
  leds[42] = CRGB::Blue;
  leds[43] = CRGB::Blue;
  FastLED.show();
}


void loop() {


  DateTime now = rtc.now();  // get time from rtc


 
  Serial.print(now.hour());
  Serial.print(":");
  Serial.print(now.minute());
  Serial.print(":");
  Serial.println(now.second());


  switch (now.minute())  {
    case 0:
      zero3();
      zero4();
      break;
    case 1:
      zero3();
      one4();
      break;
    case 2:
      zero3();
      two4();
      break;
    case 3:
      zero3();
      three4();
      break;
    case 4:
      zero3();
      four4();
      break;
    case 5:
      zero3();
      five4();
      break;
    case 6:
      zero3();
      six4();
      break;
    case 7:
      zero3();
      seven4();
      break;
    case 8:
      zero3();
      eight4();
      break;
    case 9:
      zero3();
      nine4();
      break;
    case 10:
      one3();
      zero4();
      break;
    case 11:
      one3();
      one4();
      break;
    case 12:
      one3();
      two4();
      break;
    case 13:
      one3();
      three4();
      break;
    case 14:
      one3();
      four4();
      break;
    case 15:
      one3();
      five4();
      break;
    case 16:
      one3();
      six4();
      break;
    case 17:
      one3();
      seven4();
      break;
    case 18:
      one3();
      eight4();
      break;
    case 19:
      one3();
      nine4();
      break;
    case 20:
      two3();
      zero4();
      break;
    case 21:
      two3();
      one4();
      break;
    case 22:
      two3();
      two4();
      break;
    case 23:
      two3();
      three4();
      break;
    case 24:
      two3();
      four4();
      break;
    case 25:
      two3();
      five4();
      break;
    case 26:
      two3();
      six4();
      break;
    case 27:
      two3();
      seven4();
      break;
    case 28:
      two3();
      eight4();
      break;
    case 29:
      two3();
      nine4();
      break;
    case 30:
      three3();
      zero4();
      break;
    case 31:
      three3();
      one4();
      break;
    case 32:
      three3();
      two4();
      break;
    case 33:
      three3();
      three4();
      break;
    case 34:
      three3();
      four4();
      break;
    case 35:
      three3();
      five4();
      break;
    case 36:
      three3();
      six4();
      break;
    case 37:
      three3();
      seven4();
      break;
    case 38:
      three3();
      eight4();
      break;
    case 39:
      three3();
      nine4();
      break;
    case 40:
      four3();
      zero4();
      break;
    case 41:
      four3();
      one4();
      break;
    case 42:
      four3();
      two4();
      break;
    case 43:
      four3();
      three4();
      break;
    case 44:
      four3();
      four4();
      break;
    case 45:
      four3();
      five4();
      break;
    case 46:
      four3();
      six4();
      break;
    case 47:
      four3();
      seven4();
      break;
    case 48:
      four3();
      eight4();
      break;
    case 49:
      four3();
      zero4();
    case 50:
      five3();
      zero4();
      break;
    case 51:
      five3();
      one4();
      break;
    case 52:
      five3();
      two4();
      break;
    case 53:
      five3();
      three4();
      break;
    case 54:
      five3();
      four4();
      break;
    case 55:
      five3();
      five4();
      break;
    case 56:
      five3();
      six4();
      break;
    case 57:
      five3();
      seven4();
      break;
    case 58:
      five3();
      eight4();
      break;
    case 59:
      five3();
      nine4();
      break;
    default:
      leds[143] = CRGB::Blue;
      FastLED.show();
  }

  switch (now.hour()) {

    case 0:
      two2();
      break;
    case 1:
      one2();
      break;
    case 2:
      two2();
      break;
    case 3:
      three2();
      break;
    case 4:
      four2();
      break;
    case 5:
      five2();
      break;
    case 6:
      six2();
      break;
    case 7:
      seven2();
      break;
    case 8:
      eight2();
      break;
    case 9:
      nine2();
      break;
    case 10:
      one1();
      zero2();
      break;
    case 11:
      one2();
      one1();
      break;
    case 12:
      one1();
      two2();
      break;
    case 13:
      one2();
      break;
    case 14:
      two2();
      break;
    case 15:
      three2();
      break;
    case 16:
      four2();
      break;
    case 17:
      five2();
      break;
    case 18:
      six2();
      break;
    case 19:
      seven2();
      break;
    case 20:
      eight2();
      break;
    case 21:
      nine2();
      break;
    case 22:
      one1();
      zero2();
      break;
    case 23:
      one1();
      one2();
      break;
    default:
      Serial.println("error_hour");
      break;

  }

  leds[42] = CRGB::Red;
  leds[43] = CRGB::Red;
  FastLED.show();

  if (now.second() == 59)  {
    FastLED.clear();
  }

  delay(1000);

}

Are you getting stuck in while loops when serial is not connected? Haven’t really looked in detail but common problem when you disconnect from computer and code doesn’t run the same

I haven’t used any while loops in my code, and am not really familiar with them anyway.

There is one on ~ line 36.

The problem appears to occur when the code requires a serial output but there is no serial connected to output to. Don’t know if it is your issue just same symptoms

How are you powering the WS2812B LEDs? At 5V, each LED draws about 50mA.

Oh ya you’re right. I copy pasted that part for the RTC and didn’t see it there before. How would I fix the problem?

I suppose if you don’t need the serial print when not connected you could just comment out all the serial code and see if that works.

The adapter I'm using outputs 1 Amp

This didn't seem to solve the problem.

Well that’s one thing ruled out. Power supply is the likely other reason when you unplug from the computer for a failure. I can’t see much else in your code but it is long and I am on a phone

How exactly is your power connected? 144 LEDs is a lot of potential power to sink, and if they're all taking it from the Arduino's on-board voltage regulator it's not going to like that. The regulator might be overloaded, dropping the voltage and causing the microcontroller to brown out or something.

I had the leds powered through the Arduino, but I could easily hook them up to the power supply module, and don't know why I didn't do that before. Everything is now working fine. Thank you for your help!

:+1:t2: Mark the post as solved