-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino-sound-programmer.ino
194 lines (184 loc) · 3.99 KB
/
arduino-sound-programmer.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <Wire.h>
#include <LCD_I2C.h>
#include "notes.h"
const static int pinUp PROGMEM = 4;
const static int pinLeft PROGMEM = 8;
const static int pinRight PROGMEM = 10;
const static int pinDown PROGMEM = 12;
const static int pinLengthUp PROGMEM = 11;
const static int pinLengthDown PROGMEM = 9;
const static int pinPlay PROGMEM = 6;
const static int pinSpeaker PROGMEM = 2;
const static int melodyLength PROGMEM = 100;
const static int standardNote PROGMEM = 0;
const static int standardLength PROGMEM = 4;
const static int maxLength PROGMEM = 16;
const static int pauseBetweenNotes PROGMEM = 50;
const static int BPM PROGMEM = 130;
const static int debounceTime PROGMEM = 200;
const static int debounceTimeNote PROGMEM = 100;
int selNote = 0;
typedef struct
{
int note;
int length;
} melody;
melody Melody[melodyLength + 1];
byte arrowLeft[8] = {
B00010,
B00110,
B01110,
B11110,
B01110,
B00110,
B00010,
B00000};
byte arrowRight[8] = {
B01000,
B01100,
B01110,
B01111,
B01110,
B01100,
B01000,
B00000};
LCD_I2C lcd(0x3F, 16, 2);
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
Serial.begin(250000);
Serial.println("Starting!");
pinMode(pinUp, INPUT);
pinMode(pinLeft, INPUT);
pinMode(pinRight, INPUT);
pinMode(pinDown, INPUT);
pinMode(pinLengthUp, INPUT);
pinMode(pinLengthDown, INPUT);
pinMode(pinPlay, INPUT);
for (int i = 0; i <= melodyLength; i++)
{
Melody[i].note = standardNote;
Melody[i].length = standardLength;
};
if (digitalRead(pinLengthDown) == HIGH)
{
for (int i = 0; i <= 90; i++)
{
Melody[i].note = i;
Melody[i].length = standardLength;
};
}
if (digitalRead(pinDown) == HIGH)
{
for (int i = 0; i <= 90; i++)
{
Melody[i].note = i;
Melody[i].length = i;
};
}
lcd.begin();
lcd.backlight();
lcd.createChar(0, arrowLeft);
lcd.createChar(1, arrowRight);
drawLCD();
}
void loop()
{
if (digitalRead(pinUp) == HIGH && Melody[selNote].note < 90)
{
Melody[selNote].note++;
drawLCD();
delay(debounceTimeNote);
}
else if (digitalRead(pinDown) == HIGH && Melody[selNote].note > 0)
{
Melody[selNote].note--;
drawLCD();
delay(debounceTimeNote);
}
else if (digitalRead(pinLeft) == HIGH && selNote > 0)
{
selNote--;
drawLCD();
delay(debounceTime);
}
else if (digitalRead(pinRight) == HIGH && selNote < melodyLength)
{
selNote = selNote + 1;
drawLCD();
delay(debounceTime);
}
else if (digitalRead(pinLengthUp) == HIGH && Melody[selNote].length < maxLength)
{
Melody[selNote].length++;
drawLCD();
delay(debounceTime);
}
else if (digitalRead(pinLengthDown) == HIGH && Melody[selNote].length > 0)
{
Melody[selNote].length--;
drawLCD();
delay(debounceTime);
}
else if (digitalRead(pinPlay) == HIGH)
{
playMelody();
drawLCD();
delay(debounceTime);
}
}
void drawLCD()
{
if (selNote > 0)
{
lcd.setCursor(0, 0);
lcd.write(byte(0));
lcd.setCursor(2, 0);
lcd.print(Notes[Melody[selNote - 1].note].name);
}
else
{
lcd.setCursor(0, 0);
lcd.print(F(" "));
}
lcd.setCursor(6, 0);
lcd.print(Notes[Melody[selNote].note].name);
if (selNote < melodyLength)
{
lcd.setCursor(16, 0);
lcd.write(byte(1));
lcd.setCursor(10, 0);
lcd.print(Notes[Melody[selNote + 1].note].name);
}
else
{
lcd.setCursor(10, 0);
lcd.print(F(" "));
}
lcd.setCursor(0, 1);
lcd.print(F("L:"));
lcd.print(Melody[selNote].length);
lcd.print(F(" "));
lcd.setCursor(5, 1);
lcd.print(selNote);
lcd.print(F("/"));
lcd.print(melodyLength);
lcd.print(F(" "));
lcd.setCursor(7, 0);
}
void playMelody()
{
for (int i = 0; i <= melodyLength; i++)
{
selNote = i;
if (Melody[selNote].note != 0)
{
drawLCD();
delay(pauseBetweenNotes);
tone(pinSpeaker, Notes[Melody[selNote].note].pitch);
delay(((60.0 / BPM) / Melody[selNote].length) * 1000);
noTone(pinSpeaker);
}
}
selNote = 0;
}