Advertisement
CountRogula

Untitled

Dec 8th, 2023
1,229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.46 KB | Source Code | 0 0
  1. /************************************************************************************************************
  2. /* Christmas Hat Music                                                                                      *
  3. /* Arduino Nano                                                                                                 *
  4. /***********************************************************************************************************/
  5. #include <avr/sleep.h>
  6. #include "pitches.h"
  7. #include "songs.h"
  8.  
  9.  
  10. #define BUZZERPIN 5
  11. #define LEDPIN 8
  12. #define WAKEPIN 3  // pin used for waking up
  13.  
  14.  
  15.  
  16. int tempo = 140;
  17. int thisNote = 0;
  18. int whichSong = 1;
  19. bool inSleepMode = false;
  20.  
  21. void setup() {
  22.   Serial.begin(9600);
  23.   pinMode(LEDPIN, OUTPUT);
  24.   pinMode(BUZZERPIN, OUTPUT);
  25.   pinMode(WAKEPIN, INPUT);
  26. }
  27.  
  28. void loop() {
  29.   Serial.println("loop");
  30.   if (whichSong == 1) {
  31.     playSong(jingleBells, sizeof(jingleBells));
  32.   } else {
  33.     playSong(santaClaus, sizeof(santaClaus));
  34.   }
  35.   whichSong += 1;
  36.   if (whichSong > 2) {
  37.     whichSong = 1;
  38.   }
  39.  
  40.   Serial.println("wait 15 seconds");
  41.   delay(15000);
  42.   sleepNow();
  43. }
  44.  
  45. void playSong(int melody[], int theSize) {
  46.   Serial.println("playSong");
  47.   int flashingLED = 1;
  48.   int notes = theSize / sizeof(melody[0]) / 2;
  49.   thisNote = 0;
  50.  
  51.   // this calculates the duration of a whole note in ms (60s/tempo)*4 beats
  52.   int wholenote = (60000 * 4) / tempo;
  53.  
  54.   int divider = 0, noteDuration = 0;
  55.  
  56.   // iterate over the notes of the melody.
  57.   // Remember, the array is twice the number of notes (notes + durations)
  58.   for (thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
  59.  
  60.     // calculates the duration of each note
  61.     divider = melody[thisNote + 1];
  62.     if (divider > 0) {
  63.       // regular note, just proceed
  64.       noteDuration = (wholenote) / divider;
  65.     } else if (divider < 0) {
  66.       // dotted notes are represented with negative durations!!
  67.       noteDuration = (wholenote) / abs(divider);
  68.       noteDuration *= 1.5;  // increases the duration in half for dotted notes
  69.     }
  70.  
  71.     // we only play the note for 90% of the duration, leaving 10% as a pause
  72.     tone(BUZZERPIN, melody[thisNote], noteDuration * 0.9);
  73.  
  74.     // Wait for the specief duration before playing the next note.
  75.     delay(noteDuration);
  76.  
  77.     // stop the waveform generation before the next note.
  78.     noTone(BUZZERPIN);
  79.  
  80.     if (flashingLED == 0) {
  81.       flashingLED = 1;
  82.       digitalWrite(LEDPIN, HIGH);
  83.     } else {
  84.       flashingLED = 0;
  85.       digitalWrite(LEDPIN, LOW);
  86.     }
  87.   }
  88.   digitalWrite(LEDPIN, LOW);
  89. }
  90.  
  91. void wakeUpNow() {
  92.   if (inSleepMode) {
  93.     detachInterrupt(digitalPinToInterrupt(WAKEPIN));  // disables interrupt 1 on pin 2 so the wakeUpNow code will not be executed during normal running time.
  94.     //sleep_disable();
  95.     digitalWrite(13, LOW);  // first thing after waking from sleep: disable sleep...
  96.   }
  97.   Serial.println("motion detected");
  98. }
  99.  
  100. void sleepNow() {
  101.   noTone(BUZZERPIN);
  102.   Serial.println("entering sleep");
  103.   Serial.flush();
  104.   digitalWrite(13, HIGH);
  105.   delay(20);
  106.   set_sleep_mode(SLEEP_MODE_PWR_DOWN);  // sleep mode is set here
  107.   //sleep_enable();                       // enables the sleep bit in the mcucr register
  108.   attachInterrupt(digitalPinToInterrupt(WAKEPIN), wakeUpNow, RISING);
  109.   inSleepMode = true;  // use interrupt 1 (pin 3) and run function
  110.   sleep_mode();        // here the device is actually put to sleep!!
  111.   // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement