Lesson 70: Using an Ultrasonic Distance Sensor HC-SR04 with Arduino
In this tutorial we learn how to use HC-SR04 Ultrasonic with Arduino to measure distance of up to 3 meters.
455-Lesson 70: Using an Ultrasonic Distance Sensor with Arduino
语言: C++
/*
* S08-05 Sharp IR
* Lesson 70: Using Sharp Infrared Distance Sensor with Arduino
* This Arduino sketch to measure distance using Sharp GP2Y0A51SK0F or any other
* Written by Ahmad Shamshiri for Robojax.com
* on Jan 15, 2019 at 19:15 in Ajax, Ontario, Canada
* Watch full video instruction: https://youtu.be/CpCLa60WNXY
This video is part of Arduino Step by Step Course which starts here: https://youtu.be/-6qSrDUA5a8
If you found this tutorial helpful, please support me so I can continue creating
content like this.
or make a donation using PayPal http://robojax.com/L/?id=64
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <SharpIR.h>
#define IR A0 // define signal pin
#define model 1080 // used 1080 because model GP2Y0A21YK0F is used
// Sharp IR code for Robojax.com
// ir: the pin where your sensor is attached
// model: an int that determines your sensor: 1080 for GP2Y0A21YK0F
// 20150 for GP2Y0A02YK0F
// 430 for GP2Y0A41SK0F
/*
2 to 15 cm GP2Y0A51SK0F use 215
4 to 30 cm GP2Y0A41SK0F / GP2Y0AF30 series use 430
10 to 80 cm GP2Y0A21YK0F use 1080
10 to 150 cm GP2Y0A60SZLF use 10150
20 to 150 cm GP2Y0A02YK0F use 20150
100 to 550 cm GP2Y0A710K0F use 100550
*/
SharpIR SharpIR(IR, model);
void setup() {
// Sharp IR code for Robojax.com
Serial.begin(9600);
Serial.println("Robojax Sharp IR ");
}
void loop() {
// Sharp IR code for Robojax.com
delay(500);
unsigned long startTime=millis(); // takes the time before the loop on the library begins
int dis=SharpIR.distance(); // this returns the distance to the object you're measuring
// Sharp IR code for Robojax.com
Serial.print("Mean distance: "); // returns it to the serial monitor
Serial.println(dis);
unsigned long endTime=millis()-startTime; // the following gives you the time taken to get the measurement
Serial.print("Time taken (ms): ");
Serial.println(endTime);
// Sharp IR code for Robojax.com
Serial.println();
}
资源与参考
尚无可用资源。
文件📁
没有可用的文件。