Arduino code works on PC but not with my battery

Hello, I know this question has already been asked a couple times but the solutions do not work for me.


I have this circuit (sorry, not really professional, it's just for cosplay purpose).

I use this code:

#include <Adafruit_NeoPixel.h>

class Strip
{
public:
  uint8_t   effect;
  uint8_t   effects;
  uint16_t  effStep;
  unsigned long effStart;
  Adafruit_NeoPixel strip;
  Strip(uint16_t leds, uint8_t pin, uint8_t toteffects, uint16_t striptype) : strip(leds, pin, striptype) {
    effect = -1;
    effects = toteffects;
    Reset();
  }
  void Reset(){
    effStep = 0;
    effect = (effect + 1) % effects;
    effStart = millis();
  }
};

struct Loop
{
  uint8_t currentChild;
  uint8_t childs;
  bool timeBased;
  uint16_t cycles;
  uint16_t currentTime;
  Loop(uint8_t totchilds, bool timebased, uint16_t tottime) {currentTime=0;currentChild=0;childs=totchilds;timeBased=timebased;cycles=tottime;}
};

Strip strip_0(40, 6, 40, NEO_GRB + NEO_KHZ800);
struct Loop strip0loop0(1, false, 1);

//[GLOBAL_VARIABLES]

void setup() {

  //Your setup here:

  strip_0.strip.begin();
}

void loop() {

  //Your code here:

  strips_loop();
}

void strips_loop() {
  if(strip0_loop0() & 0x01)
    strip_0.strip.show();
}

uint8_t strip0_loop0() {
  uint8_t ret = 0x00;
  switch(strip0loop0.currentChild) {
    case 0: 
           ret = strip0_loop0_eff0();break;
  }
  if(ret & 0x02) {
    ret &= 0xfd;
    if(strip0loop0.currentChild + 1 >= strip0loop0.childs) {
      strip0loop0.currentChild = 0;
      if(++strip0loop0.currentTime >= strip0loop0.cycles) {strip0loop0.currentTime = 0; ret |= 0x02;}
    }
    else {
      strip0loop0.currentChild++;
    }
  };
  return ret;
}

uint8_t strip0_loop0_eff0() {
    // Strip ID: 0 - Effect: Rainbow - LEDS: 40
    // Steps: 40 - Delay: 20
    // Colors: 3 (255.0.88, 255.67.255, 113.0.255)
    // Options: rainbowlen=40, toLeft=true, 
  if(millis() - strip_0.effStart < 20 * (strip_0.effStep)) return 0x00;
  float factor1, factor2;
  uint16_t ind;
  for(uint16_t j=0;j<40;j++) {
    ind = strip_0.effStep + j * 1;
    switch((int)((ind % 40) / 13.333333333333334)) {
      case 0: factor1 = 1.0 - ((float)(ind % 40 - 0 * 13.333333333333334) / 13.333333333333334);
              factor2 = (float)((int)(ind - 0) % 40) / 13.333333333333334;
              strip_0.strip.setPixelColor(j, 255 * factor1 + 255 * factor2, 0 * factor1 + 67 * factor2, 88 * factor1 + 255 * factor2);
              break;
      case 1: factor1 = 1.0 - ((float)(ind % 40 - 1 * 13.333333333333334) / 13.333333333333334);
              factor2 = (float)((int)(ind - 13.333333333333334) % 40) / 13.333333333333334;
              strip_0.strip.setPixelColor(j, 255 * factor1 + 113 * factor2, 67 * factor1 + 0 * factor2, 255 * factor1 + 255 * factor2);
              break;
      case 2: factor1 = 1.0 - ((float)(ind % 40 - 2 * 13.333333333333334) / 13.333333333333334);
              factor2 = (float)((int)(ind - 26.666666666666668) % 40) / 13.333333333333334;
              strip_0.strip.setPixelColor(j, 113 * factor1 + 255 * factor2, 0 * factor1 + 0 * factor2, 255 * factor1 + 88 * factor2);
              break;
    }
  }
  if(strip_0.effStep >= 40) {strip_0.Reset(); return 0x03; }
  else strip_0.effStep++;
  return 0x01;
}

When I connect my prop to the PC via the USB cable everything works but when I plug it off and turn on my switch the animation is gone. The LEDs still light up but without the animation.

@michi160200
Vin need to be between 7V and 12V, A 3.7V battery won't work.
Also don't power an LED strip from the nano 5V output.
What strip are you using?

What is the voltage appied to Vin? The votage appied yo Vin must be greater than 7V for the internal 5V regulator to regulate properly. If the outpout of the Baltoy (sic) is regulated 5:V, connect it to the 5V pin.

I watched some tutorials on YouTube and thought a 3.7V battery would work.
I'm using a WS2812B LED strip which I ordered on Amazon.

How many LEDs are on the strip?
Maybe we can come up with an easy way to do this.

3.7V into Vin will not work.

WS2812 LED strips need lots of current if fully it. Each pixel (a pixel is made up of a red, a green and a blue LED) will draw about 60mA when fully lit white (all LEDs on 100%). So your 5V supply must be able to supply enough current as well as maintain 5V.

A 60 pixel strip fully it white will consume nearly 4A.

So would it work if I added a second battery?

No, use a 5V regulated power supply that can provide several Amperes.

I cut it to 80 LEDs.
Thank you so much for your help

The rule of thumb for WS2812B LED strips is 5V, 60mA per pixel.

For 80 pixels, the current consumption is 4.8 Amperes, so you need a 5V 5A power supply at minimum. You might be able to use a 5V 5A power bank, but it won't last long at full brightness.

So you will need a little more than 4A for LEDs
Does this need to be run from a battery, or will an AC power supply be OK?
If it need to be a battery, you have a few options.

It should last around 5 hours until I have to recharge it. Why wouldn't it work if I added another 3.7V battery? Wouldn't that make 7.4V with 7400mAh? I also thought about a power bank but they're too big for the project to hide them properly.

The strip and the Arduino run on 5V.

Voltage adds for batteries in series, but capacity (mAh) stays the same.
Capacity (mAh) adds for batteries in parallel, but the voltage stays the same.

It should last around 5 hours

5V at 5A = 25 W, so you will need 125 watt-hours of energy storage, which is a lot. If the output voltage is 5V, you need a 25 Ah (25000 mAh) power pack.

4 batteries and a buck converter might last 5 hours or more

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.