Project coding Question

Hi, This is something I would normally do myself but as of yet I have not found out how to code my 2 dc motors (3v to 12v) to run independently speeding up and slowing down at a random rate. I am thinking of running it through my motor shield just in case it may overload the Arduino (Mega 2560). any help will be appreciated. Thanks

Sincerely,

Matthew

What have you managed to do using the motor shield ?

Run one motor forward and reverse ?
Run one motor forward and reverse fast and slow ?
Run two motors forward and reverse ?
Run two motors forward and reverse fast and slow ?
Any of the above ?

Ok I have Considered your questions and thinking how I missed those details but to the point...

2 motors will be close to each other and what I want them to do is spin in one direction (both in the same direction at same speed mayby one delayed from the other by 1 or 2 sec) slowly and then work faster then slower, stopping every now and then at a random rate. The motor shield will run 4 dc motors at 3v to 36v and can handle the current of the motors. The shield can also run 2 stepper motors and 2 servos if desired and that I wont be needing in this anyway.

Thanks for the help I really appreciate it

sincerely,

Matthew

mzise1:
Ok I have Considered your questions and thinking how I missed those details but to the point...

2 motors will be close to each other and what I want them to do is spin in one direction (both in the same direction at same speed mayby one delayed from the other by 1 or 2 sec) slowly and then work faster then slower, stopping every now and then at a random rate. The motor shield will run 4 dc motors at 3v to 36v and can handle the current of the motors. The shield can also run 2 stepper motors and 2 servos if desired and that I wont be needing in this anyway.

Thanks for the help I really appreciate it

sincerely,

Matthew

Very interesting, but what about the questions I asked ?
Put more simply, have you managed to get a motor running using the motor shield ? Is there any sample code associated with the motor shield that you could use ?

Ok thanks for the help but here is a motor test sketch for the shield (can run 4 at a time):

#include <AFMotor.h>

AF_DCMotor motor(4);

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor test!");

// turn on motor
motor.setSpeed(200);

motor.run(RELEASE);
}

void loop() {
uint8_t i;

Serial.print("tick");

motor.run(FORWARD);
for (i=0; i<255; i++) {
motor.setSpeed(i);
delay(10);
}

for (i=255; i!=0; i--) {
motor.setSpeed(i);
delay(10);
}

Serial.print("tock");

motor.run(BACKWARD);
for (i=0; i<255; i++) {
motor.setSpeed(i);
delay(10);
}

for (i=255; i!=0; i--) {
motor.setSpeed(i);
delay(10);
}

Serial.print("tech");
motor.run(RELEASE);
delay(1000);
}

Hope this helps

sincerely,

Matthew

This is like pulling teeth.
Have you run that program and did it do anything ?

I have run the program and it worked running the motor forward faster then at its peak slows it down again till it goes into a reverse direction and repeats this. If it is possible to run the motors directly from the arduino's pins I can try that out but will it overload the board? the motors are larger than a normal 3v hobby motor and are ones out of an old printer and do take a bit of power when no resistance is added. If there is resistance added will this help the Arduino run the motors without overloading the board? Thanks

Have you ever taken apart an old mobile phone and found the vibra motor?
They're usually about 6mm square in cross-section, maybe about 20mm long, with an eccentric weight mounted on the shaft.

Would it surprise you to know that you couldn't connect one of those to an AVR output pin without overloading it?

oh really?? wow I wouldn't have thought that would overload the arduino. well I will play around with some sketches and find out a way to do what I want and hopefully I will be able to make something. Thanks for all of the help.

One last question what kind of mosfets can i use in a 3 to 24v range?? any recommendations?

I have run the program and it worked running the motor forward faster then at its peak slows it down again till it goes into a reverse direction and repeats this.

Good, because that is what it is supposed to do. Do you understand why/how it works ? Did you try changing the program in any way to make it do something different or the same thing but slower or faster maybe ?

By experimenting like this by changing code that works you will understand better what is possible and how. If an experiment does not work then try to work out why and try again. You want motors to speed up and slow down at a random rate. What is it about the program that controls the rate of speeding up and slowing down ? Can you change it ? Can you make it random ?

At some point you will need to stop using the delay() function but that can come later.

I have noticed the delay and people talking about not using it but what are the disadvantages of using it?
Also what can I use instead?

delay() "does what it says on the tin" it delays the program until the delay is over.

So, you want to run 2 motors at random speeds and directions. You start motor 1 and delay() for a random time. Nothing else happens until the delay() is over so how do you start/stop motor 2 ?

The BlinkWithoutDelay example in the IDE shows how to use millis() to get the current time since the Arduino started and compare it with the elapsed time since an action, such as starting a motor, began. Only if the elapsed time exceeds the required period does the code do anything else, so it can move on to check other (different) elapsed times.

start motor1
save startTime1
define period1
start motor2
save startTime2
define period2

start of loop
  if millis() - startTime1 >= period1
    stop motor1
    //other actions such as starting motor 1 again etc
  end if
  
  if millis() - startTime2 >= period2
    stop motor2
    //other actions such as starting motor 2 again etc
  end if
end of loop

Code like this runs freely instead of stalling as it would using delay(). The actions when the periods elapse are up to you but if you start a new timed period remember to save the new start time.

ok thanks for the help i have a good understanding of where i can start now.
Thanks