Skip to content

Commit b95f4e9

Browse files
committed
added tasknote example
1 parent 80e1edc commit b95f4e9

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

examples/TaskTone/TaskTone.ino

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <Taskfun.h>
2+
3+
#define PIN 3
4+
5+
const char* _melody[] = {
6+
"4G4", "4F4", "4G4", "4D4", "8B3", "4D4", "2G3",
7+
"4G4", "4F4", "4G4", "4D4", "8B3", "4D4", "2G3",
8+
"4G4", "4A4", "4B4", "8A4", "4B4", "4B4", "8G4", "4A4", "8G4", "4A4", "4A4", "8F4", "4G4", "8F4", "4G4", "4G4", "8E4", "2G4"
9+
};
10+
const int _melodyLength = sizeof(_melody) / sizeof(_melody[0]);
11+
12+
// Frequency table for notes (in Hz)
13+
const int _freq[9][7] = {
14+
{ 27, 31, 16, 18, 21, 22, 25 }, // A0 - G1
15+
{ 55, 62, 33, 37, 41, 44, 49 }, // A1 - G2
16+
{ 110, 123, 65, 73, 82, 87, 98 }, // A2 - G3
17+
{ 220, 247, 131, 147, 165, 175, 196 }, // A3 - G4
18+
{ 440, 494, 261, 294, 330, 349, 392 }, // A4 - G5
19+
{ 880, 988, 523, 587, 659, 698, 784 }, // A5 - G6
20+
{ 1760, 1976, 1047, 1174, 1319, 1397, 1568 }, // A6 - G7
21+
};
22+
23+
SyncVar<int> _note(0);
24+
25+
int getFreq(const char* note) {
26+
return _freq[(note[2] - '0')][note[1] - 'A'];
27+
}
28+
29+
int getDuration(const char* note) {
30+
return 800 / (note[0] - '0');
31+
}
32+
33+
void playNote(char note) {
34+
while (1) {
35+
if (_melody[_note][1] == note) {
36+
tone(PIN, getFreq(_melody[_note]));
37+
delay(getDuration(_melody[_note]));
38+
noTone(PIN);
39+
delay(20);
40+
_note = (_note + 1) % _melodyLength;
41+
}
42+
}
43+
}
44+
45+
void setup() {
46+
pinMode(LED_BUILTIN, OUTPUT);
47+
pinMode(PIN, OUTPUT);
48+
setupTasks('G' - 'A' + 1);
49+
50+
noInterrupts();
51+
for (auto i = 'A'; i <= 'G'; i++) {
52+
runTask(playNote, i);
53+
}
54+
interrupts();
55+
}
56+
57+
void loop() {
58+
digitalWrite(LED_BUILTIN, HIGH);
59+
delay(1000);
60+
digitalWrite(LED_BUILTIN, LOW);
61+
delay(1000);
62+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Taskfun
2-
version=0.1.1
2+
version=0.1.2
33
author=Eugene Pistrak
44
maintainer=Eugene Pistrak
55
sentence=Preemptive multitasking for Arduino AVR and SAMD21

0 commit comments

Comments
 (0)