|
1 | | -//Current version on the collar as of 3-19-2026! |
| 1 | +// Final "Halt on Press" Version - 3-19-2026 |
2 | 2 | #include <Adafruit_NeoPixel.h> |
3 | 3 |
|
4 | 4 | #define PIN 3 |
5 | 5 | #define NUM_OF_LEDS 56 |
6 | 6 |
|
7 | | -Adafruit_NeoPixel strip = Adafruit_NeoPixel(56, PIN, NEO_GRB + NEO_KHZ800); |
| 7 | +Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_OF_LEDS, PIN, NEO_GRB + NEO_KHZ800); |
8 | 8 |
|
9 | | - |
10 | | -int randomNumber; |
11 | | -int red[56]; |
12 | | -int green[56]; |
13 | | -int blue[56]; |
14 | | - int sensorPin = A0; |
15 | | - int sensorValue = 0; |
16 | | - int select = 0; |
17 | | - int i,j; |
18 | | - int element[8][3]; |
19 | | -boolean change; |
| 9 | +int red[56], green[56], blue[56]; |
| 10 | +int sensorPin = A0; |
| 11 | +int select = 0; |
20 | 12 |
|
21 | 13 | void setup() { |
22 | | - // put your setup code here, to run once: |
23 | | - strip.begin(); |
24 | | - strip.show(); // Initialize all pixels to 'off' |
25 | | - change = false; |
26 | | - |
27 | | - |
28 | | - randomSeed(analogRead(A1)); |
| 14 | + strip.begin(); |
| 15 | + strip.setBrightness(140); |
| 16 | + strip.show(); |
| 17 | + randomSeed(analogRead(A1)); |
29 | 18 | } |
30 | 19 |
|
31 | | -boolean check(){ |
32 | | - int sensorValue=analogRead(sensorPin); |
33 | | - if (sensorValue < 512 && !change) { |
34 | | - change = true; |
| 20 | +// This function now only handles the MODE SWAP after release |
| 21 | +boolean check() { |
| 22 | + if (analogRead(sensorPin) < 512) { |
| 23 | + // Wait until released |
| 24 | + while (analogRead(sensorPin) < 512) { |
| 25 | + delay(10); |
| 26 | + } |
| 27 | + |
| 28 | + // Advance mode |
35 | 29 | select++; |
36 | | - } else { |
37 | | - |
38 | | - change = false; |
| 30 | + if (select > 4) select = 0; |
39 | 31 |
|
| 32 | + delay(50); // Small debounce |
| 33 | + return true; |
40 | 34 | } |
41 | | - |
42 | | - if (select > 4) select = 0; |
43 | | - return change; |
| 35 | + return false; |
44 | 36 | } |
45 | 37 |
|
46 | | - |
47 | | - |
48 | | -void clearStrip(){ |
49 | | - for (int i = 0; i < NUM_OF_LEDS; i++){ |
50 | | - strip.setPixelColor(i, 0, 0, 0); |
| 38 | +void clearStrip() { |
| 39 | + for (int k = 0; k < NUM_OF_LEDS; k++) { |
| 40 | + strip.setPixelColor(k, 0, 0, 0); |
51 | 41 | } |
52 | 42 | strip.show(); |
53 | 43 | } |
54 | 44 |
|
55 | | -void greenChaserTask(int i){ |
| 45 | +void greenChaserTask(int i) { |
56 | 46 | int chaser_len = 5; |
57 | | - |
58 | 47 | strip.setPixelColor(i, 0, 125, 0); |
59 | | - |
60 | | - if (i < chaser_len) { |
61 | | - |
62 | | - strip.setPixelColor((i-chaser_len+56)%56, 0, 0, 0); |
63 | | - |
64 | | - } |
65 | | - |
66 | | - strip.setPixelColor(i-chaser_len, 0, 0, 0); |
67 | | - strip.setBrightness(140); |
| 48 | + // Modulo prevents negative index crashes |
| 49 | + strip.setPixelColor((i - chaser_len + NUM_OF_LEDS) % NUM_OF_LEDS, 0, 0, 0); |
68 | 50 | strip.show(); |
69 | 51 | } |
70 | 52 |
|
71 | | -void randomLightsTask(int i){ |
72 | | - if (i == 0){ |
| 53 | +void randomLightsTask(int i) { |
| 54 | + if (i == 0) { |
73 | 55 | for (int r = 0; r < 56; r++) { |
74 | | - red[r] = random(255); |
75 | | - } |
76 | | - |
77 | | - for (int g = 0; g < 56; g++) { |
78 | | - green[g] = random(255); |
79 | | - } |
80 | | - |
81 | | - for (int b = 0; b < 56; b++) { |
82 | | - blue[b] = random(255); |
| 56 | + red[r] = random(255); |
| 57 | + green[r] = random(255); |
| 58 | + blue[r] = random(255); |
83 | 59 | } |
84 | 60 | } |
85 | | - |
86 | 61 | strip.setPixelColor(i, red[i], green[i], blue[i]); |
87 | | - |
88 | | - for (int r = 0; r < 56; r++) { |
89 | | - red[r] -= 4; |
90 | | - } |
91 | | - |
92 | | - for (int g = 0; g < 56; g++) { |
93 | | - green[g] -= 4; |
94 | | - } |
95 | | - |
96 | | - for (int b = 0; b < 56; b++) { |
97 | | - blue[b] -= 4; |
| 62 | + for (int k = 0; k < 56; k++) { |
| 63 | + if(red[k] > 4) red[k] -= 4; |
| 64 | + if(green[k] > 4) green[k] -= 4; |
| 65 | + if(blue[k] > 4) blue[k] -= 4; |
98 | 66 | } |
99 | | - |
100 | | - strip.setBrightness(140); |
101 | 67 | strip.show(); |
102 | 68 | } |
103 | 69 |
|
104 | | -void colorBandTask(int i, int r, int g, int b){ |
105 | | - strip.setPixelColor(i, r, g, b); |
106 | | - strip.setBrightness(140); |
107 | | - strip.show(); |
108 | | -} |
109 | | - |
110 | | -void greenBandTask(int i){ |
111 | | - colorBandTask(i, 0, 255, 0); |
112 | | -} |
113 | | - |
114 | | -void redBandTask(int i){ |
115 | | - colorBandTask(i, 255, 0, 0); |
116 | | -} |
117 | | - |
118 | | -void blueBandTask(int i){ |
119 | | - colorBandTask(i, 0, 0, 255); |
| 70 | +void colorBandTask(int i, int r, int g, int b) { |
| 71 | + strip.setPixelColor(i, r, g, b); |
| 72 | + strip.show(); |
120 | 73 | } |
121 | 74 |
|
122 | | - |
123 | 75 | void loop() { |
124 | | - // put your main code here, to run repeatedly: |
125 | | - |
126 | | - /* Read Sensor |
127 | | - Sensor value will be 1023 when open, |
128 | | - will drop to 78 when blocked |
129 | | - When sensor is blocked, advance slection by 1*/ |
130 | | - |
131 | | - /* ALL GREEN */ |
| 76 | + for (int i = 0; i < NUM_OF_LEDS; i++) { |
| 77 | + |
| 78 | + // 1. Check for the press (and hold here) |
| 79 | + if (check()) { |
| 80 | + clearStrip(); |
| 81 | + break; // Exit 'for' loop to start next animation |
| 82 | + } |
132 | 83 |
|
133 | | - for (i = 0; i < NUM_OF_LEDS; i ++){ |
| 84 | + // 2. Run animations ONLY if sensor isn't being held |
134 | 85 | if (select == 0) greenChaserTask(i); |
135 | 86 | else if (select == 1) randomLightsTask(i); |
136 | | - else if (select == 2) greenBandTask(i); |
137 | | - else if (select == 3) blueBandTask(i); |
138 | | - else if (select == 4) redBandTask(i); |
139 | | - if (check()) {clearStrip(); break;} |
| 87 | + else if (select == 2) colorBandTask(i, 0, 255, 0); // Green |
| 88 | + else if (select == 3) colorBandTask(i, 0, 0, 255); // Blue |
| 89 | + else if (select == 4) colorBandTask(i, 255, 0, 0); // Red |
| 90 | + |
140 | 91 | delay(18); |
141 | | - } |
142 | | - |
143 | 92 | } |
144 | | - |
145 | | - |
146 | | - |
147 | | - |
148 | | - |
149 | | - |
150 | | - |
151 | | - |
152 | | -int cigarettes = 0; |
| 93 | +} |
0 commit comments