Go Down

Topic: Help With Some Code (Read 2384 times) previous topic - next topic

Jamie77

Hello,

I currenctly have the adafruit motor shield connected the arduino UNO R3 and this is the code I have:

#include <AFMotor.h>

AF_DCMotor motor(1);
void setup() {
  Serial.begin(9600);
  Serial.flush();
  motor.setSpeed(200);
  motor.run(RELEASE);
}
void loop(){
  if (Serial.available()) {

    char ser = Serial.read();

   if(ser == '1'){
    Serial.print("Backward");
    motor.run(BACKWARD);//Put The Motor Going Backward
   }
  }
}

I am trying to run through serial and it works but the motor only stays on for a couple of seconds and goes off and I don't know how to keep it on.

Many Thanks

Jamie

johnwasser


I am trying to run through serial and it works but the motor only stays on for a couple of seconds and goes off and I don't know how to keep it on.


Since you don't have any code to stop the motor except in a reset my guesses would be:

A) The motor is causing a load that causes a reset.

B) The motor may be drawing too much current and causing the controller chip to overheat and shut down.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Grumpy_Mike

Most likely the motor is generating interference that is resetting the arduino. Try capacitors across the supply or power it with a separate supply.

Jamie77

Hello,

Tgank you both for your answers. I currently have the arduino running off usb and the motor shield iz runnin on six AA nimh batteries. So its already running on sparate power supply. And just to double check that the code ia fine then.

Many Thanks

Jamie

Grumpy_Mike

Quote
And just to double check that the code ia fine then.

Well apart from the fact:-
1) It is not posted correctly - read the how to use this forum post.
2) that you only turn it on not off.

I could be the speed is too low set by
Code: [Select]
motor.setSpeed(200);

Jamie77

Hello,

Sorry I thought it was about motors but anyway so I do have enough power im gessig for 4.5v motor and if I add a capcitor where abouts do I put it and will this stop any problems.

Many Thanks

Jamie

wildbill

Before changing your hardware setup, I'd suggest printing something in setup so that you can see whether it's actually resetting.

Jamie77

Hello,

Thank you for your answer again. How would I do that and where would it need to go in the code please.

Many Thanks

Jamie

wildbill

Copy the Serial.print line you have in loop into the setup function and change the string it sends to something different. Make sure it's after the Serial.begin.

Jamie77

Ok thanks for that I will try that and get back to you if it does not work

Many Thanks

Jamie

Jamie77

Hello,

It does not seem to be resetting as it only prints the line once, but I have found that it crashed on the serial window after you put the command in a few times.

Many Thanks

Jamie

Jamie77

Hello,

Could someone please help its annoying me now as I just cant get the motor to stay on.

Many Thanks

Jamie

wildbill

I suspect your issue is a lack of power, but there are other possibilities. Try a simpler sketch that just sets the speed and runs the motor forward in setup - not sure why you are using RELEASE.

Jamie77

Hello,

Thank you for your response again. I have ran a simple code before and just done it again and the motor runs fine. One thing I have noticed that in the example adafruit give you they put a freqeuncy or something for each motor so I dont know if that has anything to do with it.

Many Thanks

Jamie

Go Up