Part 3. Ultrasonic Ranger

Rachel edited this page Aug 21, 2015 · 4 revisions

yuntrashranger

We already installed the ultrasonic ranger library. To test the ultrasonic ranger we are going to run a script that prints its measurements to the Arduino IDE's Serial Monitor. Because we can communicate with the Yun over USB or WiFi, there is a different test script for each.

IF USING WIFI CONNECTION

You can copy the sketch here:

https://github.com/InitialState/arduinotrashsensor/blob/master/ultrasonictest_console.ino

This sketch uses Console.print to communicate through the Yun's Bridge.

/*
 * UltrasonicDisplayOnTerm.ino
 * Example sketch for ultrasonic ranger
 *
 * Copyright (c) 2012 seeed technology inc.
 * Website    : www.seeed.cc
 * Author     : LG, FrankieChu
 * Create Time: Jan 17,2013
 * Change Log :

/***************************************************************************/
//	Function: Measure the distance to obstacles in front and print the distance
//			  value to the serial terminal.The measured distance is from
//			  the range 0 to 400cm(157 inches).
//	Hardware: Grove - Ultrasonic Ranger
//	Arduino IDE: Arduino-1.0
/*****************************************************************************/
#include <Console.h>
#include "Ultrasonic.h"

Ultrasonic ultrasonic(7);

void setup()
{
	Bridge.begin();
	Console.begin();

	// Wait for Console port to connect
    while (!Console); 
}
void loop()
{
	long RangeInInches;
	long RangeInCentimeters;
	RangeInInches = ultrasonic.MeasureInInches();
	RangeInCentimeters = ultrasonic.MeasureInCentimeters();
	Console.println("The distance to obstacles in front is: ");
	Console.print(RangeInInches);//0~157 inches
	Console.println(" inch");
	Console.print(RangeInCentimeters);//0~400cm
	Console.println(" cm");
	delay(100);
}

Just open this sketch and upload it to your Yun. When uploading over a WiFi connection, the IDE will prompt you for the Yun's password (which is "arduino" unless you changed it on the Configuration page). Click on the Serial Monitor (magnifying glass icon) once it's finished uploading to see the distance in inches and centimeters to the nearest obstacle.

ultrasonicdatascreenie

IF USING USB SERIAL CONNECTION

The ultrasonic ranger library contains a test script called UltrasonicDisplayOnTerm that you can find under File -> Examples -> Grove_Ultrasonic_Ranger-master in the IDE. Or you can copy it from here:

https://github.com/InitialState/arduinotrashsensor/blob/master/ultrasonictest.ino

This sketch uses Serial.print to print using the serial connection established through the USB connector.

/*
 * UltrasonicDisplayOnTerm.ino
 * Example sketch for ultrasonic ranger
 *
 * Copyright (c) 2012 seeed technology inc.
 * Website    : www.seeed.cc
 * Author     : LG, FrankieChu
 * Create Time: Jan 17,2013
 * Change Log :
 *
 * The MIT License (MIT)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */


/***************************************************************************/
//	Function: Measure the distance to obstacles in front and print the distance
//			  value to the serial terminal.The measured distance is from
//			  the range 0 to 400cm(157 inches).
//	Hardware: Grove - Ultrasonic Ranger
//	Arduino IDE: Arduino-1.0
/*****************************************************************************/

#include "Ultrasonic.h"

Ultrasonic ultrasonic(7);
void setup()
{
	Serial.begin(9600);
}
void loop()
{
	long RangeInInches;
	long RangeInCentimeters;
	RangeInInches = ultrasonic.MeasureInInches();
	RangeInCentimeters = ultrasonic.MeasureInCentimeters();
	Serial.println("The distance to obstacles in front is: ");
	Serial.print(RangeInInches);//0~157 inches
	Serial.println(" inch");
	Serial.print(RangeInCentimeters);//0~400cm
	Serial.println(" cm");
	delay(100);
}

Just open this sketch and upload it to your Yun. Click on the Serial Monitor (magnifying glass icon) once it's finished uploading to see the distance in inches and centimeters to the nearest obstacle.

ultrasonicdatascreenie

Try moving your hand closer to and farther away from the sensor to see if it is working. Then we can move on!

<< Part 3: Making the Connections - Part 4: Putting it All Together >>

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.