forked from diyelectromusic/sdemp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoMIDI7SegmentController.ino
199 lines (179 loc) · 6.33 KB
/
ArduinoMIDI7SegmentController.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
195
196
197
198
199
/*
// Simple DIY Electronic Music Projects
// diyelectromusic.wordpress.com
//
// Arduino MIDI 7 Segment Controller
// https://diyelectromusic.wordpress.com/2021/01/15/arduino-midi-7-segment-controller/
//
MIT License
Copyright (c) 2020 diyelectromusic (Kevin)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHERIN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
Using principles from the following Arduino tutorials:
Arduino MIDI Library - https://github.com/FortySevenEffects/arduino_midi_library
Arduino Button - https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
Arduino 7-Segment Tutorial - https://www.circuitbasics.com/arduino-7-segment-display-tutorial/
*/
#include <MIDI.h>
#include <SevSeg.h>
// Uncomment this to print to the Serial port instead of sending MIDI
//#define TEST 1
// This is required to set up the MIDI library.
// The default MIDI setup uses the Arduino built-in serial port
// which is pin 1 for transmitting on the Arduino Uno.
MIDI_CREATE_DEFAULT_INSTANCE();
#define MIDI_CHANNEL 1 // Define which MIDI channel to transmit on (1 to 16).
// Set up the LED display
SevSeg disp;
// Pin definitions for the display
//
// = A =
// F B
// = G =
// E C
// = D =
//
// Pattern repeated for three digits (1,2,3,4) each with a common cathode.
// Note: Cathode should be connected to the "digit" pins via a resistor (1k).
//
// Digit Order: D1-D2-D3-D4
//
// Pinout of my 12-pin display.
//
// D1--A--F-D2-D3--B
// | |
// E--D-DP--C--G-D4
//
#define NUM_DIGITS 4
#define NUM_SEGMENTS 8
byte digitPins[NUM_DIGITS] = {2,3,4,5}; // Order: D1,D2,D3,D4
byte segmentPins[NUM_SEGMENTS] = {6,7,8,9,10,11,12,13}; // Order: A,B,C,D,E,F,G,DP
#define RESISTORS_ON_SEGMENTS false // Resistors on segments (true) or digitals (false)
#define HARDWARE_CONFIG COMMON_CATHODE // COMMON_CATHODE or COMMON_ANODE (or others)
#define UPDATE_WITH_DELAYS false // false recommended apparently
#define LEADING_ZEROS true // Set true to show leading zeros
#define NO_DECIMAL_POINT false // Set true if no DP or not connected
#define BTN_UP A0
#define BTN_DOWN A1
#define MIN_PROG 1
#define MAX_PROG 128
int progNum;
int lastBtnUp;
int lastBtnDown;
// ------------------------------------------------------
// The display needs to be updated continually, which
// means that we can't use delay() anywhere in the code
// without interrupting the display.
//
// This function can be used as a replacement for the
// delay() function which will do the "waiting" whilst
// at the same time keeping the display updated.
//
// ------------------------------------------------------
void delayWithDisplay(unsigned long mS) {
unsigned long end_mS = mS + millis();
unsigned long time_mS = millis();
while (time_mS < end_mS) {
disp.refreshDisplay();
time_mS = millis();
}
}
void setup() {
disp.begin(HARDWARE_CONFIG, NUM_DIGITS, digitPins, segmentPins,
RESISTORS_ON_SEGMENTS, UPDATE_WITH_DELAYS, LEADING_ZEROS, NO_DECIMAL_POINT);
disp.setBrightness(90);
pinMode (BTN_UP, INPUT_PULLUP);
pinMode (BTN_DOWN, INPUT_PULLUP);
progNum = MIN_PROG;
lastBtnUp = HIGH;
lastBtnDown = HIGH;
#ifdef TEST
Serial.begin(9600);
#else
MIDI.begin(MIDI_CHANNEL);
#endif
updateDisplay();
}
void loop() {
// Note: Need to keep the display updated continually.
// This means we cannot use delay(), so have to use
// the replacement delayWithDisplay()
//
disp.refreshDisplay();
int btnUp = digitalRead(BTN_UP);
int btnDown = digitalRead(BTN_DOWN);
if ((btnUp == LOW) && (lastBtnUp == HIGH)) {
// Button has been pressed
progNum++;
if (progNum > MAX_PROG) progNum = MAX_PROG;
updateDisplay();
delayWithDisplay (300); // simple "manual" switch debouncing
}
if ((btnUp == LOW) && (lastBtnUp == LOW)) {
// Button is held down, so update more quickly
progNum++;
if (progNum > MAX_PROG) progNum = MAX_PROG;
updateDisplay();
delayWithDisplay (100);
}
if ((btnUp == HIGH) && (lastBtnUp == LOW)) {
// Button has been released
updateProgNum();
}
if ((btnDown == LOW) && (lastBtnDown == HIGH)) {
// Button has been pressed
progNum--;
if (progNum < MIN_PROG) progNum = MIN_PROG;
updateDisplay();
delayWithDisplay (300); // simple "manual" switch debouncing
}
if ((btnDown == LOW) && (lastBtnDown == LOW)) {
// Button is held down, so update more quickly
progNum--;
if (progNum < MIN_PROG) progNum = MIN_PROG;
updateDisplay();
delayWithDisplay (100);
}
if ((btnDown == HIGH) && (lastBtnDown == LOW)) {
// Button has been released
updateProgNum();
}
lastBtnUp = btnUp;
lastBtnDown = btnDown;
}
void updateDisplay () {
disp.setNumber(progNum,0);
}
void updateProgNum () {
#ifdef TEST
Serial.print ("Program Change: ");
Serial.println (progNum);
#else
// MIDI programs are defined as 1 to 128, but "on the wire"
// have to be converted to 0 to 127.
MIDI.sendProgramChange (progNum-1, MIDI_CHANNEL);
delayWithDisplay(500);
// Optional: play a note after the program change to confirm it worked
// playTestNote(60);
#endif
}
void playTestNote (byte note) {
MIDI.sendNoteOn(note, 127, MIDI_CHANNEL);
delayWithDisplay(100);
MIDI.sendNoteOff(note, 0, MIDI_CHANNEL);
delayWithDisplay(100);
}