Skip to content

Commit 0b3ad27

Browse files
author
katspaugh
committed
dual
1 parent 6d257ec commit 0b3ad27

File tree

1 file changed

+55
-50
lines changed

1 file changed

+55
-50
lines changed

arduino-euclid/arduino-euclid.ino

+55-50
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,64 @@
66
#define INVERTED_OUT_PIN 10
77
#define RESET_PIN A7
88
#define LONG_PRESS 300
9-
#define CLOCK_PULSES 1
9+
#define SHORT_PRESS 10
1010
#define MAX_STEPS 8
1111

12-
int leds[] = {
13-
3,
14-
4,
15-
5,
16-
6,
17-
8,
18-
9,
19-
7,
20-
2
21-
};
22-
23-
int active_steps = 1;
12+
int leds[] = { 2, 7, 9, 8, 6, 5, 4, 3 };
13+
14+
bool alt_positions = false;
15+
int active_steps1 = 1;
16+
int active_steps2 = 1;
2417
int offset = 0;
25-
int clock_counter = 0;
26-
int counter = 0;
18+
int counter = -1;
2719
int steps = MAX_STEPS;
28-
bool positions[MAX_STEPS] = {};
20+
bool positions1[MAX_STEPS] = {};
21+
bool positions2[MAX_STEPS] = {};
2922

3023
bool send_tick = false;
3124
bool clock_state = false;
25+
bool last_clock_state = false;
3226
bool last_button_state = false;
3327
bool last_reset_state = false;
3428
int button_pressed_time = 0;
3529

36-
bool getPosition(uint8_t index) {
37-
return positions[(index + offset) % steps];
30+
bool getPosition(uint8_t index, bool positions[MAX_STEPS]) {
31+
int step_idx = (index + offset) % steps;
32+
return positions[step_idx];
3833
}
3934

4035
void checkButton() {
4136
bool button_state = digitalRead(BUTTON_PIN) == LOW;
4237
if (last_button_state == button_state) return;
38+
last_button_state = button_state;
39+
40+
int now = millis();
4341

44-
// On button up
45-
if (last_button_state) {
46-
int press_time = millis() - button_pressed_time;
42+
if (button_state) {
43+
button_pressed_time = now;
44+
} else {
45+
int press_duration = now - button_pressed_time;
46+
47+
if (press_duration < SHORT_PRESS) return;
4748

48-
// Change the offset on long press
49-
if (press_time >= LONG_PRESS) {
50-
offset += 1;
51-
if (offset >= steps) offset = 0;
52-
// or change the number of active steps
49+
if (press_duration >= LONG_PRESS) {
50+
alt_positions = !alt_positions;
5351
} else {
54-
active_steps += 1;
55-
if (active_steps > steps) active_steps = 1;
56-
EEPROM.write(0, active_steps);
52+
if (alt_positions) {
53+
active_steps2 += 1;
54+
if (active_steps2 > steps) active_steps2 = 1;
55+
setPositions(active_steps2, positions2);
56+
EEPROM.write(1, active_steps2);
57+
} else {
58+
active_steps1 += 1;
59+
if (active_steps1 > steps) active_steps1 = 1;
60+
setPositions(active_steps1, positions1);
61+
EEPROM.write(0, active_steps1);
62+
}
5763
}
5864

59-
setPositions();
6065
setActiveLeds();
61-
// On button down
62-
} else {
63-
button_pressed_time = millis();
6466
}
65-
66-
last_button_state = button_state;
6767
}
6868

6969
void checkReset() {
@@ -73,7 +73,7 @@ void checkReset() {
7373
if (reset) counter = 0;
7474
}
7575

76-
void setPositions() {
76+
void setPositions(int active_steps, bool positions[MAX_STEPS]) {
7777
for (int i = 0; i < steps; i++) {
7878
positions[i] = false;
7979
}
@@ -112,18 +112,21 @@ void setLed(int index, bool active) {
112112

113113
void setActiveLeds() {
114114
for (int i = 0; i < steps; i++) {
115-
setLed(i, getPosition(i));
115+
setLed(i, getPosition(i, alt_positions ? positions2 : positions1));
116116
}
117117
}
118118

119119
void onClockOn() {
120-
bool is_active = getPosition(counter);
120+
counter += 1;
121+
if (counter >= steps) counter = 0;
121122

122-
digitalWrite(OUT_PIN, is_active ? HIGH : LOW);
123-
digitalWrite(INVERTED_OUT_PIN, is_active ? LOW : HIGH);
124-
setLed(counter, !is_active);
123+
bool is_active1 = getPosition(counter, positions1);
124+
digitalWrite(OUT_PIN, is_active1);
125125

126-
counter = (counter + 1) % steps;
126+
bool is_active2 = getPosition(counter, positions2);
127+
digitalWrite(INVERTED_OUT_PIN, is_active2);
128+
129+
setLed(counter, alt_positions ? !is_active2 : !is_active1);
127130
}
128131

129132
void onClockOff() {
@@ -139,10 +142,7 @@ void pciSetup(byte pin) {
139142
}
140143

141144
ISR(PCINT0_vect) {
142-
clock_state = !clock_state;
143-
if (clock_counter == 0) send_tick = true;
144-
clock_counter += 1;
145-
if (clock_counter >= CLOCK_PULSES) clock_counter = 0;
145+
send_tick = true;
146146
}
147147

148148
void setup() {
@@ -151,16 +151,18 @@ void setup() {
151151
pinMode(pin, OUTPUT);
152152
}
153153

154-
pinMode(CLOCK_PIN, INPUT_PULLUP);
154+
pinMode(CLOCK_PIN, INPUT);
155155
pinMode(BUTTON_PIN, INPUT_PULLUP);
156156
pinMode(OUT_PIN, OUTPUT);
157157
pinMode(INVERTED_OUT_PIN, OUTPUT);
158158

159159
pciSetup(CLOCK_PIN);
160160

161-
active_steps = EEPROM.read(0);
161+
active_steps1 = EEPROM.read(0);
162+
active_steps2 = EEPROM.read(1);
162163

163-
setPositions();
164+
setPositions(active_steps1, positions1);
165+
setPositions(active_steps2, positions2);
164166
setActiveLeds();
165167
}
166168

@@ -169,8 +171,11 @@ void loop() {
169171
checkReset();
170172

171173
if (!send_tick) return;
172-
173174
send_tick = false;
174175

176+
clock_state = digitalRead(CLOCK_PIN);
177+
if (clock_state == last_clock_state) return;
178+
last_clock_state = clock_state;
179+
175180
clock_state ? onClockOn() : onClockOff();
176181
}

0 commit comments

Comments
 (0)