-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttiny85_MIDICV.ino
More file actions
264 lines (208 loc) · 6.1 KB
/
Copy pathAttiny85_MIDICV.ino
File metadata and controls
264 lines (208 loc) · 6.1 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Code based on the "ATtiny85 MIDI to CV" project by Kevin
// https://emalliab.wordpress.com/2019/03/02/attiny85-midi-to-cv/
// Authors: Paolo Estorm, Kevin
// Version 3.51
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Add this to the additional boards in Preferences:
// http://drazzy.com/package_drazzy.com_index.json
// intall "ATTinyCore" in boards manager
// set Board to "ATiny25/45/85 (No bootloader)"
// set Port to your arduino programmer
// set Chip to "ATtiny85"
// set Clock Source to "8 Mhz (internal)"
// set millis()/micros() to "Enabled"
// set Timer 1 Clock to "64Mhz"
// set programmer to "Arduino as ISP"
// burn the bootloader ONLY THE FIRST TIME
// upload using the Upload button
#define USE_MIDI_INDICATOR
#include <SoftwareSerial.h>
#define MIDICH 1 // Set this to the MIDI channel to listen to (1 to 16)
#define MIDIRX 0 // PB0 (Pin 5) MIDI in
#define GATE 2 // PB2 (Pin 7) Gate
#define PITCHCV 1 // PB1 (Pin 6) Pitch CV
#if defined(USE_MIDI_INDICATOR)
#define MIDILED 3 // PB3 (Pin 2) MIDI Indicator
bool BlinkMidiLed = false;
#endif
SoftwareSerial midiSerial(MIDIRX, -1);
uint8_t MIDIRunningStatus = 0;
uint8_t MIDINote = 0;
uint8_t MIDILevel = 0;
bool SameChannel = false;
const uint8_t NoteBuffersSize = 8;
int8_t NoteBuffer[NoteBuffersSize];
int8_t NumKeyPressed = 0;
bool Sustain = false;
void setup() {
midiSerial.begin(31250); // MIDI Baud rate
pinMode(GATE, OUTPUT);
pinMode(PITCHCV, OUTPUT);
#if defined(USE_MIDI_INDICATOR)
pinMode(MIDILED, OUTPUT); // Enable midi led output pin
digitalWrite(MIDILED, LOW);
#endif
TCCR1 = _BV(PWM1A) | _BV(COM1A1) | _BV(CS10);
GTCCR = 0;
OCR1C = 127;
OCR1A = 0; // Initial Pitch CV = 0
GateOff();
}
void SetTimerPWM(uint8_t note) {
OCR1A = note;
}
void GateOn() {
digitalWrite(GATE, HIGH);
}
void GateOff() {
digitalWrite(GATE, LOW);
}
void loop() {
if (midiSerial.available()) {
doMIDI(midiSerial.read());
}
#if defined(USE_MIDI_INDICATOR)
LedOnTimer();
#endif
}
void doMIDI(uint8_t midibyte) {
#if defined(USE_MIDI_INDICATOR)
BlinkMidiLed = true;
#endif
if ((midibyte >= 0x80) && (midibyte <= 0xEF)) { //if it's a status message
//
// MIDI Voice category message
//
// Start handling the RunningStatus
if ((midibyte & 0x0F) == (MIDICH - 1)) {
// Store, but remove channel information now we know its for us
MIDIRunningStatus = midibyte & 0xF0;
SameChannel = true;
MIDINote = 0;
MIDILevel = 0;
} else {
SameChannel = false;
// Not on our channel, so ignore
}
}
else if (midibyte <= 0x7F && SameChannel) {
if (MIDIRunningStatus == 0x80) { // NoteOff
// First find the note
if (MIDINote == 0) {
MIDINote = midibyte;
} else {
// If we already have a note, assume its the level
MIDILevel = midibyte;
HandleNoteOff(MIDINote);
MIDINote = 0;
MIDILevel = 0;
}
}
else if (MIDIRunningStatus == 0x90) { // NoteOn
if (MIDINote == 0) {
MIDINote = midibyte;
}
else {
// If we already have a note, assume its the level
MIDILevel = midibyte;
// Now we have a note/velocity pair, act on it
if (MIDILevel == 0) {
HandleNoteOff(MIDINote);
}
else {
HandleNoteOn(MIDINote);
}
MIDINote = 0;
MIDILevel = 0;
}
}
else if (MIDIRunningStatus == 0xB0) { // CC
// First find the note
if (MIDINote == 0) {
MIDINote = midibyte;
}
else {
// If we already have a note, assume its the level
MIDILevel = midibyte;
// Now we have a note/velocity pair, act on it
HandleCC(MIDINote, MIDILevel);
MIDINote = 0;
MIDILevel = 0;
}
}
}
}
void LedOnTimer() {
#if defined(USE_MIDI_INDICATOR)
static unsigned long SampleTime = 0; // Time at which the timer was set
static bool TimerState = false; // Is the timer still running?
// Start the timer
if (BlinkMidiLed) {
BlinkMidiLed = false;
TimerState = true;
SampleTime = millis();
digitalWrite(MIDILED, HIGH);
}
// If the timer is running, check if it has expired
if (TimerState && millis() - SampleTime > 100) {
TimerState = false;
digitalWrite(MIDILED, LOW);
}
#endif
}
void ResetSynth() {
NumKeyPressed = 0;
GateOff();
Sustain = false;
for (uint8_t i = 0; i < NoteBuffersSize; i++) { // shift the -1s to the left of the "activeNotes" array
NoteBuffer[i] = -1;
}
}
void HandleCC(uint8_t cc, uint8_t value) {
if (cc == 64) { // sustain pedal cc
if (value > 0) Sustain = true;
else {
Sustain = false;
if (NumKeyPressed <= 0) GateOff();
}
}
if (cc == 123) ResetSynth();
}
void HandleNoteOn(uint8_t note) {
NumKeyPressed++;
if (NumKeyPressed > NoteBuffersSize) NumKeyPressed = NoteBuffersSize;
if (NumKeyPressed > 0) NoteBuffer[NumKeyPressed - 1] = note; // store in buffer
SetTimerPWM(note);
GateOn();
}
void HandleNoteOff(uint8_t note) {
for (uint8_t j = 0; j < NoteBuffersSize; j++) { // remove from buffer
for (uint8_t i = 0; i < NoteBuffersSize; i++) {
if (NoteBuffer[i] == note) {
NoteBuffer[i] = -1;
SortArray();
NumKeyPressed--;
if (NumKeyPressed < 0) NumKeyPressed = 0;
}
}
}
if (NumKeyPressed > 0) SetTimerPWM(NoteBuffer[NumKeyPressed - 1]);
else {
if (!Sustain) GateOff();
}
}
void SortArray() { // sort "NoteBuffer" array
for (uint8_t i = 0; i < NoteBuffersSize; i++) { // shift the -1s to the left of the "activeNotes" array
if (NoteBuffer[i] == -1) {
NoteBuffer[i] = NoteBuffer[i + 1];
NoteBuffer[i + 1] = -1;
}
}
}