float c0 = 16.352; float d0 = 18.354; float e0 = 20.602; float f0 = 21.827; float g0 = 24.500; float a0 = 27.500; float b0 = 30.868; float b3 = b0 * 8; float c4 = c0 * 16; float d4 = d0 * 16; float e4 = e0 * 16; float f4 = f0 * 16; float g4 = g0 * 16; float a4 = a0 * 16; float b4 = b0 * 16; float b4f = (b4 + a4) / 2; // b4 Бемоль (Flat) float c5 = c0 * 32; float d5 = d0 * 32; float f5 = f0 * 32; float g5 = g0 * 32; float a5 = a0 * 32; float b5 = b0 * 32; float b5f = (b5 + a5) / 2; void setup() { pinMode(2, OUTPUT); } void play_tone(int port, float f, long t) { const long T = 1000000 / f; // Период одного колебания long d = T / 2; // Половина периода (Полупериод) int count = t / T; // Количество колебаний for (int k = 0; k < count; k = k + 1) { digitalWrite(port, HIGH); delayMicroseconds(d); digitalWrite(port, LOW); delayMicroseconds(d); } } float melody[89][2] = { // 1 такт {g4, 8}, {g4, 8}, {g4, 8}, {g4, 8}, {g4, 8}, {g4, 8}, {g4, 8}, {g4, 8}, // 2 {g4, 8}, {g4, 8}, {g4, 8}, {g4, 8}, {g4, 8}, {g4, 8}, {g4, 8}, {g4, 8}, // 3 {b4f, 8}, {b4f, 8}, {b4f, 8}, {b4f, 8}, {b4f, 8}, {b4f, 8}, {b4f, 8}, {b4f, 8}, // 4 {b4f, 8}, {b4f, 8}, {b4f, 8}, {b4f, 8}, {b4f, 8}, {b4f, 8}, {b4f, 8}, {b4f, 8}, // 5 {b4f, 8}, {b4f, 8}, {b4f, 8}, {b4f, 8}, {d5, 8}, {d5, 8}, {d5, 8}, {d5, 8}, // 6 {c5, 8}, {c5, 8}, {c5, 8}, {c5, 8}, {f5, 8}, {f5, 8}, {f5, 8}, {f5, 8}, // 7 {g5, 8}, {g5, 8}, {g5, 8}, {g5, 8}, {g5, 8}, {g5, 8}, {g5, 8}, {g5, 8}, // 8 (64) {g5, 8}, {g5, 8}, {g5, 8}, {g5, 8}, {c5, 8}, {b4f, 8}, {a4, 8}, {f4, 8}, // 9 (69) {g4, 4}, {g4, 8}, {d5, 8}, {c5, 4}, {b4f, 4}, // 10 (75) {a4, 4}, {a4, 8}, {a4, 8}, {c5, 4}, {b4f, 8}, {a4, 8}, // 11 (82) {g4, 4}, {g4, 8}, {b5f, 8}, {a5, 8}, {b5f, 8}, {a5, 8}, {b5f, 8}, // 12 (89) {g4, 4}, {g4, 8}, {b5f, 8}, {a5, 8}, {b5f, 8}, {a5, 8}, {b5f, 8}, }; void loop() { const int BPM = 122; const long MINUTE = 60000000; const long TAKT = (MINUTE / BPM) * 4; for (int n = 0; n < 89; n = n + 1) { play_tone(2, melody[n][0], TAKT / melody[n][1] ); delay(10); } }