go away spammer

Author Topic: Making robot accelarate gradually  (Read 1662 times)

0 Members and 1 Guest are viewing this topic.

Offline amando96Topic starter

  • Robot Overlord
  • ****
  • Posts: 187
  • Helpful? 1
Making robot accelarate gradually
« on: April 11, 2010, 05:06:53 AM »
Hi guys, well, my robot has a few mechanical issues, when it goes straight ahead at max speed, it does a wheelie, i have to get another ball castor for the rear..

anyway, i would like it to accelerate gradually, instead of going at max speed really quickly, i made this code:
Code: [Select]


// ________________________________________ Sensors
#define SharpIR 0
// —————————————————————————  Motors
int motor_left[] = {3,5};
int motor_right[] = {6,9};
int Speed = 0;

// ————————————————————————— Setup
void setup() {
  int i;
  for(i = 0; i < 2; i++){
    pinMode(motor_left[i], OUTPUT);
    pinMode(motor_right[i], OUTPUT);
  }

}

// ————————————————————————— Loop
void loop() {
  if(analogRead(SharpIR) > 120){
    drive_backward();
    delay(100);
    turn_left();
    delay(300);
  }
  else{
    accelerate();
  }
}


// ————————————————————————— Drive
void accelerate() {
  Speed = 0;
    digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], Speed);
 digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], Speed);
  delay(300);
  Speed = 10;
    digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], Speed);
 digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], Speed);
  delay(300);
  Speed = 20;
    digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], Speed);
 digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], Speed);
  delay(300);
  Speed = 40;
    digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], Speed);
 digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], Speed);
  delay(300);
  Speed = 60;
    digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], Speed);
 digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], Speed);
  delay(300);
  Speed = 80;
    digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], Speed);
 digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], Speed);
  delay(300);
  Speed = 120;
    digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], Speed);
 digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], Speed);
  delay(300);
  Speed = 160;
    digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], Speed);
 digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], Speed);
  delay(300);
  Speed = 180;
    digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], Speed);
 digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], Speed);
  delay(300);
  Speed = 230;
    digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], Speed);
 digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], Speed);
  delay(300);
  Speed = 255;
    digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], Speed);
 digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], Speed);
  delay(300);

}
  void motor_stop(){
  digitalWrite(motor_left[0], LOW);
  digitalWrite(motor_left[1], LOW);

  digitalWrite(motor_right[0], LOW);
  digitalWrite(motor_right[1], LOW);
  delay(25);
}

void drive_forward(){

  digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], 90);

  digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], 90);

}

void drive_backward(){
  analogWrite(motor_left[0], 90);
  digitalWrite(motor_left[1], LOW);

  analogWrite(motor_right[0], 90);
  digitalWrite(motor_right[1], LOW);

}

void turn_left(){
  digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], 90);

  analogWrite(motor_right[0], 90);
  digitalWrite(motor_right[1], LOW);
}

void turn_right(){
  analogWrite(motor_left[0], 90);
  digitalWrite(motor_left[1], LOW);

  digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], 90);
}
and it works great, BUT see the void accelerate()? all those delays mess with the reading from the sensor, and eventually hits something...
i have tried using this way:
Code: [Select]

  for(Speed = 0; Speed < 180; Speed += 1)
  digitalWrite(motor_left[0], LOW);
  analogWrite(motor_left[1], Speed);
  digitalWrite(motor_right[0], LOW);
  analogWrite(motor_right[1], Speed);
  delay(15);

but it accelarates very quickly, should i use an array of predefined PWM ''Speeds'' and just make it go through all of them? how can i control the delay betwen speed presets, without messing with my sensor readings?

thank you :)

EDIT: going to try and do this by using the millis() not sure if it will work
« Last Edit: April 11, 2010, 05:50:56 AM by amando96 »
Rorcle, 60% complete
AATV, 5% complete

Offline corrado33

  • Supreme Robot
  • *****
  • Posts: 611
  • Helpful? 11
Re: Making robot accelarate gradually
« Reply #1 on: April 11, 2010, 10:45:39 AM »
Quote
all those delays mess with the reading from the sensor

Is that because the program essentially pauses while the delay is going on?  When the program is pausing the robot is still moving forward, and eventually hits something?  Have you tried using a timer with an interrupt?  Every time the interrupt hits you could increment the speed, but while it's waiting to go to the next speed, the program (I.E. your sensors) still works. 

Of course this assumes that you have a timer available, and your PWM isn't using all of them. 

 


Get Your Ad Here

data_list