-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheurorack-sequencer-software.ino
More file actions
221 lines (189 loc) · 5.85 KB
/
Copy patheurorack-sequencer-software.ino
File metadata and controls
221 lines (189 loc) · 5.85 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
/***
POLYKIT 8-Step Sequencer
https://polykit.rocks/sequencer
License: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
***/
#define DEBUG_MODE
// === Pin assignment ===
const uint8_t gateOutPin = 13; // Digital gate output
const uint8_t clockOutPin = 10; // Digital clock output
const uint8_t clockInPin = 11; // Digital clock input
const uint8_t resetInPin = 12; // Digital reset input
const uint8_t cvRatePin = A2; // CV for tempo/rate
const uint8_t cvGateLengthPin = A0; // CV for gate length
const uint8_t cvStepsPin = A1; // CV for step count
const uint8_t stepOutPins[8] = {3, 5, 6, 8, 2, 4, 7, 9};
// === Sequencer Settings ===
const uint8_t maxSteps = 8;
uint8_t currentStep = 0;
uint8_t stepCount = 8; // Dynamically set by CV
// === Timing ===
unsigned long stepInterval = 200; // ms between steps (tempo)
unsigned long minStepInterval = 50; // ms between steps (tempo)
unsigned long maxStepInterval = 3000; // ms between steps (tempo)
unsigned long gateOnTime = 50; // ms gate length
unsigned long minGateOnTime = 50; // ms min gate length
unsigned long clockOnTime = 10; // ms clock length
bool gateHigh = false;
unsigned long gateStartTime = 0;
unsigned long lastStepTime = 0;
unsigned long lastClockTime = 0;
// === Clock Input Tracking ===
bool lastClockState = LOW;
bool lastResetState = LOW;
// === Link mode ===
const uint8_t linkOutPin = A3;
const uint8_t linkInPin = A4;
unsigned long lastLinkTime = 0;
bool isLinked = false;
bool isFirst = true;
bool isLast = true;
bool isActive = true;
bool lastLinkInState = HIGH; // active low
void setup() {
#ifdef DEBUG_MODE
Serial.begin(115200);
Serial.println("Starting POLYKIT Sequencer ...");
#endif
pinMode(gateOutPin, OUTPUT);
pinMode(clockOutPin, OUTPUT);
pinMode(clockInPin, INPUT);
pinMode(resetInPin, INPUT);
digitalWrite(gateOutPin, LOW);
digitalWrite(clockOutPin, LOW);
// === Initialize step output pins ===
for (uint8_t i = 0; i < maxSteps; i++) {
pinMode(stepOutPins[i], OUTPUT);
digitalWrite(stepOutPins[i], LOW);
}
// === Link mode: detect additional units ===
pinMode(linkOutPin, OUTPUT);
digitalWrite(linkOutPin, LOW);
pinMode(linkInPin, INPUT_PULLUP);
lastLinkTime = millis();
while (millis() - lastLinkTime < 1000) {
if (digitalRead(linkInPin) == LOW) {
isLinked = true;
isFirst = false;
}
}
digitalWrite(linkOutPin, HIGH);
delay(1000);
pinMode(linkOutPin, INPUT_PULLUP);
pinMode(linkInPin, OUTPUT);
digitalWrite(linkInPin, LOW);
delay(1000);
lastLinkTime = millis();
while (millis() - lastLinkTime < 1000) {
if (digitalRead(linkOutPin) == LOW) {
if (isLinked) {
isLast = false;
}
isLinked = true;
}
}
digitalWrite(linkInPin, HIGH);
pinMode(linkOutPin, OUTPUT);
digitalWrite(linkOutPin, HIGH);
pinMode(linkInPin, INPUT_PULLUP);
#ifdef DEBUG_MODE
if (isLinked) {
Serial.println("Link mode detected.");
if (isFirst) Serial.println("Unit is first device in chain.");
if (isLast) Serial.println("Unit is last device in chain.");
if (!isFirst && !isLast) Serial.println("Unit is middle device in chain.");
} else {
Serial.println("Link mode NOT detected.");
}
#endif
if (!isFirst) {
isActive = false;
}
}
void loop() {
// === Read CVs ===
int rateCV = analogRead(cvRatePin); // 0 - 1023
int gateCV = analogRead(cvGateLengthPin); // 0 - 1023
int stepsCV = analogRead(cvStepsPin); // 0 - 1023
// === Map CV to useful values ===
stepInterval = map(rateCV, 1023, 0, minStepInterval, maxStepInterval);
gateOnTime = map(gateCV, 0, 1023, minStepInterval, stepInterval);
stepCount = map(stepsCV, 0, 1023, 1, maxSteps);
// === Clock Input Handling ===
bool clockState =
digitalRead(clockInPin); // is internally wired to clock out pin
if (clockState == HIGH && lastClockState == LOW) {
if (isActive) {
advanceStep();
} else {
disableOutputs();
}
// pass active state
if (currentStep == 0 && isLinked && isActive) {
if (isLast) {
// send reset to first device
pinMode(resetInPin, OUTPUT);
digitalWrite(resetInPin, HIGH);
delay(50);
digitalWrite(resetInPin, LOW);
pinMode(resetInPin, INPUT);
} else {
// pass to next device
digitalWrite(linkOutPin, LOW);
delay(50);
digitalWrite(linkOutPin, HIGH);
}
isActive = false;
}
}
lastClockState = clockState;
// === Handle Reset Input ===
bool resetState = digitalRead(resetInPin);
if (resetState == HIGH && lastResetState == LOW) {
if (isLinked && isFirst) {
isActive = true;
}
currentStep = 0;
}
lastResetState = resetState;
// === Clock Output Handling ===
if (millis() - lastClockTime >= stepInterval) {
digitalWrite(clockOutPin, HIGH);
lastClockTime = millis();
}
// === Handle Clock Duration ===
if (millis() - lastClockTime >= clockOnTime) {
digitalWrite(clockOutPin, LOW);
}
// === Handle Gate Duration ===
if (gateHigh && (millis() - gateStartTime >= gateOnTime)) {
digitalWrite(gateOutPin, LOW);
gateHigh = false;
}
// === Receive active state from link in ===
bool linkInState = digitalRead(linkInPin);
if (!isActive && linkInState == LOW && lastLinkInState == HIGH) {
isActive = true;
}
lastLinkInState = linkInState;
}
void advanceStep() {
lastStepTime = millis();
// Update step outputs
updateStepOutputs();
// Trigger gate output
digitalWrite(gateOutPin, HIGH);
gateStartTime = millis();
gateHigh = true;
currentStep = (currentStep + 1) % stepCount;
}
void updateStepOutputs() {
for (uint8_t i = 0; i < maxSteps; i++) {
digitalWrite(stepOutPins[i], (i == currentStep) ? HIGH : LOW);
}
}
void disableOutputs() {
for (uint8_t i = 0; i < maxSteps; i++) {
digitalWrite(stepOutPins[i], LOW);
}
}