Don’t get too close or you have to Take on Me
For a futuristic science fair, the retro stuff gets the attention. The ultrasonic sensor is being widely used in the past when most electric cars are using cameras to measure the distance between vehicles on the road, they were using ultrasonics to detect their positions and show them on the screen. The principle is nothing complicated and can be achieved with an ultrasonic transmitter that transmits high-frequency sound and when an object detects and then reflect the signal to the receiver modules, the distance is calculated based on the time required.
Requirements:
1. Arduino Uno
2. Ultrasonic sensor HC-SR04
3. LED
4. Resistor 221 ohm
5. Breadboard
6. Jumper wires
7. Passive buzzer
Step 1: Set up the ultrasonic sensor and test it
Code
const int trig = 12;
const int echo = 13;
int duration = 0;
int distance = 0;
void setup()
{
pinMode(trig , OUTPUT);
pinMode(echo , INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trig , HIGH);
delayMicroseconds(1000);
digitalWrite(trig , LOW);
duration = pulseIn(echo , HIGH);
distance = (duration/2) / 29.1 ;
Serial.println(distance);
}
Corresponding Serial Monitor Output
Step 2: Connect the passive buzzer with jumper wires to GND and 8 on the Uno correspondingly
Step 3: Set up the resistors and connect the jumper wires for the LED
Step 4: Connect the GND and 5V from the breadboard to the UNO correspondingly and rewire the ultrasonic sensor
Code
There are two files in order to play Take On Me by A-ha, one is the pitches.h that has the pitch of the song, and one is the Arduino ino file
pitches.h file: https://gist.github.com/mikeputnam/2820675
Arduino code:
#define trigPin 7
#define echoPin 6
#define LEDlampRed 9
#define LEDlampYellow 10
#define LEDlampGreen 11
#define soundbuzzer 8
int sound = 500;
#include “pitches.h”
// The melody array
int melody[] = {
NOTE_FS5, NOTE_FS5, NOTE_D5, NOTE_B4, NOTE_B4, NOTE_E5,
NOTE_E5, NOTE_E5, NOTE_GS5, NOTE_GS5, NOTE_A5, NOTE_B5,
NOTE_A5, NOTE_A5, NOTE_A5, NOTE_E5, NOTE_D5, NOTE_FS5,
NOTE_FS5, NOTE_FS5, NOTE_E5, NOTE_E5, NOTE_FS5, NOTE_E5
};
// The note duration, 8 = 8th note, 4 = quarter note, etc.
int durations[] = {
8, 8, 8, 4, 4, 4,
4, 5, 8, 8, 8, 8,
8, 8, 8, 4, 4, 4,
4, 5, 8, 8, 8, 8
};
// determine the length of the arrays to use in the loop iteration
int songLength = sizeof(melody)/sizeof(melody[0]);
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDlampRed, OUTPUT);
pinMode(LEDlampYellow, OUTPUT);
pinMode(LEDlampGreen, OUTPUT);
pinMode(soundbuzzer, OUTPUT);
}
void loop() {
long durationindigit, distanceincm;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
durationindigit = pulseIn(echoPin, HIGH);
distanceincm = (durationindigit/5) / 29.1;
if (distanceincm < 50) {
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow,LOW);
digitalWrite(LEDlampRed, LOW);
noTone(soundbuzzer);
}
if (distanceincm < 20) {
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampRed, LOW);
tone(soundbuzzer, sound);
}
if (distanceincm < 5) {
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow,LOW);
digitalWrite(LEDlampRed, HIGH);
sound = 1000;
for (int thisNote = 0; thisNote < songLength; thisNote++){
// determine the duration of the notes that the computer understands
// divide 1000 by the value, so the first note lasts for 1000/8 milliseconds
int duration = 1000/ durations[thisNote];
tone(8, melody[thisNote], duration);
// pause between notes
int pause = duration * 1.3;
delay(pause);
// stop the tone
noTone(8);
}
}
else {
digitalWrite(LEDlampRed,LOW);
}
delay(300);
}
Demo
Initial state (distance under 50cm):
Distance under 20cm:
Distance under 5cm:
Video Demo:
Leave a Reply