-
Notifications
You must be signed in to change notification settings - Fork 0
/
MIDI_ch340.ino
41 lines (34 loc) · 1.03 KB
/
MIDI_ch340.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
/*
MIDI message:
https://www.midi.org/specifications-old/item/table-2-expanded-messages-list-status-bytes
*/
void MIDImessage(byte status, byte data1, byte data2)
{
Serial.write(status);
Serial.write(data1);
Serial.write(data2);
}
// POTENTIOMETER //
const int pot1 = A0; // potentiometer pins
int potVal1 = 0;
int lastPotVal1 = 0;
void readPots() {
int diff = 2; // difference amount
// READ POTENTIOMETER//
potVal1 = analogRead(pot1);
// CALCULATE DIFFERENCE BETWEEN NEW VALUE AND LAST RECORDED VALUE //
int potVal1diff = potVal1 - lastPotVal1;
// SEND MIDI MESSAGE //
if (abs(potVal1diff) > diff) // execute only if new and old values differ enough
{
MIDImessage(176, 0, map(potVal1, 0, 1023, 0, 127)); // map sensor range to MIDI range
lastPotVal1 = potVal1; // reset old value with new reading
}
delay(5); // small delay in milliseconds further stabilizes sensor readings
}
void setup() {
Serial.begin(9600); // enable serial communication
}
void loop() {
readPots(); // read potentiometers
}