-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfax-redes3_EN.ino
More file actions
117 lines (99 loc) · 2.79 KB
/
Copy pathfax-redes3_EN.ino
File metadata and controls
117 lines (99 loc) · 2.79 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const int buttonPins[] = {2,3,4,5,6,7,8,9,10,11,12}; // key buttons (12 is unused)
const int controlPins[] = {A0,A1,A2,A3,A4,A5}; // przyciski opcji (A0 is unused)
const int speakerPin = 13; // speaker
int volume = 128;
int scaleOffset = 0;
int baseFreqs[] = {261,293,329,349,392,440,494,523,587,659,698}; // C-major
bool lastControlStates[6];
int introNotes[] = {
392, 440, 494, 440,
392, 349, 330, 349
};
int introDurations[] = {
220, 180, 180, 220,
220, 180, 180, 260
};
//wstepne granie
void playIntro() {
for (int i = 0; i < 8; i++) {
tone(speakerPin, introNotes[i], introDurations[i]);
delay(introDurations[i] + 30);
noTone(speakerPin);
}
}
// przygotowanie urządzenia
void setup() {
Serial.begin(9600);
Serial.println("KEYBOARD ACTIVATED");
for (int i = 0; i < 11; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
for (int i = 0; i < 6; i++) {
pinMode(controlPins[i], INPUT_PULLUP);
lastControlStates[i] = HIGH;
}
pinMode(speakerPin, OUTPUT);
noTone(speakerPin);
playIntro();
}
// przyciski opcji
void readControls() {
for (int i = 0; i < 6; i++) {
int state = digitalRead(controlPins[i]);
if (state == LOW && lastControlStates[i] == HIGH) {
if (i == 1) volume = min(255, volume + 10); // A1 volume up
if (i == 2) volume = max(0, volume - 10); // A2 volume down
if (i == 3) scaleOffset = min(3, scaleOffset + 1); // A3 higher pitch
if (i == 4) scaleOffset = max(-2, scaleOffset - 1); // A4 lower pitch
if (i == 5) { // A5 reset
volume = 128;
scaleOffset = 0;
Serial.println("\n*** RESET DO DEFAULT ***");
}
delay(10);
}
if (state == HIGH) {
lastControlStates[i] = HIGH;
} else {
lastControlStates[i] = LOW;
}
}
}
void loop() {
bool anyKeyPlaying = false;
int newFreq = 0;
readControls();
// checking key states
for (int i = 0; i < 11; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
if (scaleOffset >= 0) {
int shift = min(scaleOffset, 3);
newFreq = baseFreqs[i] * (1 << shift);
} else {
int s = min(-scaleOffset, 2);
newFreq = baseFreqs[i] / (1 << s);
if (newFreq < 1) newFreq = baseFreqs[i];
}
anyKeyPlaying = true;
break;
}
}
// gra tylko jeśli faktycznie wciśnięto klawisz
if (anyKeyPlaying && newFreq > 0) {
tone(speakerPin, newFreq);
} else {
noTone(speakerPin);
}
// diagnostics
Serial.print("Volume:");
Serial.print(volume);
Serial.print(" Scale:");
Serial.print(scaleOffset);
Serial.print(" Keys:");
for (int i = 0; i < 11; i++) {
Serial.print(digitalRead(buttonPins[i]));
Serial.print(i == 10 ? "" : ",");
}
Serial.println();
delay(20);
}