Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Particle Photon/Core/P1/Electron Ping PulseIn Example (HC-SR04) with Sound!
/*
******************************************************************************
* Copyright (c) 2015 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
******************************************************************************
*/
/* HC-SR04 Ping / Range finder wiring:
* -----------------------------------
* Particle - HC-SR04
* GND - GND
* VIN - VCC
* D2 - TRIG
* D6 - ECHO
*
* D0 - SPEAKER to GND
*/
#include "application.h"
// The Photon has 9 Tone pins: D0, D1, D2, D3, A4, A5, A7, RX and TX.
#define speakerPin D0
// Notes defined in microseconds (Period/2)
// from note C to B, Octaves 3 through 7
int notes[] =
{0,
/* C, C#, D, D#, E, F, F#, G, G#, A, A#, B */
3817,3597,3401,3205,3030,2857,2703,2551,2404,2273,2146,2024, // 3 (1-12)
1908,1805,1701,1608,1515,1433,1351,1276,1205,1136,1073,1012, // 4 (13-24)
956, 903, 852, 804, 759, 716, 676, 638, 602, 568, 536, 506, // 5 (25-37)
478, 451, 426, 402, 379, 358, 338, 319, 301, 284, 268, 253, // 6 (38-50)
239, 226, 213, 201, 190, 179, 169, 159, 151, 142, 134, 127 }; // 7 (51-62)
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
// Trigger pin, Echo pin, delay (ms), visual=true|info=false
ping(D2, D6, 20, true);
}
void ping(pin_t trig_pin, pin_t echo_pin, uint32_t wait, bool info)
{
uint32_t duration, inches;
static bool init = false;
if (!init) {
pinMode(trig_pin, OUTPUT);
digitalWriteFast(trig_pin, LOW);
pinMode(echo_pin, INPUT);
delay(50);
init = true;
}
/* Trigger the sensor by sending a HIGH pulse of 10 or more microseconds */
digitalWriteFast(trig_pin, HIGH);
delayMicroseconds(10);
digitalWriteFast(trig_pin, LOW);
duration = pulseIn(echo_pin, HIGH);
/* Convert the time into a distance */
// Sound travels at 1130 ft/s (73.746 us/inch)
// or 340 m/s (29 us/cm), out and back so divide by 2
// Ref: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
inches = duration / 74 / 2;
int noteDuration = (15000/(inches*20))+1;
if(inches < 30) tone(speakerPin, (notes[inches%60]!=0)?(500000/notes[inches%60]):0,noteDuration);
else noTone(speakerPin);
// blocking delay needed because tone() does not block
delay(noteDuration);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.