|
| 1 | +/* |
| 2 | + AUTOCHEER DEVICE |
| 3 | + --- |
| 4 | + code by Andy Doro https://andydoro.com/ |
| 5 | +
|
| 6 | + using Adafruit Feather hardware |
| 7 | + will play an MP3 at a specified time each day |
| 8 | +
|
| 9 | + sketch can easily be modified to perform some other functions at the specified time, such as operate physical/analog noisemakers |
| 10 | +
|
| 11 | +
|
| 12 | + HARDWARE |
| 13 | + --- |
| 14 | + - any Feather board, e.g. Feather M0 Basic Proto https://www.adafruit.com/product/2772 |
| 15 | +
|
| 16 | + - Adalogger FeatherWing https://www.adafruit.com/product/2922 |
| 17 | + - CR1220 coin cell https://www.adafruit.com/product/380 |
| 18 | +
|
| 19 | + - Music Maker FeatherWing https://www.adafruit.com/product/3357 for 1/8" audio jack output |
| 20 | + or Music Maker FeatherWing w/Amp https://www.adafruit.com/product/3436 for speaker wire output |
| 21 | + - MicroSD card https://www.adafruit.com/product/1294 FAT formatted with "cheer.mp3" |
| 22 | +
|
| 23 | + - FeatherWing Tripler https://www.adafruit.com/product/3417 |
| 24 | + or Feather Stacking Headers https://www.adafruit.com/product/2830 for a different form factor |
| 25 | +
|
| 26 | +
|
| 27 | +
|
| 28 | + SOFTWARE |
| 29 | + --- |
| 30 | + libraries |
| 31 | + - VS1053 https://github.com/adafruit/Adafruit_VS1053_Library for Music Maker |
| 32 | + - RCTlib https://github.com/adafruit/RTClib for RTC |
| 33 | + - DST_RTC https://github.com/andydoro/DST_RTC for Daylight Saving Time adjustments |
| 34 | +
|
| 35 | + cheer.mp3, place on FAT formatted SD card and insert into Music Maker |
| 36 | + http://www.orangefreesounds.com/street-crowd-cheering-and-applauding/ |
| 37 | +
|
| 38 | +*/ |
| 39 | + |
| 40 | +// Specifically for use with the Adafruit Feather, the pins are pre-set here! |
| 41 | + |
| 42 | +// include SPI, MP3 and SD libraries |
| 43 | +#include <SPI.h> |
| 44 | +#include <SD.h> |
| 45 | +#include <Adafruit_VS1053.h> // https://github.com/adafruit/Adafruit_VS1053_Library |
| 46 | + |
| 47 | +// Date and time functions using a DS3231 RTC connected via I2C and Wire lib |
| 48 | +#include <Wire.h> |
| 49 | +#include "RTClib.h" // https://github.com/adafruit/RTClib |
| 50 | +#include "DST_RTC.h" // download from https://github.com/andydoro/DST_RTC |
| 51 | + |
| 52 | + |
| 53 | +// These are the pins used |
| 54 | +#define VS1053_RESET -1 // VS1053 reset pin (not used!) |
| 55 | + |
| 56 | +// Feather ESP8266 |
| 57 | +#if defined(ESP8266) |
| 58 | +#define VS1053_CS 16 // VS1053 chip select pin (output) |
| 59 | +#define VS1053_DCS 15 // VS1053 Data/command select pin (output) |
| 60 | +#define CARDCS 2 // Card chip select pin |
| 61 | +#define VS1053_DREQ 0 // VS1053 Data request, ideally an Interrupt pin |
| 62 | + |
| 63 | +// Feather ESP32 |
| 64 | +#elif defined(ESP32) |
| 65 | +#define VS1053_CS 32 // VS1053 chip select pin (output) |
| 66 | +#define VS1053_DCS 33 // VS1053 Data/command select pin (output) |
| 67 | +#define CARDCS 14 // Card chip select pin |
| 68 | +#define VS1053_DREQ 15 // VS1053 Data request, ideally an Interrupt pin |
| 69 | + |
| 70 | +// Feather Teensy3 |
| 71 | +#elif defined(TEENSYDUINO) |
| 72 | +#define VS1053_CS 3 // VS1053 chip select pin (output) |
| 73 | +#define VS1053_DCS 10 // VS1053 Data/command select pin (output) |
| 74 | +#define CARDCS 8 // Card chip select pin |
| 75 | +#define VS1053_DREQ 4 // VS1053 Data request, ideally an Interrupt pin |
| 76 | + |
| 77 | +// WICED feather |
| 78 | +#elif defined(ARDUINO_STM32_FEATHER) |
| 79 | +#define VS1053_CS PC7 // VS1053 chip select pin (output) |
| 80 | +#define VS1053_DCS PB4 // VS1053 Data/command select pin (output) |
| 81 | +#define CARDCS PC5 // Card chip select pin |
| 82 | +#define VS1053_DREQ PA15 // VS1053 Data request, ideally an Interrupt pin |
| 83 | + |
| 84 | +#elif defined(ARDUINO_NRF52832_FEATHER ) |
| 85 | +#define VS1053_CS 30 // VS1053 chip select pin (output) |
| 86 | +#define VS1053_DCS 11 // VS1053 Data/command select pin (output) |
| 87 | +#define CARDCS 27 // Card chip select pin |
| 88 | +#define VS1053_DREQ 31 // VS1053 Data request, ideally an Interrupt pin |
| 89 | + |
| 90 | +// Feather M4, M0, 328, nRF52840 or 32u4 |
| 91 | +#else |
| 92 | +#define VS1053_CS 6 // VS1053 chip select pin (output) |
| 93 | +#define VS1053_DCS 10 // VS1053 Data/command select pin (output) |
| 94 | +#define CARDCS 5 // Card chip select pin |
| 95 | +// DREQ should be an Int pin *if possible* (not possible on 32u4) |
| 96 | +#define VS1053_DREQ 9 // VS1053 Data request, ideally an Interrupt pin |
| 97 | + |
| 98 | +#endif |
| 99 | + |
| 100 | +Adafruit_VS1053_FilePlayer musicPlayer = |
| 101 | + Adafruit_VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS); |
| 102 | + |
| 103 | + |
| 104 | +RTC_DS3231 rtc; |
| 105 | +//RTC_PCF8523 rtc; // RTC object |
| 106 | +DST_RTC dst_rtc; // DST object |
| 107 | + |
| 108 | +// Do you live in a country or territory that observes Daylight Saving Time? |
| 109 | +// https://en.wikipedia.org/wiki/Daylight_saving_time_by_country |
| 110 | +// Use 1 if you observe DST, 0 if you don't. This is programmed for DST in the US / Canada. If your territory's DST operates differently, |
| 111 | +// you'll need to modify the code in the DST_RTC library to make this work properly. |
| 112 | +#define OBSERVE_DST 1 |
| 113 | + |
| 114 | +// the hour and minute you'd like MP3 to start playing |
| 115 | +const int PLAYHOUR = 19; // 24 hour time |
| 116 | +const int PLAYMIN = 0; |
| 117 | + |
| 118 | +const int VOLUME = 0; // lower means louder! |
| 119 | + |
| 120 | + |
| 121 | +void setup() { |
| 122 | + // put your setup code here, to run once: |
| 123 | + Serial.begin(115200); |
| 124 | + // if you're using Bluefruit or LoRa/RFM Feather, disable the radio module |
| 125 | + //pinMode(8, INPUT_PULLUP); |
| 126 | + |
| 127 | + // Wait for serial port to be opened, remove this line for 'standalone' operation |
| 128 | + /*while (!Serial) { |
| 129 | + delay(1); |
| 130 | + } |
| 131 | + */ |
| 132 | + delay(500); |
| 133 | + Serial.println("\n\nAdafruit VS1053 Feather Test"); |
| 134 | + |
| 135 | + if (! musicPlayer.begin()) { // initialise the music player |
| 136 | + Serial.println(F("Couldn't find VS1053, do you have the right pins defined?")); |
| 137 | + while (1); |
| 138 | + } |
| 139 | + |
| 140 | + Serial.println(F("VS1053 found")); |
| 141 | + |
| 142 | + |
| 143 | + // Set volume for left, right channels. lower numbers == louder volume! |
| 144 | + musicPlayer.setVolume(VOLUME, VOLUME); |
| 145 | + |
| 146 | + musicPlayer.sineTest(0x44, 500); // Make a tone to indicate VS1053 is working |
| 147 | + |
| 148 | + if (!SD.begin(CARDCS)) { |
| 149 | + Serial.println(F("SD failed, or not present")); |
| 150 | + while (1); // don't do anything more |
| 151 | + } |
| 152 | + Serial.println("SD OK!"); |
| 153 | + |
| 154 | + // list files |
| 155 | + printDirectory(SD.open("/"), 0); |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +#if defined(__AVR_ATmega32U4__) |
| 160 | + // Timer interrupts are not suggested, better to use DREQ interrupt! |
| 161 | + // but we don't have them on the 32u4 feather... |
| 162 | + musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int |
| 163 | +#else |
| 164 | + // If DREQ is on an interrupt pin we can do background |
| 165 | + // audio playing |
| 166 | + musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int |
| 167 | +#endif |
| 168 | + |
| 169 | + // Play a file in the background, REQUIRES interrupts! |
| 170 | + /* |
| 171 | + Serial.println(F("Playing full track 001")); |
| 172 | + musicPlayer.playFullFile("/track001.mp3"); |
| 173 | +
|
| 174 | + Serial.println(F("Playing track 002")); |
| 175 | + musicPlayer.startPlayingFile("/track002.mp3"); |
| 176 | + */ |
| 177 | + |
| 178 | + // start RTC |
| 179 | + if (! rtc.begin()) { |
| 180 | + Serial.println("Couldn't find RTC"); |
| 181 | + while (1); |
| 182 | + } |
| 183 | + |
| 184 | + // set RTC time if needed |
| 185 | + //if (rtc.lostPower()) { |
| 186 | + if (! rtc.initialized()) { |
| 187 | + Serial.println("RTC lost power, lets set the time!"); |
| 188 | + // following line sets the RTC to the date & time this sketch was compiled |
| 189 | + rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); |
| 190 | + // DST? If we're in it, let's subtract an hour from the RTC time to keep our DST calculation correct. This gives us |
| 191 | + // Standard Time which our DST check will add an hour back to if we're in DST. |
| 192 | + if (OBSERVE_DST == 1) { |
| 193 | + DateTime standardTime = rtc.now(); |
| 194 | + if (dst_rtc.checkDST(standardTime) == true) { // check whether we're in DST right now. If we are, subtract an hour. |
| 195 | + standardTime = standardTime.unixtime() - 3600; |
| 196 | + } |
| 197 | + rtc.adjust(standardTime); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +void loop() { |
| 203 | + // put your main code here, to run repeatedly: |
| 204 | + |
| 205 | + // check time |
| 206 | + if (OBSERVE_DST == 1) { |
| 207 | + DateTime theTime = dst_rtc.calculateTime(rtc.now()); // takes into account DST |
| 208 | + } else { |
| 209 | + DateTime theTime = rtc.now(); // use if you don't need DST |
| 210 | + } |
| 211 | + |
| 212 | + printTheTime(theTime); |
| 213 | + |
| 214 | + byte theHour = theTime.hour(); |
| 215 | + byte theMinute = theTime.minute(); |
| 216 | + |
| 217 | + //check whether it's time to play mp3 |
| 218 | + if (theHour == PLAYHOUR && theMinute == PLAYMIN) { |
| 219 | + Serial.println(F("Playing full track")); |
| 220 | + musicPlayer.playFullFile("/cheer.mp3"); |
| 221 | + } |
| 222 | + |
| 223 | + // only check every second |
| 224 | + delay(1000); |
| 225 | + |
| 226 | +} |
| 227 | + |
| 228 | + |
| 229 | +// File listing helper |
| 230 | +void printDirectory(File dir, int numTabs) { |
| 231 | + while (true) { |
| 232 | + |
| 233 | + File entry = dir.openNextFile(); |
| 234 | + if (! entry) { |
| 235 | + // no more files |
| 236 | + //Serial.println("**nomorefiles**"); |
| 237 | + break; |
| 238 | + } |
| 239 | + for (uint8_t i = 0; i < numTabs; i++) { |
| 240 | + Serial.print('\t'); |
| 241 | + } |
| 242 | + Serial.print(entry.name()); |
| 243 | + if (entry.isDirectory()) { |
| 244 | + Serial.println("/"); |
| 245 | + printDirectory(entry, numTabs + 1); |
| 246 | + } else { |
| 247 | + // files have sizes, directories do not |
| 248 | + Serial.print("\t\t"); |
| 249 | + Serial.println(entry.size(), DEC); |
| 250 | + } |
| 251 | + entry.close(); |
| 252 | + } |
| 253 | +} |
| 254 | + |
| 255 | +// print time to serial |
| 256 | +void printTheTime(DateTime theTimeP) { |
| 257 | + Serial.print(theTimeP.year(), DEC); |
| 258 | + Serial.print('/'); |
| 259 | + Serial.print(theTimeP.month(), DEC); |
| 260 | + Serial.print('/'); |
| 261 | + Serial.print(theTimeP.day(), DEC); |
| 262 | + Serial.print(' '); |
| 263 | + Serial.print(theTimeP.hour(), DEC); |
| 264 | + Serial.print(':'); |
| 265 | + Serial.print(theTimeP.minute(), DEC); |
| 266 | + Serial.print(':'); |
| 267 | + Serial.print(theTimeP.second(), DEC); |
| 268 | + Serial.println(); |
| 269 | +} |
0 commit comments