Components and supplies
Buzzer
Ultrasonic Sensor - HC-SR04 (Generic)
Arduino Mega 2560
Apps and platforms
TinkerCAD - Free!
Project description
Code
SR04.h
c_cpp
1#ifndef SR04_H 2#define SR04_H 3 4#if defined(ARDUINO) && ARDUINO >= 100 5 #include "Arduino.h" 6#else 7 #include "WProgram.h" 8#endif 9//#include "pins_arduino.h" 10 11#include <inttypes.h> 12 13#define PULSE_TIMEOUT 150000L // 100ms 14#define DEFAULT_DELAY 10 15#define DEFAULT_PINGS 5 16class SR04 { 17public: 18 19 /** 20 * Constructor 21 * Ultrasonic sensor SR04, four connections pins 22 * VCC, ECHO, TRIGGER, GND 23 * <br> 24 * \\param echoPin digital INPUT-Pin for measuring distance 25 * \\param triggerPin if 10us high a trigger signal is generated from 26 * SR04 27 * 28 * \ eturn void 29 */ 30 SR04(int echoPin, int triggerPin); 31 32 /** 33 * Do a measurment for this sensor. Return distance as long 34 * in centimenter 35 * \ eturn long distance in centimeter 36 */ 37 long Distance(); 38 39 /** 40 * Do count measurents and calculate the average. 41 * To avoid defilement from ow/high peaks, min/max values 42 * are substracted from the average 43 * 44 * \\param wait delay between measurements, default = DEFAULT_DELAY/ms 45 * \\param count number of measurements, default DEFAULT_PINGS 46 * \ eturn long distance in centimeter 47 **/ 48 long DistanceAvg(int wait=DEFAULT_DELAY, int count=DEFAULT_PINGS); 49 50 /** 51 * Do only a ping. Get result with methode getDistance() 52 * 53 * \\param keine 54 */ 55 void Ping() ; 56 57 /** 58 * return latest distance. Methode Ping() should be called before 59 * \\param keine 60 * \ eturn Distanz in Zentimeter 61 */ 62 long getDistance(); 63 64 65private: 66 /** 67 * Do the measurement calculation and return result in centimeter 68 * SR04 measure echo time to obstacle and return way. 69 * <br> 70 * Sound travels with 340m/sec 71 * <br> 72 * Example: Obstace 100cm away from SR04. Measure time is 100cm to 73 * obstacle and 100cm return = 200cm 74 * <br> 75 * 1sec = 1000ms = 1.000.000uS 76 * 1.000.000 / 340 = Distance in microseconds for 100cm 77 * 2941uS fuer 100cm = 5882 uS fuer 200cm 78 * 79 * duration / 5882 * 100 = distance in cm 80 */ 81 long MicrosecondsToCentimeter(long duration); 82 83 long _currentDistance; 84 int _echoPin, _triggerPin; 85 long _duration, _distance; 86 bool _autoMode; 87}; 88#endif 89 90 91 92
theremin.ino
arduino
Here's the code. You're going to need the library, as well. Paste it in your global "libraries" folder (C:\ProgramFiles (x86)\Arduino\libraries or something).
1#include <SR04.h> //you'll need this library 2 3#define TRIG_PIN 11 //here we use pins 11+12, you can use whichever digital PWM you want 4#define ECHO_PIN 12 5 6SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN); //initialize our ultrasonic sensor 7long dist; //our distance variable 8int buzzer = 8; //buzzer pin 9 10void setup() { 11 Serial.begin(9600); //debugging purposes 12 pinMode(buzzer, OUTPUT); 13} 14 15void loop() { 16 dist = sr04.Distance(); //get distance 17 Serial.println(dist); //print to serial for debugging 18 //delay(100); you'll need this if you use the serial monitor so you don't get overwhelmed 19 if (not (dist > 1000)) { //so we don't get annoying high-pitch squeacks if we get a bad reading 20 tone(buzzer, dist + 200, 1000); //add 200 so it's not so low 21 } 22} 23
SR04.cpp
c_cpp
1#include "SR04.h" 2 3SR04::SR04(int echoPin, int triggerPin) { 4 _echoPin = echoPin; 5 _triggerPin = triggerPin; 6 pinMode(_echoPin, INPUT); 7 pinMode(_triggerPin, OUTPUT); 8 _autoMode = false; 9 _distance = 999; 10} 11 12 13long SR04::Distance() { 14 long d = 0; 15 _duration = 0; 16 digitalWrite(_triggerPin, LOW); 17 delayMicroseconds(2); 18 digitalWrite(_triggerPin, HIGH); 19 delayMicroseconds(10); 20 digitalWrite(_triggerPin, LOW); 21 delayMicroseconds(2); 22 _duration = pulseIn(_echoPin, HIGH, PULSE_TIMEOUT); 23 d = MicrosecondsToCentimeter(_duration); 24 delay(25); 25 return d; 26} 27 28long SR04::DistanceAvg(int wait, int count) { 29 long min, max, avg, d; 30 min = 999; 31 max = 0; 32 avg = d = 0; 33 34 if (wait < 25) { 35 wait = 25; 36 } 37 38 if (count < 1) { 39 count = 1; 40 } 41 42 for (int x = 0; x < count + 2; x++) { 43 d = Distance(); 44 45 if (d < min) { 46 min = d; 47 } 48 49 if (d > max) { 50 max = d; 51 } 52 53 avg += d; 54 } 55 56 // substract highest and lowest value 57 avg -= (max + min); 58 // calculate average 59 avg /= count; 60 return avg; 61} 62 63void SR04::Ping() { 64 _distance = Distance(); 65} 66 67long SR04::getDistance() { 68 return _distance; 69} 70 71long SR04::MicrosecondsToCentimeter(long duration) { 72 long d = (duration * 100) / 5882; 73 //d = (d == 0)?999:d; 74 return d; 75} 76 77 78 79 80
SR04.cpp
c_cpp
1#include "SR04.h" 2 3SR04::SR04(int echoPin, int triggerPin) { 4 _echoPin = echoPin; 5 _triggerPin = triggerPin; 6 pinMode(_echoPin, INPUT); 7 pinMode(_triggerPin, OUTPUT); 8 _autoMode = false; 9 _distance = 999; 10} 11 12 13long SR04::Distance() { 14 long d = 0; 15 _duration = 0; 16 digitalWrite(_triggerPin, LOW); 17 delayMicroseconds(2); 18 digitalWrite(_triggerPin, HIGH); 19 delayMicroseconds(10); 20 digitalWrite(_triggerPin, LOW); 21 delayMicroseconds(2); 22 _duration = pulseIn(_echoPin, HIGH, PULSE_TIMEOUT); 23 d = MicrosecondsToCentimeter(_duration); 24 delay(25); 25 return d; 26} 27 28long SR04::DistanceAvg(int wait, int count) { 29 long min, max, avg, d; 30 min = 999; 31 max = 0; 32 avg = d = 0; 33 34 if (wait < 25) { 35 wait = 25; 36 } 37 38 if (count < 1) { 39 count = 1; 40 } 41 42 for (int x = 0; x < count + 2; x++) { 43 d = Distance(); 44 45 if (d < min) { 46 min = d; 47 } 48 49 if (d > max) { 50 max = d; 51 } 52 53 avg += d; 54 } 55 56 // substract highest and lowest value 57 avg -= (max + min); 58 // calculate average 59 avg /= count; 60 return avg; 61} 62 63void SR04::Ping() { 64 _distance = Distance(); 65} 66 67long SR04::getDistance() { 68 return _distance; 69} 70 71long SR04::MicrosecondsToCentimeter(long duration) { 72 long d = (duration * 100) / 5882; 73 //d = (d == 0)?999:d; 74 return d; 75} 76 77 78 79 80
SR04.h
c_cpp
1#ifndef SR04_H 2#define SR04_H 3 4#if defined(ARDUINO) && ARDUINO >= 100 5 #include "Arduino.h" 6#else 7 #include "WProgram.h" 8#endif 9//#include "pins_arduino.h" 10 11#include <inttypes.h> 12 13#define PULSE_TIMEOUT 150000L // 100ms 14#define DEFAULT_DELAY 10 15#define DEFAULT_PINGS 5 16class SR04 { 17public: 18 19 /** 20 * Constructor 21 * Ultrasonic sensor SR04, four connections pins 22 * VCC, ECHO, TRIGGER, GND 23 * <br> 24 * \\param echoPin digital INPUT-Pin for measuring distance 25 * \\param triggerPin if 10us high a trigger signal is generated from 26 * SR04 27 * 28 * \ eturn void 29 */ 30 SR04(int echoPin, int triggerPin); 31 32 /** 33 * Do a measurment for this sensor. Return distance as long 34 * in centimenter 35 * \ eturn long distance in centimeter 36 */ 37 long Distance(); 38 39 /** 40 * Do count measurents and calculate the average. 41 * To avoid defilement from ow/high peaks, min/max values 42 * are substracted from the average 43 * 44 * \\param wait delay between measurements, default = DEFAULT_DELAY/ms 45 * \\param count number of measurements, default DEFAULT_PINGS 46 * \ eturn long distance in centimeter 47 **/ 48 long DistanceAvg(int wait=DEFAULT_DELAY, int count=DEFAULT_PINGS); 49 50 /** 51 * Do only a ping. Get result with methode getDistance() 52 * 53 * \\param keine 54 */ 55 void Ping() ; 56 57 /** 58 * return latest distance. Methode Ping() should be called before 59 * \\param keine 60 * \ eturn Distanz in Zentimeter 61 */ 62 long getDistance(); 63 64 65private: 66 /** 67 * Do the measurement calculation and return result in centimeter 68 * SR04 measure echo time to obstacle and return way. 69 * <br> 70 * Sound travels with 340m/sec 71 * <br> 72 * Example: Obstace 100cm away from SR04. Measure time is 100cm to 73 * obstacle and 100cm return = 200cm 74 * <br> 75 * 1sec = 1000ms = 1.000.000uS 76 * 1.000.000 / 340 = Distance in microseconds for 100cm 77 * 2941uS fuer 100cm = 5882 uS fuer 200cm 78 * 79 * duration / 5882 * 100 = distance in cm 80 */ 81 long MicrosecondsToCentimeter(long duration); 82 83 long _currentDistance; 84 int _echoPin, _triggerPin; 85 long _duration, _distance; 86 bool _autoMode; 87}; 88#endif 89 90 91 92
Downloadable files
Wiring diagram
The distance sensor here only has one output wire but pretend it has another going to PWM12
Wiring diagram
Wiring diagram
The distance sensor here only has one output wire but pretend it has another going to PWM12
Wiring diagram
Comments
Only logged in users can leave comments
squrril
0 Followers
•0 Projects
Table of contents
Intro
7
0