-
-
Save softplus/3b1b4a8bc0c556ffddfd8bcc2029d188 to your computer and use it in GitHub Desktop.
Ultrasound MP3 player on Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See https://johnmu.com/ultrasonic-triggered-mp3-player-with/ | |
#include <arduino.h> | |
#include <Fat16.h> | |
#include <Fat16Util.h> | |
#include <NewSPI.h> | |
#include “MusicPlayer.h” | |
MusicPlayer myplayer; | |
// I/O pin usage | |
#define US_TRIG 4 | |
#define US_ECHO 5 | |
#define WATCHDOG_LED_PIN 2 | |
// debug modes for ultrasound sensor readings & general debug-text | |
// 1=on, 0=off | |
#define DEBUG_US 1 | |
#define DEBUG_TEXT 0 | |
// cycle for watchdog LED | |
#define WATCHDOG_LED_COUNT 5 | |
int watchdogStatus = 0; | |
int watchdogCounter = 0; | |
// files on Micro-SD card: | |
// msg0004.mp3 (someone saying hello) | |
void setup() { | |
Serial.begin(9600); | |
Serial.println(“setup() starting”); | |
myplayer.keyDisable(); // disable hw keys to enable I/O pins | |
myplayer.digitalControlEnable(); | |
myplayer.begin(); | |
pinMode(US_TRIG, OUTPUT); | |
pinMode(US_ECHO, INPUT); | |
pinMode(WATCHDOG_LED_PIN, OUTPUT); | |
Serial.println(“setup() complete”); | |
} | |
void loop() { | |
Serial.println(“loop() starting, should never end”); | |
myplayer.setPlayMode(MODE_NORMAL); | |
myplayer.setVolume(10); // 0=max, 10=pretty good | |
if (1==DEBUG_TEXT) Serial.println(“starting loop”); | |
int someoneThereCounter = 0; | |
int pauseTime = 300; // ms - loop frequency | |
int timeRequired = 900; // ms - time before it plays | |
while(1) { // this is really my loop | |
if (1==DEBUG_TEXT) Serial.print("."); | |
showImAround(); | |
if (someoneAround()) { | |
if (1==DEBUG_TEXT) { | |
Serial.print(“someone found! counter at “); | |
Serial.print(someoneThereCounter); | |
Serial.print(” of “); | |
Serial.print(timeRequired); | |
Serial.println(“ms”); | |
} | |
someoneThereCounter += pauseTime; | |
if (someoneThereCounter >= timeRequired) { | |
if (1==DEBUG_TEXT) Serial.println(“still with us, play”); | |
myplayer.setPlayMode(MODE_NORMAL); // set mode to repeat to play a song | |
myplayer.playSong(“msg0004.mp3”); // play a song named with this name | |
if (1==DEBUG_TEXT) Serial.println(“short wait”); | |
delay(8000); // pause | |
if (1==DEBUG_TEXT) Serial.println(“awaiting next”); | |
someoneThereCounter = 0; | |
} | |
} else someoneThereCounter = 0; | |
delay(pauseTime); | |
} | |
if (1==DEBUG_TEXT) Serial.println("loop() complete, something broke?"); | |
} | |
boolean someoneAround() { | |
// return true if someone around | |
// check ultrasound sensor for nearby person | |
int distance = ultrasoundDistance(); | |
if (distance<=0) return false; // no good measurement or off the scale high | |
if (distance<70) return true; // close enough | |
return false; // further away | |
} | |
int ultrasoundDistance() { | |
int duration, distance; | |
digitalWrite(US_TRIG, HIGH); | |
delayMicroseconds(100); // 100us pulse | |
digitalWrite(US_TRIG, LOW); | |
duration = pulseIn(US_ECHO, HIGH, 10000); // max 10ms wait | |
distance = (duration/2) / 29.1; // get distance in cm | |
if (1==DEBUG_US) { | |
Serial.print(“Duration: “); | |
Serial.print(duration); | |
Serial.print(“ms; “); | |
Serial.print(“distance: “); | |
} | |
if (distance >= 900 || distance <= 0){ | |
if (1==DEBUG_US) Serial.println(“Out of range”); | |
distance = -1; | |
} | |
else { | |
if (1==DEBUG_US) { | |
Serial.print(distance); | |
Serial.println(” cm”); | |
} | |
} | |
return distance; | |
} | |
int showImAround() { | |
// flicker LED on/off with given frequency | |
watchdogCounter += 1; | |
if (watchdogCounter > WATCHDOG_LED_COUNT) { | |
watchdogStatus = 1-watchdogStatus; | |
watchdogCounter = 0; | |
if (watchdogStatus==0) { | |
if (1==DEBUG_TEXT) Serial.println(“LED:ON”); | |
digitalWrite(WATCHDOG_LED_PIN, HIGH); | |
} else { | |
if (1==DEBUG_TEXT) Serial.println(“LED:OFF”); | |
digitalWrite(WATCHDOG_LED_PIN, LOW); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment