Go Down

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

persianhero

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);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  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){
  motor1.run(BACKWARD);
  motor4.run(BACKWARD);
  for (i=0; i<255; i++) {
    motor1.setSpeed(i);
   motor4.setSpeed(i);
    delay(10);
}}else{
if (digitalRead(15)==HIGH && digitalRead(14)==HIGH && digitalRead(16)==HIGH){
motor1.run(FORWARD);
motor4.run(RELEASE);
digitalWrite(2,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
  motor1.run(RELEASE);   
  motor4.run(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
  motor1.run(FORWARD);
  motor4.run(RELEASE);
}}
 
 
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}
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;
}

persianhero

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

AWOL

Quote
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?

Quote
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.

Benji

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