6
6
#define INVERTED_OUT_PIN 10
7
7
#define RESET_PIN A7
8
8
#define LONG_PRESS 300
9
- #define CLOCK_PULSES 1
9
+ #define SHORT_PRESS 10
10
10
#define MAX_STEPS 8
11
11
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 ;
24
17
int offset = 0 ;
25
- int clock_counter = 0 ;
26
- int counter = 0 ;
18
+ int counter = -1 ;
27
19
int steps = MAX_STEPS;
28
- bool positions[MAX_STEPS] = {};
20
+ bool positions1[MAX_STEPS] = {};
21
+ bool positions2[MAX_STEPS] = {};
29
22
30
23
bool send_tick = false ;
31
24
bool clock_state = false ;
25
+ bool last_clock_state = false ;
32
26
bool last_button_state = false ;
33
27
bool last_reset_state = false ;
34
28
int button_pressed_time = 0 ;
35
29
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];
38
33
}
39
34
40
35
void checkButton () {
41
36
bool button_state = digitalRead (BUTTON_PIN) == LOW;
42
37
if (last_button_state == button_state) return ;
38
+ last_button_state = button_state;
39
+
40
+ int now = millis ();
43
41
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 ;
47
48
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;
53
51
} 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
+ }
57
63
}
58
64
59
- setPositions ();
60
65
setActiveLeds ();
61
- // On button down
62
- } else {
63
- button_pressed_time = millis ();
64
66
}
65
-
66
- last_button_state = button_state;
67
67
}
68
68
69
69
void checkReset () {
@@ -73,7 +73,7 @@ void checkReset() {
73
73
if (reset) counter = 0 ;
74
74
}
75
75
76
- void setPositions () {
76
+ void setPositions (int active_steps, bool positions[MAX_STEPS] ) {
77
77
for (int i = 0 ; i < steps; i++) {
78
78
positions[i] = false ;
79
79
}
@@ -112,18 +112,21 @@ void setLed(int index, bool active) {
112
112
113
113
void setActiveLeds () {
114
114
for (int i = 0 ; i < steps; i++) {
115
- setLed (i, getPosition (i));
115
+ setLed (i, getPosition (i, alt_positions ? positions2 : positions1 ));
116
116
}
117
117
}
118
118
119
119
void onClockOn () {
120
- bool is_active = getPosition (counter);
120
+ counter += 1 ;
121
+ if (counter >= steps) counter = 0 ;
121
122
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);
125
125
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);
127
130
}
128
131
129
132
void onClockOff () {
@@ -139,10 +142,7 @@ void pciSetup(byte pin) {
139
142
}
140
143
141
144
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 ;
146
146
}
147
147
148
148
void setup () {
@@ -151,16 +151,18 @@ void setup() {
151
151
pinMode (pin, OUTPUT);
152
152
}
153
153
154
- pinMode (CLOCK_PIN, INPUT_PULLUP );
154
+ pinMode (CLOCK_PIN, INPUT );
155
155
pinMode (BUTTON_PIN, INPUT_PULLUP);
156
156
pinMode (OUT_PIN, OUTPUT);
157
157
pinMode (INVERTED_OUT_PIN, OUTPUT);
158
158
159
159
pciSetup (CLOCK_PIN);
160
160
161
- active_steps = EEPROM.read (0 );
161
+ active_steps1 = EEPROM.read (0 );
162
+ active_steps2 = EEPROM.read (1 );
162
163
163
- setPositions ();
164
+ setPositions (active_steps1, positions1);
165
+ setPositions (active_steps2, positions2);
164
166
setActiveLeds ();
165
167
}
166
168
@@ -169,8 +171,11 @@ void loop() {
169
171
checkReset ();
170
172
171
173
if (!send_tick) return ;
172
-
173
174
send_tick = false ;
174
175
176
+ clock_state = digitalRead (CLOCK_PIN);
177
+ if (clock_state == last_clock_state) return ;
178
+ last_clock_state = clock_state;
179
+
175
180
clock_state ? onClockOn () : onClockOff ();
176
181
}
0 commit comments