Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on May 24, 2020. It is now read-only.

Commit e3d5ff9

Browse files
separate code for play melodies
1 parent 6729e04 commit e3d5ff9

File tree

4 files changed

+55
-42
lines changed

4 files changed

+55
-42
lines changed

‎arduino_firmware/arduino_firmware.ino‎

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,12 @@
2020
#define SOUND_TOUCH_FREQ_HZ 800 // частота звукового сигнала после обнаружения касания
2121
#define SOUND_VIBR_FREQ_HZ 2500 // частота звукового сигнала после обнаружения вибрации
2222

23-
#include <TM74HC595Display.h>
2423
#include <SPI.h>
2524
#include <Ethernet.h>
2625

27-
// типы мелодий которые могут играть
28-
enum MELODY_TYPE {
29-
INIT_MELODY, // начальная мелодия при включении
30-
GAIN_10_MELODY, // каждые 10 очков
31-
GAIN_100_MELODY // каждые 100 очков
32-
};
26+
#include <TM74HC595Display.h>
27+
28+
#include "play_melody.h"
3329

3430
TM74HC595Display disp(SCLK_PIN, RCLK_PIN, DIO_PIN); // дисплей
3531
EthernetClient client; // клиент Ethernet
@@ -55,10 +51,10 @@ void setup() {
5551
pinMode(BLUE_LIGHT_PIN, OUTPUT);
5652
pinMode(SOUND_PIN, OUTPUT);
5753

58-
disp.clear(); // очистить дисплей
59-
play_melody(INIT_MELODY); // играть начальную мелодию
60-
digitalWrite(RED_LIGHT_PIN, HIGH); // включить красный светодиод
61-
disp.digit4(0); // вывести 0 на дисплей
54+
disp.clear(); // очистить дисплей
55+
play_melody(INIT_MELODY, SOUND_PIN); // играть начальную мелодию
56+
digitalWrite(RED_LIGHT_PIN, HIGH); // включить красный светодиод
57+
disp.digit4(0); // вывести 0 на дисплей
6258
}
6359

6460
void loop() {
@@ -106,10 +102,10 @@ void loop() {
106102

107103
// если число кратно 10 или 100 проиграть мелодию
108104
if (num_detected % 10 == 0 && num_detected != 0 && num_detected % 100 != 0) {
109-
play_melody(GAIN_10_MELODY);
105+
play_melody(GAIN_10_MELODY, SOUND_PIN);
110106
}
111107
else if (num_detected % 100 == 0 && num_detected != 0) {
112-
play_melody(GAIN_100_MELODY);
108+
play_melody(GAIN_100_MELODY, SOUND_PIN);
113109
}
114110
}
115111
}
@@ -228,35 +224,6 @@ void check_display() {
228224
}
229225
}
230226

231-
void play_melody(MELODY_TYPE melody_type) {
232-
#include "melody.h"
233-
234-
switch(melody_type) {
235-
case INIT_MELODY:
236-
play_one_melody(init_melody, init_note_durations, init_melody_duration);
237-
break;
238-
239-
case GAIN_10_MELODY:
240-
play_one_melody(gain_10_melody, gain_10_note_durations, gain_10_melody_duration);
241-
break;
242-
243-
case GAIN_100_MELODY:
244-
play_one_melody(gain_100_melody, gain_100_note_durations, gain_100_melody_duration);
245-
break;
246-
}
247-
}
248-
249-
void play_one_melody(const uint16_t melody[], const uint8_t note_durations[], const uint8_t num_notes) {
250-
for (uint8_t current_note = 0; current_note < num_notes; current_note++) {
251-
uint16_t note_duration = 1000 / note_durations[current_note];
252-
uint16_t pause_between_notes = note_duration * 1.20;
253-
254-
tone(SOUND_PIN, melody[current_note], note_duration);
255-
delay(pause_between_notes);
256-
noTone(SOUND_PIN);
257-
}
258-
}
259-
260227
// посчитать кол-во цифр в числе
261228
// необходимо для подсчёта Content-Length в запросе
262229
uint8_t count_digits_uint(uint16_t val) {
File renamed without changes.

‎arduino_firmware/play_melody.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <Arduino.h>
2+
3+
#include "play_melody.h"
4+
5+
void play_melody(MELODY_TYPE melody_type, const uint8_t sound_pin) {
6+
#include "melodies.h"
7+
8+
switch(melody_type) {
9+
case INIT_MELODY:
10+
play_one_melody(init_melody, init_note_durations, init_melody_duration, sound_pin);
11+
break;
12+
13+
case GAIN_10_MELODY:
14+
play_one_melody(gain_10_melody, gain_10_note_durations, gain_10_melody_duration, sound_pin);
15+
break;
16+
17+
case GAIN_100_MELODY:
18+
play_one_melody(gain_100_melody, gain_100_note_durations, gain_100_melody_duration, sound_pin);
19+
break;
20+
}
21+
}
22+
23+
void play_one_melody(const uint16_t melody[], const uint8_t note_durations[], const uint8_t num_notes, const uint8_t sound_pin) {
24+
for (uint8_t current_note = 0; current_note < num_notes; current_note++) {
25+
uint16_t note_duration = 1000 / note_durations[current_note];
26+
uint16_t pause_between_notes = note_duration * 1.20;
27+
28+
tone(sound_pin, melody[current_note], note_duration);
29+
delay(pause_between_notes);
30+
noTone(sound_pin);
31+
}
32+
}

‎arduino_firmware/play_melody.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <Arduino.h>
4+
5+
// типы мелодий которые могут играть
6+
enum MELODY_TYPE {
7+
INIT_MELODY, // начальная мелодия при включении
8+
GAIN_10_MELODY, // каждые 10 очков
9+
GAIN_100_MELODY // каждые 100 очков
10+
};
11+
12+
void play_melody(MELODY_TYPE melody_type, const uint8_t sound_pin);
13+
14+
void play_one_melody(const uint16_t melody[], const uint8_t note_durations[], const uint8_t num_notes, const uint8_t sound_pin);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /