[Solved] Troubleshooting Wireless Communication while battery powered

Hello All,
I have a project that is working very well when I am programming and plugged in via USB. I send some data from a bluetooth enabled device to an arduino leonardo with a bluetooth radio. The data is 6 bytes, and the code to send and receive seems to work perfectly when the arduino is powered via a USB cable.

The idea is that this arduino node will be battery powered. I am using a 9v battery for now, but eventually would change to something rechargeable, prob 3.7v lipo. I understand the capacities, currents, of these two, and the 5v supply from my laptop are all different.

When I power the arduino node from the 9v, the system does not respond to the incoming bluetooth messages (well, with simpler code, when I am just sending one byte at a time things work, but now I want to be able to send a data packet). The bluetooth module signals to me that it is connected and my application sending the bluetooth commands verifies that it is connected to the bluetooth module. I have a switch(case) structure that runs different code based on the incoming message, but I only ever hit the default case, meaning that the arduino is not understanding the incoming message. The first byte of the message is the program to run. If I look at the serial monitor while connected via USB, this comes in beautifully. I've omitted the specific program codes for brevity, but at the moment I am changing the blinking rate of an LED based on the incoming program byte.

Again, working beautifully when powered via USB, not so much when connected via battery. Is the communication affected by the battery voltage? Would this cause the communications to get out of sync? What are some strategies to deal with this? Would adding capacitors to my circuit (not much of a circuit, just everything plugged into the arduino pins, battery + into Vin). Any help would be greatly appreciated!

Here is the basic code on the arduino:

byte data[6];
byte prog = 0;

void setup()
{

  Serial.begin(9600);
  Serial1.begin(38400); //start bluetooth

}


void loop() {
  if (Serial1.available() >= 6) // this will only be true if there are at least 9 bytes to read
  {
    for (int i=0; i <=5 ; i++)
    {
      data[i] = Serial1.read(); 
    }

    prog = data[0];

    /*
    Serial.print(data[0]);
     Serial.print(",");
     Serial.print(data[1]);
     Serial.print(",");
     Serial.print(data[2]);
     Serial.print(",");
     Serial.print(data[3]);
     Serial.print(",");
     Serial.print(data[4]);
     Serial.print(",");
     Serial.println(data[5]);
     */
  } 
    program(prog);
}

void program(byte p )
{
  switch(p)
  {
  case 1:      
   //do program 1
    break;

  case 2:
    //do program 2
    break;

  case 3:
    //do program 3
    break;

  case 4:
    //do program 4
    break;

  case 5:
    //do program 5
    break;

  case 6:
    //do program 6
    break;

  default:
    //do default program
    break;
  }
}

When the arduino is on battery power, does it sync? If it does sync, what is the current going to the BT module? (compare with current from USB)

wisof:
Again, working beautifully when powered via USB, not so much when connected via battery. Is the communication affected by the battery voltage?

If the volts are actually there, no, but if you are using a compact 9v battery, they probably aren't and there lies lies your problem. The problem is the in the ability to deliver the volts under load, rather than just what it says on the label, and 9v batteries are usually the kiss of death for an Arduino project. It's not so much that Arduinos are power hogs, more that the batteries are useless for practically anything but ultra-low power devices like smoke detectors, and the world would be a better place if they were made illegal. A pack of 6xAAs would be worth a try to prove the point.

Thanks for the replies.
@HazardsMind Here are the voltage and current readings:

USB (in sync):    current into BT 2.04 - 15.96 mA, voltage 4.02 - 4.89 v
4 x AA (not in sync):        current into BT 2.25 - 26.4 mA, voltage 3.92v
4 x AA (in sync):             current into BT 2.06 - 20.00 mA, voltage 3.85 - 4.17v
9v (not in sync):              current into BT 2.21 - 14.27 mA, voltage 4.17v
9v (in sync):                   current into BT 2.27 - 15.00 mA, voltage 3.87 - 6.24v

I see that if I turn on the power, then after a few seconds, hit the reset button, then the communication seems to be ok in all cases (hence why there are battery scenarios now that i can classify as in sync as the communication is getting through).

So it seems communication is going through in all cases, but I must hit the reset button to get things synced (on battery, not on usb). Why would this be? Are there some strategies in the code that I can use to ensure the packet is synced?

In the end, I think this was more an issue with syncing than anything. I still do not know why on battery it was more difficult to sync, but I've rewritten the code to listen for an end of packet character, and this seems to be working much better in all scenarios. For reference, here is the code:

byte data[6];
byte prog = 0;

void setup()
{

  Serial.begin(9600);
  Serial1.begin(38400); //start bluetooth

}


byte inByte;
  if (Serial1.available() >= 6) //the packet I am sending is 5 bytes plus the EOP which is 255
  {
    inByte = Serial1.read();
    data[0] = inByte;

    while(inByte != 255)
    {
      inByte = Serial1.read();
      cnt++;
      data[cnt] = inByte;
    }
    cnt=0;
    prog = data[0];
 
    program(prog);
  } 
  else {
    program(prog);
  }
}

void program(byte p )
{
  switch(p)
  {
  case 1:      
   //do program 1
    break;

  case 2:
    //do program 2
    break;

  case 3:
    //do program 3
    break;

  case 4:
    //do program 4
    break;

  case 5:
    //do program 5
    break;

  case 6:
    //do program 6
    break;

  default:
    //do default program
    break;
  }
}