-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiny-tune.ino
63 lines (51 loc) · 1.94 KB
/
tiny-tune.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Copyright (c) 2019 Jonathan Schneibel
MIT License
GitHub: https://github.com/jschneibel/tiny-tune
This is a sample program that plays a tune on an Arduino. The program has been optimized to consume
as little SRAM (variable memory) as possible for long melodies. Furthermore, other code can
be executed while the tune is played, since no delays are used to play the tunes.
In order to run the program, compile and upload the following files to your Arduino:
- tiny-tune.ino
- tunes.ino
- pitches.h
- libraries/ArduinoThread (library by Ivan Seidel)
Use playTune() to start playing the tune.
Use cancelTune() to stop playing the tune.
Edit getTuneData() in tunes.ino to change the sample tune or add your own tunes.
*/
#include <Thread.h>
#include <ThreadController.h>
// global variables used for playing tunes
#define BUZZER_PIN 2
#define UPDATE_INTERVAL 1000
int currentNote = 0; // buffer variable for notes (only one note gets loaded into SRAM at a time)
byte currentNoteIndex = 0;
byte maxNoteIndex = 1;
int currentNoteDuration = 100; // buffer variable for note durations (only one note duration gets loaded into SRAM at a time)
int pauseBetweenNotes = 100;
bool playPauseNext = false;
// Threads are used to play the tunes.
ThreadController threadController = ThreadController();
Thread tuneThread = Thread();
// Callback function for tuneThread.
void tuneCallback() {
playCurrentNote();
}
void setup() {
// Configure threads.
tuneThread.onRun(tuneCallback);
tuneThread.setInterval(100);
tuneThread.enabled = false; // no tune playing on setup
threadController.add(&tuneThread);
// Start playing the tune in tunes.ino. The rest of the code will keep executing.
playTune();
// Additional code here.
}
void loop() {
noInterrupts();
// Run threads in threadController, in case their set intervals have passed.
threadController.run();
interrupts();
// Additional code here.
}