Components and supplies
2
Ultrasonic Sensor - HC-SR04 (Generic)
1
Buzzer
1
Tactile Switch, Top Actuated
1
Arduino Mega 2560
Apps and platforms
1
Arduino IDE
Project description
Code
Untitled file
arduino
1#include "pitches.h" //add note library 2 3 4 5#define trigPin2 23 // pin for sending sensor #1 trigger signal 6#define echoPin2 22 // pin for receiving sensor #1 echo signal 7#define trigPin1 25 // pin for sending sensor #2 trigger signal 8#define echoPin1 24 // pin for receiving sensor #2 echo signal 9 10 11//digital pin 12 has a button attached to it. Give it an name 12int buttonPin1= 12; 13int buttonPin2= 11; 14int buttonPin3= 10; 15 16 17 18int buzzerPin = 8; 19 20 21 22// defines variables 23long duration1; // variable for the duration of sound wave travel 24long duration2; 25int distance1; // variable for centimeters measurement 26int distance2; // variable for inches measurement 27 28long d1; // sensor 1 distance 29long d2; // sensor 2 distance 30long t1 = 0; // sensor 1 timestamp; initialize as 0 31long t2 = 0; // sensor 2 timestamp; initialize as 0 32unsigned long start_time; // time since program start 33float max_distance = 15; // movement sensing range (in cm) 34float min_distance =3; 35 36 37 38void setup() { 39 //make the button's pin input 40 pinMode(buttonPin1, INPUT); 41 pinMode(buttonPin2, INPUT); 42 pinMode(buttonPin3, INPUT); 43 44 45 46 pinMode(trigPin1, OUTPUT); // Sets the trigPin as an OUTPUT 47 pinMode(echoPin1, INPUT); // Sets the echoPin as an INPUT 48 pinMode(trigPin2, OUTPUT); // Sets the trigPin as an OUTPUT 49 pinMode(echoPin2, INPUT); // Sets the echoPin as an INPUT 50 //Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed 51 52 start_time = millis(); // get program start time 53} 54void loop() { 55 56//read the input pin 57 int buttonState1 = digitalRead(buttonPin1); 58 int buttonState2 = digitalRead(buttonPin2); 59 int buttonState3 = digitalRead(buttonPin3); 60 61 if (buttonState1 == 0 && buttonState2 == 0 && buttonState3 == 0) { // if left sensor triggered first 62 noTone(buzzerPin); // Stop sound... 63 } 64 65 // Clears the trigPin condition 66 digitalWrite(trigPin1, LOW); 67 delayMicroseconds(2); 68 // Sets the trigPin HIGH (ACTIVE) for 10 microseconds 69 digitalWrite(trigPin1, HIGH); 70 delayMicroseconds(10); 71 digitalWrite(trigPin1, LOW); 72 // Reads the echoPin, returns the sound wave travel time in microseconds 73 duration1 = pulseIn(echoPin1, HIGH); 74 75 // Clears the trigPin condition 76 digitalWrite(trigPin2, LOW); 77 delayMicroseconds(2); 78 // Sets the trigPin HIGH (ACTIVE) for 10 microseconds 79 digitalWrite(trigPin2, HIGH); 80 delayMicroseconds(10); 81 digitalWrite(trigPin2, LOW); 82 // Reads the echoPin, returns the sound wave travel time in microseconds 83 duration2 = pulseIn(echoPin2, HIGH); 84 85 // Calculating the distance 86 distance1 = duration1 * 0.034 / 2; // Speed of sound wave divided by 2 (go and back) 87 distance2 = duration2 * 0.034 / 2; // Speed of sound wave divided by 2 (go and back) 88 89 90 91 d1 = distance1; 92 d2 = distance2; 93 94 // if either sensor distance drops below limit, record timestamp 95 if (d1 < max_distance && d1 > 3) { 96 t1 = millis(); 97 } 98 else if (d2 < max_distance && d2 > 3) { 99 t2 = millis(); 100 } 101 102 if (t1 > 0 && t2 > 0) { // if both sensors have nonzero timestamps 103 if ((t1 < t2)) { 104 noTone(buzzerPin); 105 //Serial.println("Left to right"); // direction is left to right 106 if (buttonState1 == 1 && buttonState2 == 0 && buttonState3 == 0) { 107 tone(buzzerPin, NOTE_C4); 108 } 109 else if (buttonState1 == 0 && buttonState2 == 1 && buttonState3 == 0){ 110 tone(buzzerPin, NOTE_D4); 111 } 112 else if (buttonState1 == 0 && buttonState2 == 0 && buttonState3 == 1){ 113 tone(buzzerPin, NOTE_E4); 114 } 115 else if (buttonState1 == 1 && buttonState2 == 1 && buttonState3 == 0){ 116 tone(buzzerPin, NOTE_F4); 117 } 118 else if (buttonState1 == 1 && buttonState2 == 0 && buttonState3 == 1){ 119 tone(buzzerPin, NOTE_G4); 120 } 121 else if (buttonState1 == 0 && buttonState2 == 1 && buttonState3 == 1){ 122 tone(buzzerPin, NOTE_A4); 123 } 124 else if (buttonState1 == 1 && buttonState2 == 1 && buttonState3 == 1){ 125 tone(buzzerPin, NOTE_B4); 126 } 127 128 } 129 130 131 else if (t2 < t1) { 132 noTone(buzzerPin); // if right sensor triggered first' 133 //Serial.println("Right to left"); // direction is right to left 134 if (buttonState1 == 1 && buttonState2 == 0 && buttonState3 == 0) { 135 tone(buzzerPin, NOTE_C4); 136 } 137 else if (buttonState1 == 0 && buttonState2 == 1 && buttonState3 == 0){ 138 tone(buzzerPin, NOTE_D4); 139 } 140 else if (buttonState1 == 0 && buttonState2 == 0 && buttonState3 == 1){ 141 tone(buzzerPin, NOTE_E4); 142 } 143 else if (buttonState1 == 1 && buttonState2 == 1 && buttonState3 == 0){ 144 tone(buzzerPin, NOTE_F4); 145 } 146 else if (buttonState1 == 1 && buttonState2 == 0 && buttonState3 == 1){ 147 tone(buzzerPin, NOTE_G4); 148 } 149 else if (buttonState1 == 0 && buttonState2 == 1 && buttonState3 == 1){ 150 tone(buzzerPin, NOTE_A4); 151 } 152 else if (buttonState1 == 1 && buttonState2 == 1 && buttonState3 == 1){ 153 tone(buzzerPin, NOTE_B4); 154 } 155 } 156 // else { 157 // Serial.println("No movement"); // else print nothing (shouldn't happen) 158 // } 159 160 // after printing direction, reset both timestamps to 0 161 t1 = 0; 162 t2 = 0; 163 } 164 165}
Downloadable files
CIRCUIT
I am using Mega2560. But in the image is Arduino UNO. The port number is not correct. The code is the correct.
CIRCUIT
Comments
Only logged in users can leave comments