Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
303 lines (250 loc) · 9.31 KB
/
Copy pathmain.cpp
File metadata and controls
303 lines (250 loc) · 9.31 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <Arduino.h>
#include <DigiMIDI.h>
/*
* Softpot Ribbon MIDI Controller for Digispark (ATtiny85)
* Maps analog ribbon position to MIDI notes across 3 octaves.
* Hysteresis prevents note jumping.
*
* SCALE MODES:
* - No Scale: All chromatic notes available
* - Major Scale: Major scale notes only (whole, whole, half, whole, whole, whole, half)
* - Minor Scale: Natural minor scale notes only (whole, half, whole, whole, half, whole, whole)
*
* Toggle scale mode by holding the bottom of the ribbon for 2 seconds.
* LED blinks to indicate scale mode changes.
*/
// Configuration - adjust these values to match your ribbon
const int SENSOR_PIN = 1; // P2 on Digispark (analog input)
const int LED_PIN = 1; // Onboard LED (P1) for visual feedback
// Analog reading range (you may need to calibrate)
const int MIN_RAW = 10; // Minimum usable analog value (ignore lower noise)
const int MAX_RAW = 1010; // Maximum usable analog value (ignore upper noise)
// MIDI settings
const int START_NOTE = 36; // C2 (MIDI note number)
const int NUM_NOTES = 36; // 3 octaves = 36 notes
const int VELOCITY = 100; // Fixed velocity (0-127)
const int MIDI_CHANNEL = 1; // MIDI channel 1
// Hysteresis threshold in raw ADC units to prevent note bouncing
const int HYSTERESIS = 20; // Adjust: larger = more stable, less sensitive
// Touch detection threshold: if raw < TOUCH_THRESHOLD, treat as no touch
const int TOUCH_THRESHOLD = MIN_RAW + 20;
// Debounce time (ms) between note changes to reduce spam
const unsigned long DEBOUNCE_MS = 20;
// Scale toggle settings
const unsigned long SCALE_TOGGLE_HOLD_MS = 2000; // Hold time to toggle scale mode
const int BOTTOM_ZONE_THRESHOLD = 50; // Raw value threshold for bottom zone detection
// Scale mode enum
enum ScaleMode {
NO_SCALE, // Chromatic - all notes available
WHITE_KEYS, // White keys only (C, D, E, F, G, A, B)
MAJOR_SCALE, // Major scale
MINOR_SCALE // Natural minor scale
};
// Scale interval patterns (in semitones from root)
// White keys: C, D, E, F, G, A, B = 0,2,4,5,7,9,11
const int WHITE_KEYS_INTERVALS[] = {0, 2, 4, 5, 7, 9, 11};
const int WHITE_KEYS_INTERVALS_COUNT = 7;
// Major: W-W-H-W-W-W-H = 0,2,4,5,7,9,11
const int MAJOR_INTERVALS[] = {0, 2, 4, 5, 7, 9, 11};
const int MAJOR_INTERVALS_COUNT = 7;
// Natural Minor: W-H-W-W-H-W-W = 0,2,3,5,7,8,10
const int MINOR_INTERVALS[] = {0, 2, 3, 5, 7, 8, 10};
const int MINOR_INTERVALS_COUNT = 7;
// Global variables
int currentNote = -1; // -1 means no note active
int currentRaw = 0;
unsigned long lastNoteChange = 0;
ScaleMode currentScaleMode = NO_SCALE; // Default to no scale (chromatic)
// Scale toggle state
unsigned long scaleHoldStart = 0;
bool scaleHoldActive = false;
// The DigiMIDI library provides a global instance 'DigiMIDI'
// (Alternatively you can create your own: DigiMIDIDevice midi;)
void setup() {
// Startup indicator: 3 fast blinks
pinMode(LED_PIN, OUTPUT);
for (int i = 0; i < 3; i++) {
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
}
delay(500);
// The DigiMIDI library initializes itself automatically.
// No further setup needed.
}
// Map raw ADC value (MIN_RAW..MAX_RAW) to note index (0..NUM_NOTES-1)
int rawToNoteIndex(int raw) {
// Constrain raw to usable range
if (raw < MIN_RAW) raw = MIN_RAW;
if (raw > MAX_RAW) raw = MAX_RAW;
// Linear mapping
long noteIndex = (long)(raw - MIN_RAW) * (NUM_NOTES - 1) / (MAX_RAW - MIN_RAW);
return (int)noteIndex;
}
// Convert note index to MIDI note number, applying scale if active
int noteIndexToMidi(int index) {
if (currentScaleMode == NO_SCALE) {
// Chromatic mode - all notes available
return START_NOTE + index;
}
// Scale mode - map index to scale notes only
// Determine intervals array
const int* intervals;
int intervalCount;
if (currentScaleMode == MAJOR_SCALE) {
intervals = MAJOR_INTERVALS;
intervalCount = MAJOR_INTERVALS_COUNT;
} else { // MINOR_SCALE
intervals = MINOR_INTERVALS;
intervalCount = MINOR_INTERVALS_COUNT;
}
// Calculate which octave and scale degree we're in
// Each octave has 7 scale degrees
int totalScaleDegrees = (NUM_NOTES * intervalCount) / 12; // Approximate scale degrees in range
int scaleDegreeIndex = (index * intervalCount) / 12; // Map chromatic index to scale degree
int octave = scaleDegreeIndex / intervalCount;
int degreeInOctave = scaleDegreeIndex % intervalCount;
// Calculate the MIDI note
int rootNote = START_NOTE + (octave * 12);
int midiNote = rootNote + intervals[degreeInOctave];
return midiNote;
}
// Alternative simpler scale mapping - maps ribbon position directly to scale notes
int noteIndexToMidiScaled(int index) {
if (currentScaleMode == NO_SCALE) {
// Chromatic mode - all notes available
return START_NOTE + index;
}
// For scale modes, we compress the ribbon range to only produce scale notes
// Calculate total semitones in our range
int totalSemitones = NUM_NOTES;
// Determine intervals array
const int* intervals;
int intervalCount;
if (currentScaleMode == WHITE_KEYS) {
intervals = WHITE_KEYS_INTERVALS;
intervalCount = WHITE_KEYS_INTERVALS_COUNT;
} else if (currentScaleMode == MAJOR_SCALE) {
intervals = MAJOR_INTERVALS;
intervalCount = MAJOR_INTERVALS_COUNT;
} else { // MINOR_SCALE
intervals = MINOR_INTERVALS;
intervalCount = MINOR_INTERVALS_COUNT;
}
// Calculate how many complete scales fit in our range
int semitonesPerScale = 12;
int scaleSteps = (totalSemitones * intervalCount) / semitonesPerScale;
// Map index to a scale step
int currentScaleStep = (index * scaleSteps) / NUM_NOTES;
// Determine which octave and which degree within the scale
int octave = currentScaleStep / intervalCount;
int degree = currentScaleStep % intervalCount;
// Calculate MIDI note number
int midiNote = START_NOTE + (octave * 12) + intervals[degree];
return midiNote;
}
// Determine if a raw change is significant enough to trigger a note change
bool shouldChangeNote(int newRaw, int oldRaw) {
return abs(newRaw - oldRaw) > HYSTERESIS;
}
// Handle scale mode toggle by holding bottom of ribbon
void handleScaleToggle(int newRaw, unsigned long now) {
// Ignore very low values (noise from air) - must be above 5
if (newRaw < 5) {
// Noise detected - reset any pending toggle
scaleHoldActive = false;
scaleHoldStart = 0;
return;
}
// Check if we're in the bottom zone
bool inBottomZone = (newRaw <= BOTTOM_ZONE_THRESHOLD);
if (inBottomZone) {
if (!scaleHoldActive) {
// Just entered bottom zone - start timing
scaleHoldActive = true;
scaleHoldStart = now;
} else {
// Already holding - check if hold time exceeded
if (now - scaleHoldStart >= SCALE_TOGGLE_HOLD_MS) {
// Toggle to next scale mode
if (currentScaleMode == NO_SCALE) {
currentScaleMode = MAJOR_SCALE;
} else if (currentScaleMode == MAJOR_SCALE) {
currentScaleMode = MINOR_SCALE;
} else {
currentScaleMode = NO_SCALE;
}
// Visual feedback - blink LED 2 times for mode change
for (int i = 0; i < 2; i++) {
digitalWrite(LED_PIN, HIGH);
delay(50);
digitalWrite(LED_PIN, LOW);
delay(50);
}
// Reset hold state
scaleHoldActive = false;
scaleHoldStart = 0;
}
}
} else {
// Not in bottom zone - reset hold state
scaleHoldActive = false;
scaleHoldStart = 0;
}
}
void updateNote(int newRaw) {
unsigned long now = millis();
// Handle scale toggle (bottom zone hold)
handleScaleToggle(newRaw, now);
// Debounce: ignore changes too quickly
if (now - lastNoteChange < DEBOUNCE_MS) {
return;
}
// Touch detection: if below threshold, treat as no touch
if (newRaw < TOUCH_THRESHOLD) {
// If a note is currently active, turn it off
if (currentNote >= 0) {
DigiMIDI.sendNoteOff(currentNote, VELOCITY, MIDI_CHANNEL);
digitalWrite(LED_PIN, LOW);
currentNote = -1;
lastNoteChange = now;
}
currentRaw = newRaw;
return;
}
// Touch detected above threshold
// Check if change is significant (hysteresis)
if (!shouldChangeNote(newRaw, currentRaw)) {
return;
}
// Map to note index
int newNoteIndex = rawToNoteIndex(newRaw);
int newMidiNote = noteIndexToMidiScaled(newNoteIndex);
// If note changed (or no note was active)
if (newMidiNote != currentNote) {
// Turn off previous note
if (currentNote >= 0) {
DigiMIDI.sendNoteOff(currentNote, VELOCITY, MIDI_CHANNEL);
digitalWrite(LED_PIN, LOW); // LED off when note off (optional)
}
// Turn on new note
DigiMIDI.sendNoteOn(newMidiNote, VELOCITY, MIDI_CHANNEL);
digitalWrite(LED_PIN, HIGH); // LED on when note on (optional)
// Update state
currentNote = newMidiNote;
lastNoteChange = now;
}
currentRaw = newRaw;
}
void loop() {
// Read analog value (0-1023)
int raw = analogRead(SENSOR_PIN);
// Update note if needed
updateNote(raw);
// Keep USB MIDI communication alive
DigiMIDI.update();
// Small delay to reduce CPU usage and ADC noise
// Adjust for desired latency (lower = faster response)
delay(5);
}