Skip to content

Instantly share code, notes, and snippets.

@technomalogical
Last active October 27, 2015 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technomalogical/a098ef40e62f4562ce47 to your computer and use it in GitHub Desktop.
Save technomalogical/a098ef40e62f4562ce47 to your computer and use it in GitHub Desktop.
"I wouldn't ring the doorbell" Halloween Decoration - Arduino Code
/*
* "I Wouldn't Ring The Doorbell" Halloween Prop Code
* Video: https://youtu.be/pO9J1Tid65o
*/
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// These are the pins used for the breakout example
#define BREAKOUT_RESET 9 // VS1053 reset pin (output)
#define BREAKOUT_CS 10 // VS1053 chip select pin (output)
#define BREAKOUT_DCS 8 // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET -1 // VS1053 reset pin (unused!)
#define SHIELD_CS 7 // VS1053 chip select pin (output)
#define SHIELD_DCS 6 // VS1053 Data/command select pin (output)
// These are common pins between breakout and shield
#define CARDCS 4 // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer =
// create breakout-example object!
//Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
// create shield-example object!
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
////
#define NEXT_TRIGGER 30000
#define LED_COUNT 4
int leds[] = { 1, 5, 8, 9 };
#define PIR_PIN 2
uint8_t pirState = LOW;
// once this is working, reprogram with a cycle time of 100 to see more clearly how the throbbing works
int cycleTime = 20; // the total duration of a PWM cycle
int changeSpeed = 1; // throb speed, or the number of milliseconds to change the onTimePerCycle per Cycle
boolean brightnessGoingUp = true; // whether the brightness is going up or down
int onTimePerCycle = 0; // the number of milliseconds to turn the LED within a cycle
void setup() {
Serial.begin(9600);
Serial.println("Adafruit VS1053 Library Test");
// initialise the music player
if (! musicPlayer.begin()) { // initialise the music player
Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
while (1);
}
Serial.println(F("VS1053 found"));
musicPlayer.sineTest(0x44, 500); // Make a tone to indicate VS1053 is working
if (!SD.begin(CARDCS)) {
Serial.println(F("SD failed, or not present"));
while (1); // don't do anything more
}
Serial.println("SD OK!");
// list files
printDirectory(SD.open("/"), 0);
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(20,20);
/***** Two interrupt options! *******/
// This option uses timer0, this means timer1 & t2 are not required
// (so you can use 'em for Servos, etc) BUT millis() can lose time
// since we're hitchhiking on top of the millis() tracker
//musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT);
// This option uses a pin interrupt. No timers required! But DREQ
// must be on an interrupt pin. For Uno/Duemilanove/Diecimilla
// that's Digital #2 or #3
// See http://arduino.cc/en/Reference/attachInterrupt for other pins
// *** This method is preferred
if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
Serial.println(F("DREQ pin is not an interrupt pin"));
for(int i=0; i<LED_COUNT; i++) {
pinMode(leds[i], OUTPUT);
}
Serial.println(F("LED setup complete"));
// Input for PIR Motion Sensor
pinMode(PIR_PIN,INPUT);
Serial.println(F("PIR setup complete"));
}
void loop() // run over and over again
{
int sense = digitalRead(PIR_PIN);
// if(!musicPlayer.playingMusic) {
if(sense==HIGH) {
if (! musicPlayer.startPlayingFile("doorbell.mp3")) {
Serial.println("Could not open file doorbell.mp3");
while (1);
}
Serial.println("PIR Pin HIGH!");
Serial.println(musicPlayer.playingMusic);
Serial.println(F("Started playing"));
Serial.println("Starting to blink LEDs");
while (musicPlayer.playingMusic) {
blinkLeds();
}
for(int i=0; i<LED_COUNT; i++) {
digitalWrite(leds[i], LOW);
}
delay(NEXT_TRIGGER);
}
// }
}
void blinkLeds() {
for(int i=0; i<LED_COUNT; i++) {
digitalWrite(leds[i], HIGH);
}
delay(onTimePerCycle); // wait for onTimePerCycle milliseconds
for(int i=0; i<LED_COUNT; i++) {
digitalWrite(leds[i], LOW);
}
delay(cycleTime - onTimePerCycle); // waitfor the rest of the complete cycle
// Now adjust the brightness up or down depending the the current state
if (brightnessGoingUp) {
onTimePerCycle += changeSpeed;
}
else {
onTimePerCycle -= changeSpeed;
}
// if we are at full brightness, where the LED is on for the complete cycle
if (onTimePerCycle >= cycleTime) {
brightnessGoingUp = false; // switch mode to brightness going down
onTimePerCycle = cycleTime; // make sure brightness did not go over
}
// if we are at zero brightness, where the LED is not on at all in a cycle
if (onTimePerCycle <= 0) {
brightnessGoingUp = true; // switch mode to brightness going up
onTimePerCycle = 0; // make sure the onTime didn't go bellow zero
}
}
/// File listing helper
void printDirectory(File dir, int numTabs) {
while(true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
//Serial.println("**nomorefiles**");
break;
}
for (uint8_t i=0; i<numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs+1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment