Go Down

Topic: start up speed of dc motors for an autonomous robot (Read 851 times) previous topic - next topic


Hi im kinda new to arduino and i was wondering is there any way to write a program to have my robot start out slow and after few seconds to go full speed? because the whole thing is autonomous i cant use any H bridges or anything else to control the speed myself.

Here is the code that i have for now:

#include <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_64KHZ); // left create motor #2, 64KHz pwm
AF_DCMotor motor4(4, MOTOR12_64KHZ);// right
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
pinMode(16, INPUT); //right
pinMode(15, INPUT); //center
pinMode(14, INPUT); //left
pinMode(2, OUTPUT);//the buzzer
  motor1.setSpeed(200);  // set the speed of right motor to 200/255
  motor4.setSpeed(200); // set the speed of left motor to 200/255
const int pingPin = 19;
int i;
void loop()
  long duration, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  digitalWrite(pingPin, HIGH);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
cm = microsecondsToCentimeters(duration);
if (cm <= 80){
  for (i=0; i<255; i++) {
if (digitalRead(15)==HIGH && digitalRead(14)==HIGH && digitalRead(16)==HIGH){
if (digitalRead(15)==LOW && digitalRead(16)==HIGH && digitalRead(14)==HIGH){
      motor1.run(FORWARD);      // turn it on going forward
      motor4.run(FORWARD);      // turn it on going forward
if (digitalRead(16)==LOW && digitalRead(15)==HIGH && digitalRead(14)==HIGH){          //if IF signal at the right side is detected rigth motor stops and left motor moves forward

if (digitalRead(14)==LOW && digitalRead(16)==HIGH && digitalRead(15)==HIGH){         //if IF signal at the left side is detected left motor stops and right motor moves forward

long microsecondsToCentimeters(long microseconds)
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;


also while alittle related to the topic, i was also wondering can i use 3 ultrasonic sensors insead of 1?


because the whole thing is autonomous i cant use any H bridges or anything else to control the speed myself.

Can you explain what that means, please?

i was also wondering can i use 3 ultrasonic sensors insead of 1?

In the immortal words of Cyndi Lauper "ain't no law against it yet"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


You could do it using a for() loop, but what I am doing in my robot, is checking how far the nearest object at 0 degrees is, if it is over x cm go high speed, otherwise stay at low speed.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131