Leong Kum Loong (NYP) / tone_h

Dependents:   Tone_Example

Files at this revision

API Documentation at this revision

Comitter:
reesey
Date:
Sun Dec 03 18:47:45 2017 +0000
Parent:
3:9ddabd24f48c
Commit message:
Written by Leong Kum Loong.; 04 Dec 2017, REV. 0; When HCSR04.h reference is included, DO NOT declare pin p21 & p22 as it is used by my library.

Changed in this revision

HCSR04.cpp Show annotated file Show diff for this revision Revisions of this file
HCSR04.h Show annotated file Show diff for this revision Revisions of this file
tone.cpp Show diff for this revision Revisions of this file
tone.h Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.cpp	Sun Dec 03 18:47:45 2017 +0000
@@ -0,0 +1,60 @@
+#include "mbed.h"
+#include "HCSR04.h"
+/*
+    HCSR04.h
+    Written by Leong Kum Loong.
+    04 Dec 2017, REV. 0
+    
+    All routines and functions in this library are written by me solely.
+    Library for HC-SR04 Ultrasonic Ranging sensor.
+    Library uses pins p21 to send pulse to sensor Trigger input & p22 to read sensor Echo output.
+    This is assuming velocity of sound rate from ultrasonic pulse is 340m/s.
+*/
+
+DigitalOut trig(p21);
+DigitalIn echo(p22);
+Timer echoTime;
+Ticker interval;
+
+float distance = 0;
+
+float divisor;
+
+static void findDistance(){
+    trig = 1;
+    wait_ms(10);
+    trig = 0;
+    wait_ms(20);
+    
+    trig = 1;
+    wait_us(10);
+    trig = 0;
+    
+    while(!echo);
+    echoTime.start();
+    while(echo);
+    echoTime.stop();
+    
+    distance = (float)echoTime.read_us()/(float)2 * 0.034 / (float)divisor;
+    echoTime.reset();
+}
+
+//Start monitoring distance.
+void getDistance(int unit){
+    //1 for CM & 2 for INCH
+    switch(unit){
+    case 1:
+        divisor = 1;        //Default divisor to get CM.
+        break;
+    case 2:
+        divisor = 2.54;     //Divisor to get INCH.
+        break;    
+    }
+
+    interval.attach(findDistance, 0.1);
+}
+
+//Retrieve distance stored.
+float readDistance(){
+    return distance;    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.h	Sun Dec 03 18:47:45 2017 +0000
@@ -0,0 +1,22 @@
+/*
+    HCSR04.h
+    Written by Leong Kum Loong.
+    04 Dec 2017, REV. 0
+    
+    All routines and functions in this library are written by me solely.
+    Library for HC-SR04 Ultrasonic Ranging sensor.
+    Library uses pins p21 to send pulse to sensor Trigger input & p22 to read sensor Echo output.
+    This is assuming velocity of sound rate from ultrasonic pulse is 340m/s.
+*/
+
+#ifndef HCSR04_h
+#define HCSR04_h
+#include "mbed.h"
+
+//1 for CM & 2 for INCH
+void getDistance(int unit=1);
+
+//Read current distance.
+float readDistance();
+
+#endif
\ No newline at end of file
--- a/tone.cpp	Mon Nov 20 03:53:48 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,471 +0,0 @@
-#include "mbed.h"
-#include "tone.h"
-/*
-    tone.h
-    Written by Leong Kum Loong.
-    20 Nov 2017, REV. 2
-    
-    All routines and functions in this library are written by me solely with the purpose
-    of implementing an identical "tone" library that was introduced by Arduino IDE.
-    
-    Example reference from Github
-    ===============================
-    The mario_example routine "notes" are reference to the work of Gregory Kielian.
-    The starwars_example routine "notes" are reference to the work of nicksort.
-*/
-
-PwmOut speaker(p26);
-
-void tone(int frequency, int duration_ms){
-    float period = (float)1/(float)frequency;
-    speaker.period(period);
-    speaker.pulsewidth(period/(float)(2));
-    if(duration_ms){
-        wait_ms(duration_ms);
-        speaker = 0;
-    }
-}
-
-void noTone(){
-    speaker = 0;
-}
-
-//Mario theme section.
-static void delay(int millis){
-    wait_ms(millis * 0.7);  //If play speed sounds weird, try modify delay here.
-}
-
-void mario_example(){
-    tone(660,100);         
-    delay(150);
-    tone(660,100);
-    delay(300);
-    tone(660,100);
-    delay(300);
-    tone(510,100);
-    delay(100);
-    tone(660,100);
-    delay(300);
-    tone(770,100);
-    delay(550);
-    tone(380,100);
-    delay(575);
-
-    tone(510,100);
-    delay(450);
-    tone(380,100);
-    delay(400);
-    tone(320,100);
-    delay(500);
-    tone(440,100);
-    delay(300);
-    tone(480,80);
-    delay(330);
-    tone(450,100);
-    delay(150);
-    tone(430,100);
-    delay(300);
-    tone(380,100);
-    delay(200);
-    tone(660,80);
-    delay(200);
-    tone(760,50);
-    delay(150);
-    tone(860,100);
-    delay(300);
-    tone(700,80);
-    delay(150);
-    tone(760,50);
-    delay(350);
-    tone(660,80);
-    delay(300);
-    tone(520,80);
-    delay(150);
-    tone(580,80);
-    delay(150);
-    tone(480,80);
-    delay(500);
-
-    tone(510,100);
-    delay(450);
-    tone(380,100);
-    delay(400);
-    tone(320,100);
-    delay(500);
-    tone(440,100);
-    delay(300);
-    tone(480,80);
-    delay(330);
-    tone(450,100);
-    delay(150);
-    tone(430,100);
-    delay(300);
-    tone(380,100);
-    delay(200);
-    tone(660,80);
-    delay(200);
-    tone(760,50);
-    delay(150);
-    tone(860,100);
-    delay(300);
-    tone(700,80);
-    delay(150);
-    tone(760,50);
-    delay(350);
-    tone(660,80);
-    delay(300);
-    tone(520,80);
-    delay(150);
-    tone(580,80);
-    delay(150);
-    tone(480,80);
-    delay(500);
-
-    tone(500,100);
-    delay(300);
-
-    tone(760,100);
-    delay(100);
-    tone(720,100);
-    delay(150);
-    tone(680,100);
-    delay(150);
-    tone(620,150);
-    delay(300);
-
-    tone(650,150);
-    delay(300);
-    tone(380,100);
-    delay(150);
-    tone(430,100);
-    delay(150);
-
-    tone(500,100);
-    delay(300);
-    tone(430,100);
-    delay(150);
-    tone(500,100);
-    delay(100);
-    tone(570,100);
-    delay(220);
-
-    tone(500,100);
-    delay(300);
-
-    tone(760,100);
-    delay(100);
-    tone(720,100);
-    delay(150);
-    tone(680,100);
-    delay(150);
-    tone(620,150);
-    delay(300);
-
-    tone(650,200);
-    delay(300);
-
-    tone(1020,80);
-    delay(300);
-    tone(1020,80);
-    delay(150);
-    tone(1020,80);
-    delay(300);
-
-    tone(380,100);
-    delay(300);
-    tone(500,100);
-    delay(300);
-
-    tone(760,100);
-    delay(100);
-    tone(720,100);
-    delay(150);
-    tone(680,100);
-    delay(150);
-    tone(620,150);
-    delay(300);
-
-    tone(650,150);
-    delay(300);
-    tone(380,100);
-    delay(150);
-    tone(430,100);
-    delay(150);
-
-    tone(500,100);
-    delay(300);
-    tone(430,100);
-    delay(150);
-    tone(500,100);
-    delay(100);
-    tone(570,100);
-    delay(420);
-
-    tone(585,100);
-    delay(450);
-
-    tone(550,100);
-    delay(420);
-
-    tone(500,100);
-    delay(360);
-
-    tone(380,100);
-    delay(300);
-    tone(500,100);
-    delay(300);
-    tone(500,100);
-    delay(150);
-    tone(500,100);
-    delay(300);
-
-    tone(500,100);
-    delay(300);
-
-    tone(760,100);
-    delay(100);
-    tone(720,100);
-    delay(150);
-    tone(680,100);
-    delay(150);
-    tone(620,150);
-    delay(300);
-
-    tone(650,150);
-    delay(300);
-    tone(380,100);
-    delay(150);
-    tone(430,100);
-    delay(150);
-
-    tone(500,100);
-    delay(300);
-    tone(430,100);
-    delay(150);
-    tone(500,100);
-    delay(100);
-    tone(570,100);
-    delay(220);
-
-    tone(500,100);
-    delay(300);
-
-    tone(760,100);
-    delay(100);
-    tone(720,100);
-    delay(150);
-    tone(680,100);
-    delay(150);
-    tone(620,150);
-    delay(300);
-
-    tone(650,200);
-    delay(300);
-
-    tone(1020,80);
-    delay(300);
-    tone(1020,80);
-    delay(150);
-    tone(1020,80);
-    delay(300);
-
-    tone(380,100);
-    delay(300);
-    tone(500,100);
-    delay(300);
-
-    tone(760,100);
-    delay(100);
-    tone(720,100);
-    delay(150);
-    tone(680,100);
-    delay(150);
-    tone(620,150);
-    delay(300);
-
-    tone(650,150);
-    delay(300);
-    tone(380,100);
-    delay(150);
-    tone(430,100);
-    delay(150);
-
-    tone(500,100);
-    delay(300);
-    tone(430,100);
-    delay(150);
-    tone(500,100);
-    delay(100);
-    tone(570,100);
-    delay(420);
-
-    tone(585,100);
-    delay(450);
-
-    tone(550,100);
-    delay(420);
-
-    tone(500,100);
-    delay(360);
-
-    tone(380,100);
-    delay(300);
-    tone(500,100);
-    delay(300);
-    tone(500,100);
-    delay(150);
-    tone(500,100);
-    delay(300);
-
-    tone(500,60);
-    delay(150);
-    tone(500,80);
-    delay(300);
-    tone(500,60);
-    delay(350);
-    tone(500,80);
-    delay(150);
-    tone(580,80);
-    delay(350);
-    tone(660,80);
-    delay(150);
-    tone(500,80);
-    delay(300);
-    tone(430,80);
-    delay(150);
-    tone(380,80);
-    delay(600);
-
-    tone(500,60);
-    delay(150);
-    tone(500,80);
-    delay(300);
-    tone(500,60);
-    delay(350);
-    tone(500,80);
-    delay(150);
-    tone(580,80);
-    delay(150);
-    tone(660,80);
-    delay(550);
-
-    tone(870,80);
-    delay(325);
-    tone(760,80);
-    delay(600);
-
-    tone(500,60);
-    delay(150);
-    tone(500,80);
-    delay(300);
-    tone(500,60);
-    delay(350);
-    tone(500,80);
-    delay(150);
-    tone(580,80);
-    delay(350);
-    tone(660,80);
-    delay(150);
-    tone(500,80);
-    delay(300);
-    tone(430,80);
-    delay(150);
-    tone(380,80);
-    delay(600);
-
-    tone(660,100);
-    delay(150);
-    tone(660,100);
-    delay(300);
-    tone(660,100);
-    delay(300);
-    tone(510,100);
-    delay(100);
-    tone(660,100);
-    delay(300);
-    tone(770,100);
-    delay(550);
-    tone(380,100);
-    delay(575);
-}
- 
-  
-//Star wars theme section.
-int c = 261, d = 294, e = 329, f = 349, g = 391, gS = 415, a = 440, aS = 455, b = 466, cH = 523;
-int cSH = 554, dH = 587, dSH = 622, eH = 659, fH = 698, fSH = 740, gH = 784, gSH = 830, aH = 880;
-
-void firstSection(){
-    tone(a, 500);
-    tone(a, 500);    
-    tone(a, 500);
-    tone(f, 350);
-    tone(cH, 150);  
-    tone(a, 500);
-    tone(f, 350);
-    tone(cH, 150);
-    tone(a, 650);
-    wait_ms(500);
- 
-    tone(eH, 500);
-    tone(eH, 500);
-    tone(eH, 500);  
-    tone(fH, 350);
-    tone(cH, 150);
-    tone(gS, 500);
-    tone(f, 350);
-    tone(cH, 150);
-    tone(a, 650);
-    wait_ms(500);
-}
- 
-void secondSection(){
-    tone(aH, 500);
-    tone(a, 300);
-    tone(a, 150);
-    tone(aH, 500);
-    tone(gSH, 325);
-    tone(gH, 175);
-    tone(fSH, 125);
-    tone(fH, 125);
-    tone(fSH, 250);
-    wait_ms(325);
- 
-    tone(aS, 250);
-    tone(dSH, 500);
-    tone(dH, 325);  
-    tone(cSH, 175);  
-    tone(cH, 125);  
-    tone(b, 125);  
-    tone(cH, 250);  
-    wait_ms(350);
-}
-
-void starwars_example(){
-    firstSection();
-    
-    secondSection();
- 
-    //Variant 1
-    tone(f, 250);  
-    tone(gS, 500);  
-    tone(f, 350);  
-    tone(a, 125);
-    tone(cH, 500);
-    tone(a, 375);  
-    tone(cH, 125);
-    tone(eH, 650);
-    wait_ms(500);
- 
-    secondSection();
- 
-    //Variant 2
-    tone(f, 250);  
-    tone(gS, 500);  
-    tone(f, 375);  
-    tone(cH, 125);
-    tone(a, 500);  
-    tone(f, 375);  
-    tone(cH, 125);
-    tone(a, 650);
-    wait_ms(650);
-}
\ No newline at end of file
--- a/tone.h	Mon Nov 20 03:53:48 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,27 +0,0 @@
-/*
-    tone.h
-    Written by Leong Kum Loong.
-    20 Nov 2017, REV. 2
-    
-    All routines and functions in this library are written by me solely with the purpose
-    of implementing an identical "tone" library that was introduced by Arduino IDE.
-    
-    Example reference from Github
-    ===============================
-    The mario_example routine "notes" are reference to the work of Gregory Kielian.
-    The starwars_example routine "notes" are reference to the work of nicksort.
-*/
-
-#ifndef tone_h
-#define tone_h
-#include "mbed.h"
-
-void tone(int frequency, int duration_ms=0);    //Sound tone at specific frequency of 50% duty cycle and duration(optional).
-
-void noTone();  //Turn off tone.
-
-void mario_example();  
-
-void starwars_example();
-
-#endif
\ No newline at end of file