Pages: [1]   Go Down
Author Topic: With serial, my program works the way I want it; without serial, it doesn't  (Read 870 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Quote
#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:
Code:
#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.
« Last Edit: April 29, 2012, 12:38:19 pm by teamhydraucd » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12456
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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)
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 509
Posts: 31458
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 509
Posts: 31458
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 159
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 2
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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?

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

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

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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 509
Posts: 31458
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 509
Posts: 31458
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: