With serial, my program works the way I want it; without serial, it doesn't

I got my program to work with a couple of buttons to drive two separate motors forward and backwards. So in total, 4 separate buttons. I used "serial.println()" to verify the buttons being pressed. Tested it and it worked the way I like.

When I tell motor A to move forward, I can simultaneously tell motor B to move backwards. It's all gravy until I remove the serial commands. Doing that, I can only do one command at a time, rather than simultaneously. Any inputs to why it's happening this way?

Code WITH serial commands and it works just fine with my microcontroller connected to my serial port. I also bolded the parts I took out with the code that sorta works:

#include <BMSerial.h>
#include <RoboClaw.h>

#define address 0x80

RoboClaw roboclaw(8,9);

// Joypad Buttons
const int upA = 2;
const int downA = 3;
const int upB = 4;
const int downB = 5;

void setup()
{
pinMode(upA, INPUT);
pinMode(downA, INPUT);
pinMode(upB, INPUT);
pinMode(downB, INPUT);

digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);

roboclaw.begin(2400);
Serial.begin(9600);
}

void loop()
{

if(digitalRead(upA) == LOW)
{
roboclaw.ForwardM1(address, 96);
Serial.println( F( "Up A" ) );
}

if(digitalRead(downA) == LOW)
{
roboclaw.BackwardM1(address, 96);
Serial.println( F( "Down A" ) );
}

if(digitalRead(upB) == LOW)
{
roboclaw.ForwardM2(address, 96);
Serial.println( F( "Up B" ) );
}

if(digitalRead(downB) == LOW)
{
roboclaw.BackwardM2(address, 96);
Serial.println( F( "Down B" ) );
;
}

if(digitalRead(upA) == HIGH && digitalRead(downA) == HIGH && digitalRead(upB) == HIGH && digitalRead(downB) == HIGH)
{
roboclaw.ForwardM1(address, 0);
roboclaw.ForwardM2(address, 0);
roboclaw.BackwardM1(address, 0);
roboclaw.BackwardM2(address, 0);
Serial.println( F( "0 Velocity" ) );
}

}

Code WITHOUT serial commands:

#include <BMSerial.h>
#include <RoboClaw.h>

#define address 0x80

RoboClaw roboclaw(8,9);

// Joypad Buttons
const int upA = 2;
const int downA = 3;
const int upB = 4;
const int downB = 5;

void setup()
{
  pinMode(upA, INPUT);
  pinMode(downA, INPUT);
  pinMode(upB, INPUT);
  pinMode(downB, INPUT);
  
  
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  
  roboclaw.begin(2400);
}

void loop()
{ 
  
    if(digitalRead(upA) == LOW)
  { 
    roboclaw.ForwardM1(address, 96);
  }

    if(digitalRead(downA) == LOW)
  {   
    roboclaw.BackwardM1(address, 96);  
  }
  
  
    if(digitalRead(upB) == LOW)
  {    
    roboclaw.ForwardM2(address, 96);  
  }
  
    if(digitalRead(downB) == LOW)
  {    
    roboclaw.BackwardM2(address, 96);
;  
  }

    if(digitalRead(upA) == HIGH && digitalRead(downA) == HIGH && digitalRead(upB) == HIGH && digitalRead(downB) == HIGH)
  { 
    roboclaw.ForwardM1(address, 0);
    roboclaw.ForwardM2(address, 0);
    roboclaw.BackwardM1(address, 0);
    roboclaw.BackwardM2(address, 0);
  }
  
}

EDIT: I checked again, the both code WORK "correctly" as long as my microcontroller's connected through the serial port.

The serial commands take some time to execute - that is the real difference.

at 9600 baud you do 1char per millisec so if you add delay of 5-10 millis I expect it to work to.

That is probably just the time needed to debounce the buttons (not sure on that BTW)

the both code WORK "correctly" as long as my microcontroller's connected through the serial port.

So it sounds like the problem is with your power supply. What is it and how is it connected?

Grumpy_Mike:

the both code WORK "correctly" as long as my microcontroller's connected through the serial port.

So it sounds like the problem is with your power supply. What is it and how is it connected?

The microcontroller is powered from a regulated 5 V (5.10 V to be exact) from the Roboclaw motor controller. And the Roboclaw motor controller's power from a 12V 14 A-hr lead acid battery.

The magic word motor implies that there is some interference getting into the circuit that is grounded when it is connected to the ground of your computer.
Try adding more decoupling:-
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

Just curious in which arduino board you are using (revision # ) ?

Perhaps the serial cable is providing a ground path that is needed but absent without it?

Grumpy_Mike:
The magic word motor implies that there is some interference getting into the circuit that is grounded when it is connected to the ground of your computer.
Try adding more decoupling:-
De-coupling

I got the gist of decoupling. So basically, there's some instability going from my battery to my motor controller to my microcontroller. In that case, where do you recommend applying capacitors?

justone:
Just curious in which arduino board you are using (revision # ) ?

I am using the Ethernet Pro microcontroller. (it's basically the Arduino Uno)

skyjumper:
Perhaps the serial cable is providing a ground path that is needed but absent without it?

Ah! That could very well be the probably. The Vcc source powering the buttons is from an ELVIS board since I've just been doing a lot of testing, but I've yet to try grounding the buttons with a common ground. I'm going to see what happens if EVERY component in my system has a common ground. What circuit reasoning can explain this?

I'm going to see what happens if EVERY component in my system has a common ground.

I will tell you unless all the grounds are common it will not work, this is why
http://www.thebox.myzen.co.uk/Tutorial/Power_Supplies.html

Decoupling capacitors should be applied on all chips and motors.

Grumpy_Mike:

I'm going to see what happens if EVERY component in my system has a common ground.

I will tell you unless all the grounds are common it will not work, this is why
Power Supplies

Decoupling capacitors should be applied on all chips and motors.

I see. My next question then is, since my RoboClaw Motor Controller has like 3 5V voltage regulators with positive and negative pins. by using that to power my microcontroller, does that mean I'm sharing a common ground with the motor controller (since the motor controller's connected directly to the battery? Also, if the microcontroller is sharing a common ground, will using the voltage regulator on microcontroller to power my buttons mean I'd be sharing a common ground as well?

Common ground means that all the grounds are connected to each other. That is there is a connection between all the points you call ground in your system.