Go Down

Topic: How Do I Implement Trigger and Echo Pin? (Read 1 time) previous topic - next topic

adruinouno111

How Do I Implement Trigger and Echo Pin?

My Trigger Pin Is 51
My Echo Pin Is 53
My VCC Pin is 49

Arduino Mega 2560


Code: [Select]

#include <Servo.h>
/*
* Arduino Robot Code To Detect Objects Infront Of The Ultrasonic Sensor
*/
int motor_left[] = {12, 3};
int motor_right[] = {13, 11};
int pingPin = 9;  // Declare Pin

int servoPin = 52;
int servoRange[] = {500, 2500};  // The min/max pulse lengths to send to the servo
int servoRotateTime = 500; // How long it takes to turn the servo 180 degrees

int dangerDist = 300; // Declare How Far To Stop
int servoTime = 700; // How long it takes to turn the servo
int turnTime = 250; // How long it takes to turn 90 degrees with the motors

Servo servo;

void setup(){
  Serial.begin(9600);
  printString("Start\n");


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

  // Servo
  servo.attach(2);
  servo.setMinimumPulse(500);
  servo.setMaximumPulse(2500);
  turn_servo(100);
}


void loop(){

  int dist = read_ping(); // Read Ultrasonic Sensor To Determine Distance Of Object

// No Object Dectected
  if(dist > dangerDist){
    Serial.println("All Good\n");
    drive_straight();
    delay(50);
  }
  // Object Detected
  else{
    Serial.println("Object Ahead\n");
    change_direction();
    delay(50);
  }
}

// Something is in front of us, so find the best alternate route.
// Look left and right and decide which has more distance to drive.
void change_direction(){
  motor_stop();

  // Look left
  turn_servo(180);
  int left = read_ping_avg();

  // Look right
  turn_servo(10);
  int right = read_ping_avg();

  // Neither are good options, turn around
  if(left < dangerDist &&  right < dangerDist){
    turn_right();
    delay(turnTime * 2);
    drive_straight();
  }
  // Right is better
  else if(left < right){
    turn_right();
    delay(turnTime);
    drive_straight();
  }
  // Left is better
  else {
    turn_left();
    delay(turnTime);
    drive_straight();
  }

}

// Read the distance from the Ping sensor
int read_ping(){
  int timecount = 0;

  /*
   * Send trigger pulse to ping to tell it to start
   */
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW); // Send low pulse
  delayMicroseconds(2); // Wait for 2 microseconds
  digitalWrite(pingPin, HIGH); // Send high pulse
  delayMicroseconds(5); // Wait for 5 microseconds
  digitalWrite(pingPin, LOW); // Holdoff

  /*
   * Listen for echo pulses
   */
 
  // Wait for input
  int val = digitalRead(pingPin); // Append signal value to val
  pinMode(pingPin, INPUT); // Switch signalpin to input
  while(val == LOW) { // Loop until pin reads a high value
    val = digitalRead(pingPin);
  }

  // Count pulses
  while(val == HIGH) { // Loop until pin reads a low value
    val = digitalRead(pingPin);
    timecount = timecount +1;
  }

  delay(10);

  return timecount;
}

// Get the average distance from 3 pings
int read_ping_avg(){

  int total = 0;
  total += read_ping();
  total += read_ping();
  total += read_ping();

  int avg = total/3;

  return avg;
}

// Turn the servo to a degree between 1 - 180.
void turn_servo(int degree){
  int servoStopAt = 0;

  // Set stop time
  if(servoStopAt == 0){
    servoStopAt = millis() + servoRotateTime;
  }

  servo.write(degree);

  // Refresh until the servo is done moving
  // (using approximate timing set by servoRotateTime)
  while(servoStopAt > millis()){
    delay(20);
    Servo::refresh();
  }
}

/*
* Motor Functions
*/

void motor_stop(){    // Declares Motor Stop Function Motor Will Stop
  digitalWrite(motor_left[0], HIGH);
  digitalWrite(motor_left[1], HIGH);

  digitalWrite(motor_right[0], HIGH);
  digitalWrite(motor_right[1], HIGH);
}

void drive_straight(){  // Declares Motor Start Function Motor Will Drive Straight
  turn_servo(100); // Look straight ahead

  digitalWrite(motor_left[0], LOW);
  digitalWrite(motor_left[1], HIGH);

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

void turn_left(){  // Declares Motor Turn Left Function Robot Will Turn Left
  digitalWrite(motor_left[0], HIGH);
  digitalWrite(motor_left[1], LOW);

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

void turn_right(){ // Declares Motor Turn Right Function Robot Will Turn Right
  digitalWrite(motor_left[0], LOW);
  digitalWrite(motor_left[1], HIGH);

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


kowalski

Did you try searching in "Arduino Playground"?

Here is one of the more high quality libraries that solves your problem.
http://code.google.com/p/arduino-new-ping/

Good luck with your project. Sounds like fun!

adruinouno111

I Tried Compiling I Get This Error

sketch_mar23b.ino: In function 'void setup()':
sketch_mar23b:37: error: 'class Servo' has no member named 'setMinimumPulse'
sketch_mar23b:38: error: 'class Servo' has no member named 'setMaximumPulse'
sketch_mar23b.ino: In function 'int read_ping()':
sketch_mar23b:99: error: 'val' was not declared in this scope
sketch_mar23b:121: error: expected `;' before '}' token
sketch_mar23b.ino: At global scope:
sketch_mar23b:124: error: expected unqualified-id before 'while'
sketch_mar23b:129: error: expected constructor, destructor, or type conversion before '(' token
sketch_mar23b:131: error: expected unqualified-id before 'return'
sketch_mar23b:132: error: expected declaration before '}' token


Code: [Select]

#include <Servo.h>
#include <NewPing.h>
/*
* Arduino Robot Code To Detect Objects Infront Of The Ultrasonic Sensor
*/
int motor_left[] = {12, 3};
int motor_right[] = {13, 11};
int TRIGGER_PIN = 51;
int ECHO_PIN = 53;
int MAX_DISTANCE = 400;

int servoPin = 52;
int servoRange[] = {500, 2500};  // The min/max pulse lengths to send to the servo
int servoRotateTime = 500; // How long it takes to turn the servo 180 degrees

int dangerDist = 300; // Declare How Far To Stop
int servoTime = 700; // How long it takes to turn the servo
int turnTime = 250; // How long it takes to turn 90 degrees with the motors

Servo servo;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup(){
  Serial.begin(9600);
int i;
  for(i = 0; i < 2; i++){
    pinMode(motor_left[i], OUTPUT);
    pinMode(motor_right[i], OUTPUT);
  }

  // Servo
  servo.attach(52);
  servo.setMinimumPulse(500);
  servo.setMaximumPulse(2500);
  turn_servo(100);
}


void loop(){

  int dist = read_ping(); // Read Ultrasonic Sensor To Determine Distance Of Object

// No Object Dectected
  if(dist > dangerDist){
    Serial.println("All Good\n");
    drive_straight();
    delay(50);
  }
  // Object Detected
  else{
    Serial.println("Object Ahead\n");
    change_direction();
    delay(50);
  }
}

// Something is in front of us, so find the best alternate route.
// Look left and right and decide which has more distance to drive.
void change_direction(){
  motor_stop();

  // Look left
  turn_servo(180);
  int left = read_ping_avg();

  // Look right
  turn_servo(10);
  int right = read_ping_avg();

  // Neither are good options, turn around
  if(left < dangerDist &&  right < dangerDist){
    turn_right();
    delay(turnTime * 2);
    drive_straight();
  }
  // Right is better
  else if(left < right){
    turn_right();
    delay(turnTime);
    drive_straight();
  }
  // Left is better
  else {
    turn_left();
    delay(turnTime);
    drive_straight();
  }

}

// Read the distance from the Ping sensor
int read_ping(){                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).

val = digitalRead(uS / US_ROUNDTRIP_CM) // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
  /* int timecount = 0;

  /*
   * Send trigger pulse to ping to tell it to start
   *
  pinMode(pingPin, OUTPUT);
  igitalWrite(pingPin, LOW); // Send low pulse
  delayMicroseconds(2); // Wait for 2 microseconds
  digitalWrite(pingPin, HIGH); // Send high pulse
  delayMicroseconds(5); // Wait for 5 microseconds
  digitalWrite(pingPin, LOW); // Holdoff
*/
  /*
   * Listen for echo pulses
   */
 
  // Wait for input
//  int val = digitalRead(pingPin); // Append signal value to val
  //pinMode(pingPin, INPUT); // Switch signalpin to input
  //while(val == LOW) { // Loop until pin reads a high value
  //  val = digitalRead(pingPin);
  }

  // Count pulses
  while(val == HIGH) { // Loop until pin reads a low value
    val = digitalRead(pingPin);
    timecount = timecount +1;
  }

  delay(10);

  return timecount;
}

// Get the average distance from 3 pings
int read_ping_avg(){

  int total = 0;
  total += read_ping();
  total += read_ping();
  total += read_ping();

  int avg = total/3;

  return avg;
}

// Turn the servo to a degree between 1 - 180.
void turn_servo(int degree){
  int servoStopAt = 0;

  // Set stop time
  if(servoStopAt == 0){
    servoStopAt = millis() + servoRotateTime;
  }

  servo.write(degree);

  // Refresh until the servo is done moving
  // (using approximate timing set by servoRotateTime)
  while(servoStopAt > millis()){
    delay(20);
    Servo::refresh();
  }
}

/*
* Motor Functions
*/

void motor_stop(){    // Declares Motor Stop Function Motor Will Stop
  digitalWrite(motor_left[0], HIGH);
  digitalWrite(motor_left[1], HIGH);

  digitalWrite(motor_right[0], HIGH);
  digitalWrite(motor_right[1], HIGH);
}

void drive_straight(){  // Declares Motor Start Function Motor Will Drive Straight
  turn_servo(100); // Look straight ahead

  digitalWrite(motor_left[0], LOW);
  digitalWrite(motor_left[1], HIGH);

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

void turn_left(){  // Declares Motor Turn Left Function Robot Will Turn Left
  digitalWrite(motor_left[0], HIGH);
  digitalWrite(motor_left[1], LOW);

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

void turn_right(){ // Declares Motor Turn Right Function Robot Will Turn Right
  digitalWrite(motor_left[0], LOW);
  digitalWrite(motor_left[1], HIGH);

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

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!

Arduino
via Egeo 16
Torino, 10131
Italy