Пример #1
0
//boolean S == Scale.  True == Farenheit; False == Celcius
float EchoRange::readDistance(bool S) {
  float f;

  //digitalWrite(2, HIGH);
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  float 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(m_trigPin, OUTPUT);// attach pin 3 to Trig
  digitalWrite(m_trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(m_trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(m_trigPin, 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 (m_echoPin, INPUT);//attach pin 4 to Echo
  duration = pulseIn(m_echoPin, HIGH);

  // convert the time into a distance
  //inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  return cm;
}
Пример #2
0
// 超音波センサからデータ取得.PingあるいはSeedo studioの超音波センサしか使えない.
unsigned int getPing(int pos)
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  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(PING_PIN[pos], OUTPUT);
  digitalWrite(PING_PIN[pos], LOW);
  delayMicroseconds(2);
  digitalWrite(PING_PIN[pos], HIGH);
  delayMicroseconds(5);
  digitalWrite(PING_PIN[pos], 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(PING_PIN[pos], INPUT);
  duration = pulseIn(PING_PIN[pos], HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);

  // error
  //if (cm > 400)  cm = 999;
  //if (cm <=   0) cm = 999;

  return cm;
}
Пример #3
0
long PING::getDistance(){
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  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:

  digitalWrite(pingTrig, LOW);
  delayMicroseconds(2);
  digitalWrite(pingTrig, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingTrig, 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(Echo, INPUT);
  duration = pulseIn(Echo, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  
  return cm;

}
Пример #4
0
long Ping::getDistance() {//inches
    long time = getDuration();

    if(units == INCHES) {
        time = microsecondsToInches(time);
    } else {
        time = microsecondsToCentimeters(time);
    }

    return time;

}
Пример #5
0
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(10);
	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(inPin, INPUT);
	duration = pulseIn(inPin, HIGH);

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

	Serial.println(cm, DEC);

	if (cm < 3) {
		// play the note corresponding to this sensor:
		tone(8, notes[4], 30);
	}

	if ((cm >= 3) && (cm <= 9)) {
		// play the note corresponding to this sensor:
		tone(8, notes[0], 30);
	}

	if ((cm >= 10) && (cm <= 19)) {
		// play the note corresponding to this sensor:
		tone(8, notes[1], 30);
	}
	if ((cm >= 20) && (cm <= 29)) {
		// play the note corresponding to this sensor:
		tone(8, notes[2], 30);
	}

	if ((cm >= 30) && (cm <= 49)) {
		// play the note corresponding to this sensor:
		tone(8, notes[3], 30);
	}

	delay(100);
}
long UltrasonicSensor::distanceInCentimeters () {
  long duration;
  // 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(_trigger, OUTPUT);
  digitalWrite(_trigger, LOW);
  delayMicroseconds(2);
  digitalWrite(_trigger, HIGH);
  delayMicroseconds(5);
  digitalWrite(_trigger, 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(_echo, INPUT);
  duration = pulseIn(_echo, HIGH);

  // convert the time into a distance
  return microsecondsToCentimeters(duration);
}
Пример #7
0
void loopUltra()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  if (intervalStarted) {
    // 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(sonarPin, OUTPUT);
    digitalWrite(sonarPin, LOW);
    delayMicroseconds(2);
    digitalWrite(sonarPin, HIGH);
    delayMicroseconds(15);
    digitalWrite(sonarPin, LOW);
    delayMicroseconds(20);
    // 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(sonarPin, INPUT);
    duration = pulseIn(sonarPin, HIGH);

    // convert the time into a distance
    cm = microsecondsToCentimeters(duration);

    // put data into message
    range_msg_sonar.radiation_type = sensor_msgs::Range::ULTRASOUND;
    range_msg_sonar.header.frame_id =  "/ultraSonic"; // frame
    range_msg_sonar.header.stamp = nh.now(); // time
    range_msg_sonar.field_of_view = 60.0 / 360 * 3.14; // [rad]
    range_msg_sonar.min_range = 0.03; // [m]
    range_msg_sonar.max_range = 4.0; // [m]
    range_msg_sonar.range = cm / 100.0; // [m]
    // publish
    pub_range.publish(&range_msg_sonar);
  }
}
Пример #8
0
long Sonar::getCmSampled(long samples) {
	return microsecondsToCentimeters(this->getRawSampled(samples));
}
Пример #9
0
long Sonar::getCm() {
	return microsecondsToCentimeters(this->getRaw());
}