arduino-sketches/door-sensor/melody.h

65 lines
1.4 KiB
C
Raw Normal View History

2025-04-10 10:50:54 -03:00
#include "pitches.h"
#include <TimerFreeTone.h>
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
int melody2[] = { 262, 196, 196, 220, 196, 0, 247, 262 };
2025-04-10 14:15:26 -03:00
int duration2[] = { 250, 125, 125, 250, 250, 250, 250, 250 };
int melody3[] = { 262, 196};
int duration3[] = { 250, 125};
2025-04-10 10:50:54 -03:00
#define TONE_PIN 8
void playTone(){
// digitalWrite(TONE_PIN, HIGH);
// delay(2000);
// digitalWrite(TONE_PIN, LOW);
// return;
for (int thisNote = 0; thisNote < 8; thisNote++) { // Loop through the notes in the array.
TimerFreeTone(TONE_PIN, melody2[thisNote], duration2[thisNote]); // Play thisNote for duration.
delay(50); // Short delay between notes.
}
2025-04-10 14:15:26 -03:00
}
float sinVal;
int toneVal;
void playSound(bool silent){
int max = 180;
if(silent)
max = 2;
for(int x = 0; x < max ; x++){
//convert angle of sinusoidal to radian measure
sinVal = (sin(x*(3.1412/180)));
//generate sound of different frequencies by sinusoidal value
toneVal = 2000+(int(sinVal*1000));
//Set a frequency for Pin-out 8
tone(TONE_PIN, toneVal, 3);
// TimerFreeTone(TONE_PIN, toneVal, 2, 5);
delay(2);
2025-04-10 10:50:54 -03:00
}
noTone(TONE_PIN);
}
2025-04-10 10:50:54 -03:00
2025-04-10 14:15:26 -03:00
void playAlarm(bool silent){
playSound(silent);
delay(500);
}
2025-04-10 14:15:26 -03:00
2025-04-10 10:50:54 -03:00