apuntes_Arduino/Buzzer/otro/otro.ino

314 lines
6.5 KiB
Arduino
Raw Normal View History

2021-01-05 00:15:16 -03:00
#include <Wire.h>
#include "Sodaq_DS3231.h"
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define melodyPin 3
//Mario main theme melody
int melody[] = {
NOTE_E6, NOTE_E6, 0, NOTE_E6,
0, NOTE_C6, NOTE_E6, 0,
NOTE_G6, 0, 0, 0,
NOTE_G5, 0, 0, 0,
NOTE_C6, 0, 0, NOTE_G5,
0, 0, NOTE_E5, 0,
0, NOTE_A5, 0, NOTE_B5,
0, NOTE_AS5, NOTE_A5, 0,
NOTE_G5, NOTE_E6, NOTE_G6,
NOTE_A6, 0, NOTE_F6, NOTE_G6,
0, NOTE_E6, 0, NOTE_C6,
NOTE_D6, NOTE_B5, 0, 0,
NOTE_C6, 0, 0, NOTE_G5,
0, 0, NOTE_E5, 0,
0, NOTE_A5, 0, NOTE_B5,
0, NOTE_AS5, NOTE_A5, 0,
NOTE_G5, NOTE_E6, NOTE_G6,
NOTE_A6, 0, NOTE_F6, NOTE_G6,
0, NOTE_E6, 0, NOTE_C6,
NOTE_D6, NOTE_B5, 0, 0,
0, 0, NOTE_G6, NOTE_FS6,
NOTE_F6, NOTE_DS6, 0, NOTE_E6,
0, NOTE_GS5, NOTE_A5, NOTE_C6,
0, NOTE_A5, NOTE_C6, NOTE_D6,
0, 0, NOTE_G6, NOTE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NOTE_F6, 0, NOTE_F6,
NOTE_A5, 0, 0, 0,
NOTE_B5, NOTE_A6, NOTE_A6,
NOTE_A6, NOTE_G6, NOTE_F6,
NOTE_E6, NOTE_C6, 0, NOTE_A5,
NOTE_G5, 0, 0, 0,
NOTE_E6, NOTE_C6, 0, NOTE_G5,
0, 0, NOTE_GS5, 0,
NOTE_A5, NOTE_F6, 0, NOTE_F6,
NOTE_A5, 0, 0, 0,
NOTE_B5, NOTE_F6, 0, NOTE_F6,
NOTE_F6, NOTE_E6, NOTE_D6,
NOTE_C6, NOTE_E5, 0, NOTE_E5,
NOTE_C5, 0, 0, 0,
};
//Mario main them tempo
int tempo[] = {
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
};
char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
void setup(void)
{
pinMode(3, OUTPUT);
pinMode(13, OUTPUT);
pinMode(4, INPUT);
Wire.begin();
rtc.begin();
}
uint32_t old_ts;
void loop()
{
DateTime now = rtc.now(); //get the current date-time
uint32_t ts = now.getEpoch();
if (old_ts == 0 || old_ts != ts) {
old_ts = ts;
if (now.hour() == 21 && now.minute() == 1){
sing(1);
}
}
}
int song = 0;
void sing(int s) {
// iterate over the notes of the melody:
song = s;
if (song == 1) {
int size = sizeof(melody) / sizeof(int);
for (int thisNote = 0; thisNote < size; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / tempo[thisNote];
buzz(melodyPin, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
buzz(melodyPin, 0, noteDuration);
}
}
}
void buzz(int targetPin, long frequency, long length) {
digitalWrite(13, HIGH);
long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait again or the calculated delay value
}
digitalWrite(13, LOW);
}