Go Down

Topic: Motors only run once? [SOLVED] (Read 997 times) previous topic - next topic

Feb 24, 2013, 11:22 am Last Edit: Feb 24, 2013, 11:53 pm by liquidlightning Reason: 1
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: [Select]
#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);
}

Drew Davis



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

SurferTim

#2
Feb 24, 2013, 02:51 pm Last Edit: Feb 24, 2013, 06:21 pm by SurferTim Reason: 1
You are not initializing these variables in both functions. That will cause unexpected results.
Code: [Select]
unsigned long endTime;
unsigned long timeElapsed;


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

Code: [Select]
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);
}


I see. So how should I change it?

Drew Davis

#4
Feb 24, 2013, 07:51 pm Last Edit: Feb 24, 2013, 08:00 pm by Drew Davis Reason: 1
This should set you on the right trackā€¦
http://arduino.cc/en/Reference/VariableDeclaration

Code: [Select]
#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);
}

SurferTim

#5
Feb 24, 2013, 08:53 pm Last Edit: Feb 24, 2013, 08:57 pm by SurferTim Reason: 1
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.

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

Code: [Select]
#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);
}

Go Up