/*
Ultrasonic-Ranger
* Note: position a jumper to connect pin39 and pin19 when you use
LaunchPad MSP430G2553
This example code is in the public domain.
http://www.seeedstudio.com/wiki/Grove_-_Ultrasonic_Ranger
*/
#include "TM1637.h"
#include "Ultrasonic.h"
/* Macro Define */
#define CLK 40 /* 4-digital display clock pin */
#define DIO 39 /* 4-digiral display data pin */
#define BLINK_LED RED_LED /* blink led */
#define ULTRASONIC_PIN 38 /* pin of the Ultrasonic Ranger */
#define LED RED_LED /* LED pin*/
#define ON HIGH /* led on */
#define OFF LOW /* led off */
#define _handle_led(x) digitalWrite(LED, x) /* handle led */
#define BUZZER_PIN 24 /* sig pin of the buzzer */
#define RELAY_PIN 9
/* Global Varibles */
TM1637 tm1637(CLK, DIO); /* 4-digital display object */
Ultrasonic ultrasonic(ULTRASONIC_PIN); /* Ultrasonic Ranger object */
int distance = 0; /* varible to store the distance to obstacles in front */
int blink_interval = 0; /* led delay time */
int8_t bits[4] = {0}; /* array to store the single bits of the value */
int length = 10; /* the number of notes */
char notes[] = "aa"; /* the notes "ccggaaffeeddc "*/
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; /* the beats*/
int tempo = 300; /* the tempo */
int THRESHOLD_DIST = 20; /* distance in cm */
/* the setup() method runs once, when the sketch starts */
void setup() {
Serial.begin(9600);
/* Initialize 4-digital display */
tm1637.init();
tm1637.set(BRIGHT_TYPICAL);
/* Initialize led pin */
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
/* declare the red_led pin as an OUTPUT */
pinMode(RED_LED, OUTPUT);
/* set buzzer pin as output */
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
}
/* the loop() method runs over and over again */
void loop() {
distance = ultrasonic.MeasureInCentimeters();
/* read the value from the sensor */
if(distance < THRESHOLD_DIST){
Serial.print("in if");
digitalWrite(RELAY_PIN, HIGH); // turn the relay on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(RELAY_PIN, LOW); // turn the relay off by making the voltage LOW
delay(100); // wait for a second
for(int i = 0; i < length; i++) {
if(notes[i] == ' ') {
delay(beats[i] * tempo);
} else {
playNote(notes[i], beats[i] * tempo);
}
delay(tempo / 2); /* delay between notes */
}
} else {
Serial.print("off");
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(RELAY_PIN, LOW);
}
Serial.print("distance =");
Serial.println(distance);
memset(bits, 0, 4); /* reset array when we use it */
for(int i = 3; i >= 0; i--) {
/* get single bits of the analog value */
bits[i] = distance % 10;
distance = distance / 10;
tm1637.display(i, bits[i]); /* display by 4-digital display */
}
delay(100);
Serial.print("thresh =");
Serial.println(THRESHOLD_DIST);
Serial.print("dist < thresh");
Serial.println(distance < THRESHOLD_DIST);
}
void playTone(int _tone, int duration) {
for(long i = 0; i < duration * 1000L; i += _tone*2 ) {
digitalWrite(BUZZER_PIN, HIGH);
delayMicroseconds(_tone);
digitalWrite(BUZZER_PIN, LOW);
delayMicroseconds(_tone);
}
}
/* play notes */
void playNote(char note, int duration) {
char name[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'c'};
int _tone[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
/* play the tone corresponding to the tone name */
for(int i = 0; i < 8; i++) {
if(name[i] == note) {
playTone(_tone[i], duration);
}
}
}
Comments
Please log in or sign up to comment.