Sie sind auf Seite 1von 8

Features Pricing Find a repository…   English Sign up Log in

Tim Eckel /  Arduino New Ping
Arduino New Ping

Wiki   Clone wiki
ACTIONS

Clone Arduino New Ping / Home
 Compare
View History

Fork
NewPing Arduino Library for Arduino
NAVIGATION

Overview
Index
Source
NewPing Arduino Library for Arduino
Commits
Index
Branches Introduction
Download & Install
Pull requests Show Your Appreciation
Connection Example
Issues Syntax
History
Wiki
Support
Downloads Support Forum
Issue Tracking
Timer 2 Conflict
Multiple Definition of "_vector_7" Error
Help with 15 Sensors Sketch
Examples
Simple NewPing Sketch
Single Pin Sketch
Ping 3 Sensors Sketch
Event Timer Sketch
Timer Median Sketch
15 Sensors Sketch
Cool User Projects Using NewPing
My Other Arduino Libraries

Introduction
When I first received an ultrasonic sensor I was not happy with how poorly it performed. I soon realized
the problem wasn't the sensor, it was the available ping and ultrasonic libraries causing the problem.
The NewPing library totally fixes these problems, adds many new features, and breathes new life into
these very affordable distance sensors. Here's a list of some of the features of NewPing:

Works with many different ultrasonic sensor models: SR04, SRF05, SRF06, DYP­ME007, JSN­
SR04T & Parallax PING)))™.
Option to interface with all but the SRF06 sensor using only one Arduino pin.
Doesn't lag for a full second if no ping echo is received like all other ultrasonic libraries.
Compatible with the entire Arduino line­up (and clones), Teensy family (including $19.80 96Mhz
32 bit Teensy 3.2) and non­AVR microcontrollers.
Ping sensors consistently and reliably at up to 30 times per second.
Timer interrupt method for event­driven sketches.
Built­in digital filter method ping_median() for easy error correction.
Uses port registers when accessing pins for faster execution and smaller code size.
Allows setting of a maximum distance where pings beyond that distance are read as no ping or
clear.
Ease of using multiple sensors (sketch that pings 3 sensors ­ sketch that pings 15 sensors using
timers).
More accurate distance calculation (cm, inches & microseconds).
Doesn't use pulseIn, which is slow and gives incorrect results with some ultrasonic sensor
models.
Actively developed with features being added and bugs/issues addressed.

Download & Install
Download NewPing v1.8

Save the .zip file to your desktop, then use the Importing a .zip Library instructions to import the library
into the Arduino IDE.
(113,121 downloads on Google Code before being closed)

If you wish to fork this library, please create a private repository as to not confuse others trying to
download the latest official version.

Show Your Appreciation
Help future development by making a small donation (the teckel@bex.net payee is correct).

Supporters (latest: August 23rd, 2016):

Alan M. $30 Alastair M. $20 Davide L. $2 Mike M. $30

David M. $4 Antonio B. $1 Carsten W. $3 Jose N. $10

Martin M. $5 Fred A. $5 Alan H. $3 Orlando S. $10

Ed N. $10 Daniel H. $1 Bogdan M. $5 Soh K. $10

Pentagono Estudio $1 Stephen C. $5 Daniel D. $3 Adam G. $10

Cha­Rie T. $10 Stig M. $5 Claudio Z. $5 The Forge Initiative $10

Weyman S. $5 Bob R. $5 Zdenek K. $10 Duane G. $10

Ronald B. (Granpa) $20 EM Workbench $25 Ralph H. $25 Ron T. $5

Greg S. $5 William M. $10 Douglas M. $5 Nikita V. $3

Matthew B. $10 Olexiy L. $10 Matt K. $3 Charles D. $10

Frédérik B. $5 YourDuino.com $10 Silva M. $10 David B. $10

Lukas M. $2 Jonas H. $2.50 Graeme M. $5 Alessandro Z. $20

Maximilian S. $10 Wally H. $5 Debottam B. $2 Tim H. $20

Paul M. $5 James W. $10 Isaac R. $2 Erica S. $10

Connection Example

Syntax
NewPing sonar(trigger_pin, echo_pin [, max_cm_distance]) ­ Initialize an ultrasonic device,
trigger pin, echo pin, and optional maximum distance you wish to sensor to measure (default =
500cm).
sonar.ping([max_cm_distance]) ­ Send a ping and get the echo time (in microseconds) as a
result. [max_cm_distance] allows you to optionally set a new max distance.
sonar.ping_in([max_cm_distance]) ­ Send a ping and get the distance in whole inches.
[max_cm_distance] allows you to optionally set a new max distance.
sonar.ping_cm([max_cm_distance]) ­ Send a ping and get the distance in whole centimeters.
[max_cm_distance] allows you to optionally set a new max distance.
sonar.ping_median(iterations [, max_cm_distance]) ­ Do multiple pings (default=5), discard
out of range pings and return median in microseconds. [max_cm_distance] allows you to
optionally set a new max distance.
sonar.convert_in(echoTime) ­ Convert echoTime from microseconds to inches.
sonar.convert_cm(echoTime) ­ Convert echoTime from microseconds to centimeters.
sonar.ping_timer(function [, max_cm_distance]) ­ Send a ping and call function to test if ping
is complete. [max_cm_distance] allows you to optionally set a new max distance.
sonar.check_timer() ­ Check if ping has returned within the set distance limit.
NewPing::timer_us(frequency, function) ­ Call function every frequency microseconds.
NewPing::timer_ms(frequency, function) ­ Call function every frequency milliseconds.
NewPing::timer_stop() ­ Stop the timer.

History
v1.8 ­ Released 07/30/2016 ­ Added support for non­AVR microcontrollers. For non­AVR
microcontrollers, advanced ping_timer() timer methods are disabled due to inconsistencies or no support
at all between platforms. However, standard ping methods are all supported. Added new optional
variable to ping(), ping_in(), ping_cm(), ping_median(), and ping_timer() methods which allows you to
set a new maximum distance for each ping. Added support for the ATmega16, ATmega32 and
ATmega8535 microcontrollers. Changed convert_cm() and convert_in() methods to static members. You
can now call them without an object. For example: cm = NewPing::convert_cm(distance);

v1.7 ­ Released 09/29/2015 ­ Removed support for the Arduino Due and Zero because they're both 3.3
volt boards and are not 5 volt tolerant while the HC­SR04 is a 5 volt sensor. Also, the Due and Zero don't
support pin manipulation compatibility via port registers which can be done (see the Teensy 3.2).

v1.6 ­ Released 06/17/2014 ­ Corrected delay between pings when using ping_median() method.
Added support for the URM37 sensor (must change URM37_ENABLED from false to true). Added
support for Arduino microcontrollers like the $20 32 bit ARM Cortex­M4 based Teensy 3.2. Added
automatic support for the Atmel ATtiny family of microcontrollers. Added timer support for the ATmega8
microcontroller. Rounding disabled by default, reduces compiled code size (can be turned on with
ROUNDING_ENABLED switch). Added TIMER_ENABLED switch to get around compile­time "_vector_7"
errors when using the Tone library, or you can use the toneAC, NewTone or TimerFreeTone libraries.
Other speed and compiled size optimizations.

v1.5 ­ Released 8/15/2012 ­ Added ping_median() method which does a user specified number of pings
(default=5) and returns the median ping in microseconds (out of range pings ignored). This is a very
effective digital filter. Optimized for smaller compiled size (even smaller than sketches that don't use a
library).

v1.4 ­ Released 7/14/2012 ­ You can now interface with all but the SRF06 sensor using only one
Arduino pin. Added support for the Parallax PING)))™ sensor. You can also interface with the SRF06
using one pin if you install a 0.1uf capacitor on the trigger and echo pins of the sensor then tie the trigger
pin to the Arduino pin (doesn't work with Teensy). To use the same Arduino pin for trigger and echo,
specify the same pin for both values. Various bug fixes.

v1.3 ­ Released 6/8/2012 ­ Big feature addition, event­driven ping! Uses Timer2 interrupt, so be mindful
of PWM or timing conflicts messing with Timer2 may cause (namely PWM on pins 3 & 11 on Arduino,
PWM on pins 9 and 10 on Mega, and Tone library). Simple to use timer interrupt functions you can use
in your sketches totaly unrelated to ultrasonic sensors (don't use if you're also using NewPing?'s
ping_timer because both use Timer2 interrupts). Loop counting ping method deleted in favor of timing
ping method after inconsistant results kept surfacing with the loop timing ping method. Conversion to cm
and inches now rounds to the nearest cm or inch. Code optimized to save program space and fixed a
couple minor bugs here and there. Many new comments added as well as line spacing to group code
sections for better source readability.

NOTE: For Teensy/Leonardo (ATmega32U4) the library uses Timer4 instead of Timer2. Also, only
16Mhz microcontrollers are supported with the timer methods, which means the ATmega8 and
ATmega128 will not work with the timer methods. However, the standard ping method should work just
fine on 8Mhz microcontrollers.

v1.2 ­ Released 5/24/2012 ­ Lots of code clean­up thanks to Adruino Forum members. Rebuilt the ping
timing code from scratch, ditched the pulseIn code as it doesn't give correct results (at least with ping
sensors). The NewPing? library is now VERY accurate and the code was simplified as a bonus. Smaller
and faster code as well. Fixed some issues with very close ping results when converting to inches. All
functions now return 0 only when there's no ping echo (out of range) and a positive value for a
successful ping. This can effectively be used to detect if something is out of range or in­range and at
what distance. Now compatible with Arduino 0023.

v1.1 ­ Released 5/16/2012 ­ Changed all I/O functions to use low­level port registers for ultra­fast and
lean code (saves from 174 to 394 bytes). Tested on both the Arduino Uno and Teensy 2.0 but should
work on all Arduino­based platforms because it calls standard functions to retrieve port registers and bit
masks. Also made a couple minor fixes to defines.

v1.0 ­ Released 5/15/2012 ­ Initial release.

Support

Support Forum
NewPing support forum

Issue Tracking
NewPing issue tracking ­ I guess it's worth trying out how Bitbucket's issue tracking works. Create an
issue in the Issues section and we'll see how it goes.

Timer 2 Conflict
Having a conflict with the tone library or another library using timer 2? Instead of the tone library, use my
NewTone or toneAC libraries which instead uses timer 1 (and also has many other advantages). Or use
my Timer Free Tone library which doesn't use any timers to generate tones.

Multiple Definition of "_vector_7" Error
Wiki help page on how to resolve the multiple definition of "_vector_7" error.

Help with 15 Sensors Sketch
Trying to use the 15 sensor sketch and need some help? There's a Wiki help page with loads of
information.

Examples

Simple NewPing Sketch
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// Example NewPing library sketch that does a ping about 20 times per second.
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rat

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest de
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance
  Serial.println("cm");
}

Single Pin Sketch
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// NewPing library sketch that interfaces with all but the SRF06 sensor using
// only one Arduino pin. You can also interface with the SRF06 using one pin
// if you install a 0.1uf capacitor on the trigger and echo pins of the sensor
// then tie the trigger pin to the Arduino pin (doesn't work with Teensy).
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

#include <NewPing.h>

#define PING_PIN  12  // Arduino pin tied to both trigger and echo pins on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rat

NewPing sonar(PING_PIN, PING_PIN, MAX_DISTANCE); // NewPing setup of pin and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest de
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance
  Serial.println("cm");
}
Ping 3 Sensors Sketch
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// Example NewPing library sketch that pings 3 sensors 20 times a second.
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

#include <NewPing.h>

#define SONAR_NUM 3      // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(4, 5, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. 
  NewPing(6, 7, MAX_DISTANCE), 
  NewPing(8, 9, MAX_DISTANCE)
};

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() { 
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    Serial.print(i);
    Serial.print("=");
    Serial.print(sonar[i].ping_cm());
    Serial.print("cm ");
  }
  Serial.println();
}

Event Timer Sketch
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// This example shows how to use NewPing's ping_timer method which uses the Timer2 interrupt to get the
// ping time. The advantage of using this method over the standard ping method is that it permits a more
// event‐driven sketch which allows you to appear to do two things at once. An example would be to ping
// an ultrasonic sensor for a possible collision while at the same time navigating. This allows a
// properly developed sketch to multitask. Be aware that because the ping_timer method uses Timer2,
// other features or libraries that also use Timer2 would be effected. For example, the PWM function on
// pins 3 & 11 on Arduino Uno (pins 9 and 11 on Arduino Mega) and the Tone library. Note, only the PWM
// functionality of the pins is lost (as they use Timer2 to do PWM), the pins are still available to use.
// NOTE: For Teensy/Leonardo (ATmega32U4) the library uses Timer4 instead of Timer2.
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on ping sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on ping sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rat

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

unsigned int pingSpeed = 50; // How frequently are we going to send out a ping (in milliseconds). 50ms would be 2
unsigned long pingTimer;     // Holds the next ping time.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pingTimer = millis(); // Start now.
}

void loop() {
  // Notice how there's no delays in this sketch to allow you to do other processing in‐line while doing distance
  if (millis() >= pingTimer) {   // pingSpeed milliseconds since last ping, do another ping.
    pingTimer += pingSpeed;      // Set the next ping time.
    sonar.ping_timer(echoCheck); // Send out the ping, calls "echoCheck" function every 24uS where you can check 
  }
  // Do other stuff here, really. Think of it as multi‐tasking.
}

void echoCheck() { // Timer2 interrupt calls this function every 24uS where you can check the ping status.
  // Don't do anything here!
  if (sonar.check_timer()) { // This is how you check to see if the ping was received.
    // Here's where you can add code.
    Serial.print("Ping: ");
    Serial.print(sonar.ping_result / US_ROUNDTRIP_CM); // Ping returned, uS result in ping_result, convert to cm 
    Serial.println("cm");
  }
  // Don't do anything here!
}

Timer Median Sketch
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// Calculate a ping median using the ping_timer() method.
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

#include <NewPing.h>

#define ITERATIONS     5 // Number of iterations.
#define TRIGGER_PIN   12 // Arduino pin tied to trigger pin on ping sensor.
#define ECHO_PIN      11 // Arduino pin tied to echo pin on ping sensor.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross‐sensor echo).

unsigned long pingTimer[ITERATIONS]; // Holds the times when the next ping should happen for each iteration.
unsigned int cm[ITERATIONS];         // Where the ping distances are stored.
uint8_t currentIteration = 0;        // Keeps track of iteration step.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200);
  pingTimer[0] = millis() + 75;            // First ping starts at 75ms, gives time for the Arduino to chill befo
  for (uint8_t i = 1; i < ITERATIONS; i++) // Set the starting time for each iteration.
    pingTimer[i] = pingTimer[i ‐ 1] + PING_INTERVAL;
}

void loop() {
  for (uint8_t i = 0; i < ITERATIONS; i++) { // Loop through all the iterations.
    if (millis() >= pingTimer[i]) {          // Is it this iteration's time to ping?
      pingTimer[i] += PING_INTERVAL * ITERATIONS; // Set next time this sensor will be pinged.
      if (i == 0 && currentIteration == ITERATIONS ‐ 1) oneSensorCycle(); // Sensor ping cycle complete, do somet
      sonar.timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance)
      currentIteration = i;        // Sensor being accessed.
      cm[currentIteration] = 0;    // Make distance zero in case there's no ping echo for this iteration.
      sonar.ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look fo
    }
  }
  // Other code that *DOESN'T* analyze ping results can go here.
}

void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar.check_timer())
    cm[currentIteration] = sonar.ping_result / US_ROUNDTRIP_CM;
}

void oneSensorCycle() { // All iterations complete, calculate the median.
  unsigned int uS[ITERATIONS];
  uint8_t j, it = ITERATIONS;
  uS[0] = NO_ECHO;
  for (uint8_t i = 0; i < it; i++) { // Loop through iteration results.
    if (cm[i] != NO_ECHO) { // Ping in range, include as part of median.
      if (i > 0) {          // Don't start sort till second ping.
        for (j = i; j > 0 && uS[j ‐ 1] < cm[i]; j‐‐) // Insertion sort loop.
          uS[j] = uS[j ‐ 1];                         // Shift ping array to correct position for sort insertion.
      } else j = 0;         // First ping is sort starting point.
      uS[j] = cm[i];        // Add last ping to array in sorted position.
    } else it‐‐;            // Ping out of range, skip and don't include as part of median.
  }
  Serial.print(uS[it >> 1]);
  Serial.println("cm");
}

15 Sensors Sketch
NOTICE! ­ The following sketch uses a non­blocking time­driven programming paradigm instead of the
typical (for Arduino) blocking mode programming paradigm. This can be VERY confusing to new
programmers because the code doesn't seem to follow a normal step by step process but instead jumps
around based upon timer events. If you simply want to ping multiple sensors, use the sketch that pings
3 sensors example as a starting point as that sketch uses the more Arduino typical blocking mode.
This sketch is designed for seasoned programming experts, don't use unless you really know
what you're doing!*

// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// This example code was used to successfully communicate with 15 ultrasonic sensors. You can adjust
// the number of sensors in your project by changing SONAR_NUM and the number of NewPing objects in the
// "sonar" array. You also need to change the pins for each sensor for the NewPing objects. Each sensor
// is pinged at 33ms intervals. So, one cycle of all sensors takes 495ms (33 * 15 = 495ms). The results
// are sent to the "oneSensorCycle" function which currently just displays the distance data. Your project
// would normally process the sensor results in this function (for example, decide if a robot needs to
// turn and call the turn function). Keep in mind this example is event‐driven. Your complete sketch needs
// to be written so there's no "delay" commands and the loop() cycles at faster than a 33ms rate. If other
// processes take longer than 33ms, you'll need to increase PING_INTERVAL so it doesn't get behind.
// ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
#include <NewPing.h>

#define SONAR_NUM     15 // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross‐sensor echo).

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
  NewPing(41, 42, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(43, 44, MAX_DISTANCE),
  NewPing(45, 20, MAX_DISTANCE),
  NewPing(21, 22, MAX_DISTANCE),
  NewPing(23, 24, MAX_DISTANCE),
  NewPing(25, 26, MAX_DISTANCE),
  NewPing(27, 28, MAX_DISTANCE),
  NewPing(29, 30, MAX_DISTANCE),
  NewPing(31, 32, MAX_DISTANCE),
  NewPing(34, 33, MAX_DISTANCE),
  NewPing(35, 36, MAX_DISTANCE),
  NewPing(37, 38, MAX_DISTANCE),
  NewPing(39, 40, MAX_DISTANCE),
  NewPing(50, 51, MAX_DISTANCE),
  NewPing(52, 53, MAX_DISTANCE)
};

void setup() {
  Serial.begin(115200);
  pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill befor
  for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
    pingTimer[i] = pingTimer[i ‐ 1] + PING_INTERVAL;
}

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
    if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      if (i == 0 && currentSensor == SONAR_NUM ‐ 1) oneSensorCycle(); // Sensor ping cycle complete, do something
      sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new p
      currentSensor = i;                          // Sensor being accessed.
      cm[currentSensor] = 0;                      // Make distance zero in case there's no ping echo for this sen
      sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoC
    }
  }
  // Other code that *DOESN'T* analyze ping results can go here.
}

void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
  // The following code would be replaced with your code that does something with the ping results.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    Serial.print(i);
    Serial.print("=");
    Serial.print(cm[i]);
    Serial.print("cm ");
  }
  Serial.println();
}

Cool User Projects Using NewPing
My Other Arduino Libraries
NewPing Works with many ultrasonic sensors, can communicate using only one pin, very low lag, fast
(up to 30 pings per second), timer interrupt method for event­driven sketches, light code, and much
more.

LCDBitmap Arduino library that allows you to create a tiny 20x16 pixel bitmap (raster) display on a
normally character­only Hitachi HD44780 based LCD display. Typical drawing functions like line,
rectangle, invert, etc. Control is right down to the pixel level.

toneAC Replacement to the standard tone library with the advantage of nearly twice the volume, higher
quality, can produce higher frequencies, 1.5k smaller compiled code, and less stress on the speaker.

toneAC2 Replacement to the standard tone library with the advantage of nearly twice the volume, 800
bytes smaller compiled code size, and less stress on the speaker.

NewTone About 1,200 bytes smaller code size than the standard tone library, faster execution time,
exclusive use of port registers for fastest and smallest code, higher quality sound output than tone
library.

TimerFreeTone Replacement to the standard tone library but without using timers. Also over 1.5k
smaller compiled code, exclusive use of port registers, and compatible with ATmega, ATtiny, and ARM­
based microcontrollers.

Updated 20 hours ago

Blog  · Support  · Plans & pricing  · Documentation  · API  · Site status  · Version info  · Terms of service  ·


Privacy policy

JIRA Software  · Confluence  · Bamboo  · SourceTree  · HipChat

Das könnte Ihnen auch gefallen