Go Down

Topic: Autonomous Truck Code Help, Making turn BOTH Directions. (Read 877 times) previous topic - next topic

cherryguy

So, you may have seen my other post about my power source problems. Those now being resolved, I realized that the code I got from the internet (http://www.instructables.com/id/RC-Car-to-Robot/?ALLSTEPS) makes the car turn right every time an obstacle is detected by the ping))) sensor. I would like it so that the code tells it to turn right or left randomly. Arduino coding is new to me so I seek your help. I don't want it to find an obstacle, turn right and back up and then find another obstacle and turn left and back up. Like I said, it would be nice if it had some randomness to it, like if it wanted to it could turn right two times in a row. I don't want it to alternate which direction it turns. Here's the code I am using.
Code: [Select]
/*
RC Car to Robot Conversion
by Randy Sarafan

Used to convert an RC car into a robot that uses a PING sensor to avoid obstacles,
and an Arduino motor shield for motor control.

For more information see:
http://www.instructables.com/id/RC-Car-to-Robot/

Built atop Ping example code by Tom Igoe
*/

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;

void setup() {
 
  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //drive motor -- HIGH = forwards and LOW = backwards
  pinMode(13, OUTPUT); //turn motor -- HIGH = left and LOW = right
 
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) the drive motor
  pinMode(8, OUTPUT); //brake (disable) the turn motor

  //Turns brake off for drive motor
  digitalWrite(9, LOW);

  //Turns brake on for turn motor
  digitalWrite(8, HIGH);

  //Sets initial speed of drive motor
  analogWrite(3, 200);
 
  //Sets initial direction of drive motor
  digitalWrite(12, HIGH);
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, 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);

  // convert the time into a distance
  inches = microsecondsToInches(duration);

  //
  //if objects are less than 12 inches away
  //the robot reverses and turns to the right
  //for 2 seconds
  //
 
  if (inches < 24n  ){
   
    //brake drive motor and pause 1/10 second
    digitalWrite(9, HIGH);
    delay(100);

    //
    //setting turn motor
    //
   
    //turn off brake for turn motor
    digitalWrite(8, LOW);

    //set turn motor direction
    digitalWrite(13, HIGH);

    //activate turn motor
    analogWrite(11, 255);
   
    //
    //setting drive motor
    //
   
    //turn off brake of drive motor
    digitalWrite(9, LOW);
   
    //set drive motor backwards direction
    digitalWrite(12, LOW);
   
    //activate the drive motor
    analogWrite(3, 200);

   
    //backup for 2 seconds
    delay(2000);
   
    //
    //stopping
    //
   
    //brake both motors
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
   
   
  }
 
  //
  //when nothing is within 12"
  //the robot simply drives forwards
  //
 
  else{
   
    //
    //Setting drive motor
    //
   
    //set drive motor forward direction
    digitalWrite(12, HIGH);
   
    //turn off brake of drive motor
    digitalWrite(9, LOW);   
   
    //activate drive motor
    analogWrite(3, 200);
 
 
  }
 
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

AWOL

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

cherryguy


Code: [Select]
inches < 24n 
?

Distance from obstacle maximum to trigger the backing up. If theres an obstacle 3 feet away, it will ignore it until its 2 feet away. Oh there is a typo. It should be:
Code: [Select]
if (inches < 24){

AWOL

But the compiler would've highlighted the typo.
You did compile it before you posted it, didn't you?
"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.

PaulS

Quote
Like I said, it would be nice if it had some randomness to it, like if it wanted to it could turn right two times in a row. I don't want it to alternate which direction it turns.

You mean like if it flipped a coin? Perhaps you could put colored tape on one side of the coin, add a servo to flip the coin, and a color sensor to read the color.

Or, use the random() function.

cherryguy


But the compiler would've highlighted the typo.
You did compile it before you posted it, didn't you?

Yah I don't know why it didn't detect anything wrong with it but I fixed it and its uploaded onto my Uno. Still only turns right. I need this code spelled out to me please... thanks.

PaulS

Quote
I need this code spelled out to me please

No, you need to do the research yourself. It's the only way to learn. There are only a handful of functions being called. Figure out which ones make the truck move forward, which ones make it move backwards, which ones make it turn whichever way it turns.

When you know what makes it turn, change the arguments to make it turn the other way, instead.

Then, you can figure out how random works, and make the truck turn randomly.

cherryguy


No, you need to do the research yourself. It's the only way to learn. There are only a handful of functions being called. Figure out which ones make the truck move forward, which ones make it move backwards, which ones make it turn whichever way it turns.

When you know what makes it turn, change the arguments to make it turn the other way, instead.

Then, you can figure out how random works, and make the truck turn randomly.

I'm sorry but I'm really at a loss. Any hints on how to do this?

cherryguy


When you know what makes it turn, change the arguments to make it turn the other way, instead.
Then, you can figure out how random works, and make the truck turn randomly.


Thanks, I got it working. If anyone would like the code I used quote me on this thread.

Go Up