Skip to content

Instantly share code, notes, and snippets.

@DavidHickman
Last active November 29, 2017 18:18
Show Gist options
  • Save DavidHickman/e6f7a296bb72eb249ee29d8096f3c4c7 to your computer and use it in GitHub Desktop.
Save DavidHickman/e6f7a296bb72eb249ee29d8096f3c4c7 to your computer and use it in GitHub Desktop.
Arduino code for creating an animatronic scene
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// These are the pins used for the music maker shield
const int SHIELD_RESET = -1; // VS1053 reset pin (unused!)
const int SHIELD_CS = 7; // VS1053 chip select pin (output)
const int SHIELD_DCS = 6; // VS1053 Data/command select pin (output)
const int DREQ = 3; // VS1053 Data request, ideally an Interrupt pin
const int CARDCS = 4; // Card chip select pin
// Pins for door decorating music and mechanisms (buttons, motors, servos, etc.)
const int startButtonPin = 2; // pin for button to start the scene
const int motorPin = 5; // motor or servo control
int sceneState = LOW; // initialize the scene off
int startButtonState; // create variable to store start button state
int prevStartButtonState = LOW; // initialize button to off
long lastStart = 0; // last time the scene was triggered
long debounce = 200; // debounce time to avoid multiple presses
// Initialize Adafruit mp3 shield
Adafruit_VS1053_FilePlayer musicPlayer =
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
void setup() {
// Set up the button and output pins
pinMode(startButtonPin, INPUT); // declare the start button as input
pinMode(motorPin, OUTPUT); // declare the motor pin as output
musicPlayer.setVolume(20, 20); // Set the left/right channel volume (lower is louder)
}
void loop() {
// Create the program that will run as the song plays
start = digitalRead(startButtonPin);
if (start == HIGH && prevStartButtonState == LOW && millis() - time > debounce) {
if (sceneState == LOW)
musicPlayer.playFullFile("track001.mp3") // name must be 8 letters dot 3 letters
while (musicPlayer.playingMusic) {
// Activate motors and servos as appropriate here
sceneState = HIGH;
digitalWrite(motorPin, sceneState);
}
sceneState = LOW;
time = millis();
}
digitalWrite(motorPin, sceneState);
prevStartButtonState = start;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment