Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************************************************************************
- /* Christmas Hat Music *
- /* Arduino Nano *
- /***********************************************************************************************************/
- #include <avr/sleep.h>
- #include "pitches.h"
- #include "songs.h"
- #define BUZZERPIN 5
- #define LEDPIN 8
- #define WAKEPIN 3 // pin used for waking up
- int tempo = 140;
- int thisNote = 0;
- int whichSong = 1;
- bool inSleepMode = false;
- void setup() {
- Serial.begin(9600);
- pinMode(LEDPIN, OUTPUT);
- pinMode(BUZZERPIN, OUTPUT);
- pinMode(WAKEPIN, INPUT);
- }
- void loop() {
- Serial.println("loop");
- if (whichSong == 1) {
- playSong(jingleBells, sizeof(jingleBells));
- } else {
- playSong(santaClaus, sizeof(santaClaus));
- }
- whichSong += 1;
- if (whichSong > 2) {
- whichSong = 1;
- }
- Serial.println("wait 15 seconds");
- delay(15000);
- sleepNow();
- }
- void playSong(int melody[], int theSize) {
- Serial.println("playSong");
- int flashingLED = 1;
- int notes = theSize / sizeof(melody[0]) / 2;
- thisNote = 0;
- // this calculates the duration of a whole note in ms (60s/tempo)*4 beats
- int wholenote = (60000 * 4) / tempo;
- int divider = 0, noteDuration = 0;
- // iterate over the notes of the melody.
- // Remember, the array is twice the number of notes (notes + durations)
- for (thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
- // calculates the duration of each note
- divider = melody[thisNote + 1];
- if (divider > 0) {
- // regular note, just proceed
- noteDuration = (wholenote) / divider;
- } else if (divider < 0) {
- // dotted notes are represented with negative durations!!
- noteDuration = (wholenote) / abs(divider);
- noteDuration *= 1.5; // increases the duration in half for dotted notes
- }
- // we only play the note for 90% of the duration, leaving 10% as a pause
- tone(BUZZERPIN, melody[thisNote], noteDuration * 0.9);
- // Wait for the specief duration before playing the next note.
- delay(noteDuration);
- // stop the waveform generation before the next note.
- noTone(BUZZERPIN);
- if (flashingLED == 0) {
- flashingLED = 1;
- digitalWrite(LEDPIN, HIGH);
- } else {
- flashingLED = 0;
- digitalWrite(LEDPIN, LOW);
- }
- }
- digitalWrite(LEDPIN, LOW);
- }
- void wakeUpNow() {
- if (inSleepMode) {
- detachInterrupt(digitalPinToInterrupt(WAKEPIN)); // disables interrupt 1 on pin 2 so the wakeUpNow code will not be executed during normal running time.
- //sleep_disable();
- digitalWrite(13, LOW); // first thing after waking from sleep: disable sleep...
- }
- Serial.println("motion detected");
- }
- void sleepNow() {
- noTone(BUZZERPIN);
- Serial.println("entering sleep");
- Serial.flush();
- digitalWrite(13, HIGH);
- delay(20);
- set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
- //sleep_enable(); // enables the sleep bit in the mcucr register
- attachInterrupt(digitalPinToInterrupt(WAKEPIN), wakeUpNow, RISING);
- inSleepMode = true; // use interrupt 1 (pin 3) and run function
- sleep_mode(); // here the device is actually put to sleep!!
- // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement