forked from eparadis/turing-machine-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turing-machine-clone.ino
152 lines (123 loc) · 3.97 KB
/
turing-machine-clone.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
// a loose clone of a clone of a clone
// original: MTM Turning Machine
// clone: 2hp TM
// clone: https://github.com/eparadis/turing-machine-clone
// clone: this thing
// inputs
#define POT1_PIN A0 // internal clock tempo
#define POT2_PIN A1 // machine 1 probability
#define POT3_PIN A2 // machine 1 steps
#define POT4_PIN A3 // machine 2 probability
#define POT5_PIN A4 // machine 2 steps
#define SWITCH1_PIN A5 // internal clock / trigger toggle
#define JACK1_PIN A6 // trigger input
// outputs
#define JACK2_PIN 5 // PD5 machine 1 pattern a
#define JACK3_PIN 6 // PD6 machine 1 pattern b
#define JACK4_PIN 9 // PB1 machine 2 pattern a
#define JACK5_PIN 10 // PB2 machine 2 pattern b
#define STEPS_MAX 32 // max number of steps
#define NOTES_MAX 12 // number of notes in the output voltage LUT
int pot1;
int pot2;
int pot3;
int pot4;
int pot5;
int switch1;
int jack1;
int jack2;
int jack3;
int jack4;
int jack5;
bool trigger = false;
int m1_pattern_a[STEPS_MAX];
int m1_pattern_b[STEPS_MAX];
int m2_pattern_a[STEPS_MAX];
int m2_pattern_b[STEPS_MAX];
int notes[NOTES_MAX];
byte m1_pattern_index = 0;
byte m2_pattern_index = 0;
void setup() {
pinMode(JACK2_PIN, OUTPUT);
pinMode(JACK3_PIN, OUTPUT);
pinMode(JACK4_PIN, OUTPUT);
pinMode(JACK5_PIN, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
// fill the notes lookup table with half-note steps
for (byte i = 0; i < NOTES_MAX; i++) {
notes[i] = (i * 1000.0) / 12.0;
}
// fill the pattern buffer with the indices of the note LUT
for (byte i = 0; i < STEPS_MAX; i++) {
m2_pattern_b[i] = m2_pattern_a[i] = m1_pattern_b[i] = m1_pattern_a[i] = i % NOTES_MAX;
}
}
void sampleAnalogInputs() {
pot1 = analogRead(POT1_PIN);
pot2 = analogRead(POT2_PIN);
pot3 = analogRead(POT3_PIN);
pot4 = analogRead(POT4_PIN);
pot5 = analogRead(POT5_PIN);
jack1 = analogRead(JACK1_PIN);
switch1 = analogRead(SWITCH1_PIN);
}
void updateLED() {
digitalWrite(LED_BUILTIN, trigger);
}
int voltageToPWM(int millivolts) {
return map(millivolts, 0, 5000, 0, 1023);
}
void loop() {
sampleAnalogInputs();
int m1_probability = map(pot2, 0, 1023, 0, 1000);
int m1_steps = map(pot3, 0, 1023, 1, STEPS_MAX);
int m2_probability = map(pot4, 0, 1023, 0, 1000);
int m2_steps = map(pot5, 0, 1023, 1, STEPS_MAX);
if (trigger && jack1 < 512) { // falling edge
trigger = false;
updateLED();
} else if (!trigger && jack1 >= 512) { // rising edge
trigger = true;
updateLED();
// if a random number is under our PROB threshold,
// change the current LUT index in the pattern
// leave a small gap at the bottom to have a solid 'lock' area
if (random(25, 1000) < m1_probability) {
m1_pattern_a[m1_pattern_index] = random(0, NOTES_MAX);
}
if (random(25, 1000) < m1_probability) {
m1_pattern_b[m1_pattern_index] = random(0, NOTES_MAX);
}
if (random(25, 1000) < m2_probability) {
m2_pattern_a[m2_pattern_index] = random(0, NOTES_MAX);
}
if (random(25, 1000) < m2_probability) {
m2_pattern_b[m2_pattern_index] = random(0, NOTES_MAX);
}
// pick the current value out of the pattern
// and look up in the note LUT what voltage to output
// m1 out
{
int out = notes[m1_pattern_a[m1_pattern_index]];
out = voltageToPWM(out);
analogWrite(JACK2_PIN, out);
out = notes[m1_pattern_b[m1_pattern_index]];
out = voltageToPWM(out);
analogWrite(JACK3_PIN, out);
// reset pattern index to zero when it reaches our step count
m1_pattern_index = (m1_pattern_index + 1) % m1_steps;
}
// m2 out
{
int out = notes[m2_pattern_a[m2_pattern_index]];
out = voltageToPWM(out);
analogWrite(JACK4_PIN, out);
out = notes[m2_pattern_b[m2_pattern_index]];
out = voltageToPWM(out);
analogWrite(JACK5_PIN, out);
// reset pattern index to zero when it reaches our step count
m2_pattern_index = (m2_pattern_index + 1) % m2_steps;
}
}
}