Pages: [1]   Go Down
Author Topic: Motors only run once? [SOLVED]  (Read 930 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

So, I'm making a robot from this chassis:
http://www.dfrobot.com/index.php?route=product/product&path=37_111&product_id=97#.USnoqDDFVMQ

And an Arduino Mega 2560 / ADAFruit motor shield, and I've been having a problem.
If I try to run the motors, they run fine. As soon as you introduce a delay however, they start to twitch and not move at all (while connected to 5AA batt pack), if I switch over to arduino power from USB, they work (but very weakly due to low current).
To get around this I made a function with a for loop to act instead of a delay. What now happens is that it will run the function perfectly - the FIRST time. After that it won't even budge until arduino is reset.

Here is my code:

Code:
#include <AFMotor.h>

AF_DCMotor motorBL(1, MOTOR12_64KHZ);
AF_DCMotor motorBR(2, MOTOR12_64KHZ);
AF_DCMotor motorFR(3, MOTOR12_64KHZ);
AF_DCMotor motorFL(4, MOTOR12_64KHZ);

void setup()
{
  motorBL.setSpeed(255);
  motorBR.setSpeed(255);
  motorFR.setSpeed(255);
  motorFL.setSpeed(255);
}

void loop()
{
  turnLeft(500,255);
  delay(500);
  turnRight(500,255);
  delay(500);
}

void turnLeft(unsigned long time, int motorSpeed)
{
  motorBL.setSpeed(motorSpeed);
  motorBR.setSpeed(motorSpeed);
  motorFR.setSpeed(motorSpeed);
  motorFL.setSpeed(motorSpeed);
  
  unsigned long stTime = millis();
  unsigned long endTime;
  for(unsigned long timeElapsed; timeElapsed < time; timeElapsed = (endTime - stTime))
  {
    motorBL.run(BACKWARD);
    motorBR.run(FORWARD);
    motorFR.run(FORWARD);
    motorFL.run(BACKWARD);
    endTime = millis();
  }
  motorBL.run(RELEASE);
  motorBR.run(RELEASE);
  motorFR.run(RELEASE);
  motorFL.run(RELEASE);
}

void turnRight(unsigned long time, int motorSpeed)
{
  motorBL.setSpeed(motorSpeed);
  motorBR.setSpeed(motorSpeed);
  motorFR.setSpeed(motorSpeed);
  motorFL.setSpeed(motorSpeed);
  
  unsigned long stTime = millis();
  unsigned long endTime;
  for(unsigned long timeElapsed; timeElapsed < time; timeElapsed = (endTime - stTime))
  {
    motorBL.run(FORWARD);
    motorBR.run(BACKWARD);
    motorFR.run(BACKWARD);
    motorFL.run(FORWARD);
    endTime = millis();
  }
  motorBL.run(RELEASE);
  motorBR.run(RELEASE);
  motorFR.run(RELEASE);
  motorFL.run(RELEASE);
}
« Last Edit: February 24, 2013, 05:53:40 pm by liquidlightning » Logged

Florida
Offline Offline
God Member
*****
Karma: 2
Posts: 559
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



If you were using a motor controller I would guess their was a problem with how you grounded everything, but I don't know about the shield. 

Try using the USB and the AA Pack at the same time and tell us what happens.


Thanks,
Drew
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 149
Posts: 6119
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are not initializing these variables in both functions. That will cause unexpected results.
Code:
unsigned long endTime;
unsigned long timeElapsed;

edit: Here is a sketch that may help demonstrate the unexpected results.

Code:
void setup() {
   Serial.begin(9600);
}

void loop() {
  Serial.print("tBuf = ");
  initFunct();
  Serial.print("xBuf = ");
  noinitFunct();
  Serial.println();
  delay(1000);
}

void initFunct() {
  char tBuf[24] = "testing";
  Serial.println(tBuf);
}

void noinitFunct() {
  char xBuf[24];
  // if you expect xBuf to be empty, SURPRISE!!
  Serial.println(xBuf);
}
« Last Edit: February 24, 2013, 12:21:27 pm by SurferTim » Logged

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

I see. So how should I change it?
Logged

Florida
Offline Offline
God Member
*****
Karma: 2
Posts: 559
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This should set you on the right track…
http://arduino.cc/en/Reference/VariableDeclaration

Code:
#include <AFMotor.h>

AF_DCMotor motorBL(1, MOTOR12_64KHZ);
AF_DCMotor motorBR(2, MOTOR12_64KHZ);
AF_DCMotor motorFR(3, MOTOR12_64KHZ);
AF_DCMotor motorFL(4, MOTOR12_64KHZ);




unsigned long endTime ;
unsigned long timeElapsed ;

void setup()
{
  motorBL.setSpeed(255);
  motorBR.setSpeed(255);
  motorFR.setSpeed(255);
  motorFL.setSpeed(255);
}

void loop()
{
  turnLeft(500,255);
  delay(500);
  turnRight(500,255);
  delay(500);
}

void turnLeft(unsigned long time, int motorSpeed)
{
  motorBL.setSpeed(motorSpeed);
  motorBR.setSpeed(motorSpeed);
  motorFR.setSpeed(motorSpeed);
  motorFL.setSpeed(motorSpeed);
 
  unsigned long stTime = millis();
  unsigned long endTime;
  for(unsigned long timeElapsed; timeElapsed < time; timeElapsed = (endTime - stTime))
  {
    motorBL.run(BACKWARD);
    motorBR.run(FORWARD);
    motorFR.run(FORWARD);
    motorFL.run(BACKWARD);
    endTime = millis();
  }
  motorBL.run(RELEASE);
  motorBR.run(RELEASE);
  motorFR.run(RELEASE);
  motorFL.run(RELEASE);
}

void turnRight(unsigned long time, int motorSpeed)
{
  motorBL.setSpeed(motorSpeed);
  motorBR.setSpeed(motorSpeed);
  motorFR.setSpeed(motorSpeed);
  motorFL.setSpeed(motorSpeed);
 
  unsigned long stTime = millis();
  unsigned long endTime;
  for(unsigned long timeElapsed; timeElapsed < time; timeElapsed = (endTime - stTime))
  {
    motorBL.run(FORWARD);
    motorBR.run(BACKWARD);
    motorFR.run(BACKWARD);
    motorFL.run(FORWARD);
    endTime = millis();
  }
  motorBL.run(RELEASE);
  motorBR.run(RELEASE);
  motorFR.run(RELEASE);
  motorFL.run(RELEASE);
}
« Last Edit: February 24, 2013, 02:00:02 pm by Drew Davis » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 149
Posts: 6119
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would initialize both, even as global. (add: If you use the variables as global) insure you remove the declarations of those variables in the functions. Local variables have precedence over global.
« Last Edit: February 24, 2013, 02:57:13 pm by SurferTim » Logged

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

Thanks guys, that seems to have fixed it! Here is my updated code:

Code:
#include <AFMotor.h>

AF_DCMotor motorBL(1, MOTOR12_64KHZ);
AF_DCMotor motorBR(2, MOTOR12_64KHZ);
AF_DCMotor motorFR(3, MOTOR12_64KHZ);
AF_DCMotor motorFL(4, MOTOR12_64KHZ);

void setup()
{
  motorBL.setSpeed(255);
  motorBR.setSpeed(255);
  motorFR.setSpeed(255);
  motorFL.setSpeed(255);
}

void loop()
{
  turnRight(2000,255);
  turnLeft(2000,255);
}

void turnLeft(unsigned long time,int motorSpeed)
{
  motorBL.setSpeed(motorSpeed);
  motorBR.setSpeed(motorSpeed);
  motorFR.setSpeed(motorSpeed);
  motorFL.setSpeed(motorSpeed);
 
  unsigned long stTime = millis();
  unsigned long endTime = 1;
  for(unsigned long timeElapsed = 1; timeElapsed < time; timeElapsed = (endTime - stTime))
  {
    motorBL.run(BACKWARD);
    motorBR.run(FORWARD);
    motorFR.run(FORWARD);
    motorFL.run(BACKWARD);
    endTime = millis();
  }
  motorBL.run(RELEASE);
  motorBR.run(RELEASE);
  motorFR.run(RELEASE);
  motorFL.run(RELEASE);
}

void turnRight(unsigned long time,int motorSpeed)
{
  motorBL.setSpeed(motorSpeed);
  motorBR.setSpeed(motorSpeed);
  motorFR.setSpeed(motorSpeed);
  motorFL.setSpeed(motorSpeed);
 
  unsigned long stTime = millis();
  unsigned long endTime = 1;
  for(unsigned long timeElapsed = 1; timeElapsed < time; timeElapsed = (endTime - stTime))
  {
    motorBL.run(FORWARD);
    motorBR.run(BACKWARD);
    motorFR.run(BACKWARD);
    motorFL.run(FORWARD);
    endTime = millis();
  }
  motorBL.run(RELEASE);
  motorBR.run(RELEASE);
  motorFR.run(RELEASE);
  motorFL.run(RELEASE);
}
Logged

Pages: [1]   Go Up
Jump to: