-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkitty_kontrol.ino
478 lines (427 loc) · 14.4 KB
/
kitty_kontrol.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// kitty_kontrol.ino - code to kontrol operation of stepper motor in the cats head (and more)
// 1/31/2024 - Idea Fab Labs, Chico
// current issue: runSpeed() doesn't support acceleration ...
// see https://github.com/bblanchon/ArduinoContinuousStepper for future solution
#include <AccelStepper.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_TiCoServo.h>
// Pin usage defines (* = required to be this pin do not move)
#define PUL 9 // Stepper Pulse pin (output)
#define DIR 8 // Stepper Direction pin (output)
#define JUP 6 // Joystick Up pin (input)
#define JDN 4 // Joystick Down pin (input)
#define JLT 5 // Joystick Left pin (input)
#define JRT 7 // Joystick Right pin (input)
#define SRV 10 // Servo pin (output) *
#define ABA A2 // Arcade button #A pin (input)
#define ABB A3 // Arcade button #B pin (input)
#define LLS 3 // Left Limit Switch pin (input ~ interrupt) *
#define RLS 2 // Right Limit Switch pin (input ~ interrupt) *
#define SPD A0 // Potentiometer for speed pin (input ~ analog) *
//#define ACC A7 // Potentiometer for acceleration pin (input ~ analog)
// Neopixel defines
#define RGB A1 // Neopixel pin (output)
#define LEDS 8 // Number of Neopixels
#define BRGT 50 // Set brightness to about 1/5 (max = 255)
// servo predefined positions
#define SRVO_ON 180 // servo laser On position
#define SRVO_OFF 20 // servo laser Off position
#define SRVO_MIN 1000 // 1 ms pulse
#define SRVO_MAX 2000 // 2 ms pulse
// Stepper speed variables
int MaxSpeed = 500; // maximum speed for stepper
int MinSpeed = 50; // minimum speed for stepper
//---------------------------------------------------------------------------------------------------------------------------------------------------
// Stepper predefined direction
#define STOP 0
#define CW 1
#define CCW 2
#define RIGHT CCW
#define LEFT CW
// Predefined RGB Led patterns
#define CWR 0 // colorWipeRed
#define CWG 1 // colorWipeGreen
#define CWB 2 // colorWipeBlue
#define RNB 3 // rainbow
#define TCRB 4 // theaterChaseRainbow
#define BLK 5 // colorWipeBlack (off)
// Stepper more variables
int Speed = STOP; // initial speed stopped
//int MaxAcceleration = 200; // maximum acceleration for stepper
//int MinAcceleration = 20; // minimum acceleration for stepper
//int Acceleration = 20; // initial acceleration rate
int RUNNING = STOP; // are we moving? and in what direction?
bool AUTOMATIC = false; // are we in automatic (bounce) mode
// Joystick & Arcade button variables
int JoyLeft_curr = HIGH;
int JoyLeft_last = HIGH;
int JoyRight_curr = HIGH;
int JoyRight_last = HIGH;
int JoyUp_curr = HIGH;
int JoyUp_last = HIGH;
int JoyDown_curr = HIGH;
int JoyDown_last = HIGH;
int ArcadeA_curr = HIGH;
int ArcadeA_last = HIGH;
int ArcadeB_curr = HIGH;
int ArcadeB_last = HIGH;
// Interrupts variables
int stateRight_curr = HIGH;
int stateLeft_curr = HIGH;
int stateRight_last = HIGH;
int stateLeft_last = HIGH;
// neopixel variables
unsigned long pixelPrevious = 0; // Previous Pixel Millis
//unsigned long patternPrevious = 0; // Previous Pattern Millis
int patternCurrent = RNB; // Current Pattern Number (Rainbow)
//int patternInterval = 5000; // Pattern Interval (ms)
int pixelInterval = 50; // Pixel Interval (ms)
int pixelQueue = 0; // Pattern Pixel Queue
int pixelCycle = 0; // Pattern Pixel Cycle
uint16_t pixelCurrent = 0; // Pattern Current Pixel Number
uint16_t pixelNumber = LEDS; // Total Number of Pixels
// Define the stepper and the pins used
AccelStepper stepper(AccelStepper::DRIVER, PUL, DIR);
// Define ws2812 leds
Adafruit_NeoPixel strip(LEDS, RGB, NEO_GRB + NEO_KHZ800);
// Define servo object to control a servo
Adafruit_TiCoServo servo;
int Servo_curr = SRVO_OFF;
int Servo_last = SRVO_OFF;
// The setup
void setup() {
Serial.begin(115200); // Initialize Serial
Serial.println("Startup ...");
// Initialize NeoPixel driver
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(BRGT); // Set BRGT to about 1/5 (max = 255)
// Initialize digital pins
pinMode(LED_BUILTIN, OUTPUT); // onboard LED
digitalWrite(LED_BUILTIN, LOW);
pinMode(JUP, INPUT_PULLUP); // Joystick up
pinMode(JDN, INPUT_PULLUP); // Joystick down
pinMode(JLT, INPUT_PULLUP); // Joystick left
pinMode(JRT, INPUT_PULLUP); // Joystick right
pinMode(ABA, INPUT_PULLUP); // Arcade button A
pinMode(ABB, INPUT_PULLUP); // Arcade button B
// Initialize Limit-switch Interrupts
pinMode(LLS, INPUT_PULLUP);
pinMode(RLS, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(LLS), limitLeft, FALLING);
attachInterrupt(digitalPinToInterrupt(RLS), limitRight, FALLING);
// initialized limit switch variables
stateRight_curr = digitalRead(RLS);
stateLeft_curr = digitalRead(LLS);
if(stateRight_curr == LOW) {
Serial.println("Startin up on the right limit ...");
} else {
if(stateLeft_curr == LOW) {
Serial.println("Startin up on the left limit ...");
}
}
if(stateRight_curr == LOW && stateLeft_curr == LOW) {
Serial.println("Did you dumb-dumbs hook to the NC and not the NO pins on the switches again? ...");
}
stateRight_last = stateRight_curr;
stateLeft_last = stateLeft_curr;
// Initialize stepper settings
//stepper.setAcceleration(Acceleration);
stepper.setMaxSpeed(MaxSpeed);
stepper.setSpeed(Speed);
// Initialize servo settings
servo.attach(SRV, SRVO_MIN, SRVO_MAX); // attaches the servo to the servo object
servo.write(Servo_curr);
}
// The loop
void loop() {
checkLimits();
readJoystick();
moveStepper();
readArcade();
moveServo();
Neoloop();
}
// Move servo
void moveServo() {
if(Servo_curr != Servo_last) {
servo.write(Servo_curr);
Servo_last = Servo_curr;
}
}
// Move stepper / update speed
void moveStepper() {
if(RUNNING > STOP) {
readSpeed();
//readAcceleration();
//stepper.setAcceleration(Acceleration);
if(RUNNING == RIGHT) {
Speed = -Speed; // RIGHT
}
stepper.setSpeed(Speed);
}
stepper.runSpeed();
}
// Check limit-switches & Process
void checkLimits() {
if(stateRight_curr != stateRight_last) {
Serial.println("Right Limit Hit! ...");
RUNNING = STOP;
//stepper.setAcceleration(STOP); // hard stop!
stepper.setSpeed(STOP);
stepper.runSpeed(); // stop asap
digitalWrite(LED_BUILTIN, LOW);
stateRight_last = stateRight_curr;
if(AUTOMATIC) {
stateRight_last = HIGH;
stateRight_curr = HIGH;
RUNNING = LEFT;
digitalWrite(LED_BUILTIN, HIGH);
}
} else {
if(stateLeft_curr != stateLeft_last) {
Serial.println("Left Limit Hit! ...");
RUNNING = STOP;
//stepper.setAcceleration(STOP); // hard stop!
stepper.setSpeed(STOP);
stepper.runSpeed(); // stop asap
digitalWrite(LED_BUILTIN, LOW);
stateLeft_last = stateLeft_curr;
if(AUTOMATIC) {
stateLeft_last = HIGH;
stateLeft_curr = HIGH;
RUNNING = RIGHT;
digitalWrite(LED_BUILTIN, HIGH);
}
}
}
}
// Read Arcade buttons & Process
void readArcade() {
ArcadeA_curr = digitalRead(ABA);
ArcadeB_curr = digitalRead(ABB);
if(ArcadeA_curr != ArcadeA_last) {
if(ArcadeA_curr == HIGH) {
Serial.println("Arcade A off");
// do nothing
} else {
Serial.println("Arcade A on");
if(patternCurrent + 1 > 5) {
patternCurrent = 0;
} else {
patternCurrent = patternCurrent + 1;
}
}
ArcadeA_last = ArcadeA_curr;
}
if(ArcadeB_curr != ArcadeB_last) {
if(ArcadeB_curr == HIGH) {
Serial.println("Arcade B off");
Servo_curr = SRVO_OFF;
} else {
Serial.println("Arcade B on");
Servo_curr = SRVO_ON;
}
ArcadeB_last = ArcadeB_curr;
}
}
// Read joystick switches & Process
void readJoystick() {
JoyLeft_curr = digitalRead(JLT);
JoyRight_curr = digitalRead(JRT);
JoyUp_curr = digitalRead(JUP);
JoyDown_curr = digitalRead(JDN);
// Joystick left switch
if(JoyLeft_curr != JoyLeft_last) {
if(JoyLeft_curr == HIGH) {
Serial.println("Stop ...");
RUNNING = STOP;
//stepper.setAcceleration(Acceleration);
stepper.setSpeed(STOP);
digitalWrite(LED_BUILTIN, LOW);
} else {
if(AUTOMATIC) {
AUTOMATIC = false;
RUNNING = STOP;
stepper.setSpeed(STOP);
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Automatic stopped ...");
}
if(stateLeft_last == LOW) { // left limit already reached!
Serial.println("Left limit active!");
JoyLeft_last = JoyLeft_curr;
return;
}
if(stateRight_last == LOW) { // reset right limit switch...
stateRight_last = HIGH;
stateRight_curr = HIGH;
}
Serial.println("Left ...");
RUNNING = LEFT;
digitalWrite(LED_BUILTIN, HIGH);
}
JoyLeft_last = JoyLeft_curr;
}
// Joystick right switch
if(JoyRight_curr != JoyRight_last) {
if(JoyRight_curr == HIGH) {
Serial.println("Stop ...");
RUNNING = STOP;
//stepper.setAcceleration(Acceleration);
stepper.setSpeed(STOP);
digitalWrite(LED_BUILTIN, LOW);
} else {
if(AUTOMATIC) {
AUTOMATIC = false;
RUNNING = STOP;
stepper.setSpeed(STOP);
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Automatic stopped ...");
}
if(stateRight_last == LOW) { // Right limit already reached!
Serial.println("Right limit active!");
JoyRight_last = JoyRight_curr;
return;
}
if(stateLeft_last == LOW) { // reset left limit switch...
stateLeft_last = HIGH;
stateLeft_curr = HIGH;
}
Serial.println("Right ...");
RUNNING = RIGHT;
digitalWrite(LED_BUILTIN, HIGH);
}
JoyRight_last = JoyRight_curr;
}
// Joystick up switch
if(JoyUp_curr != JoyUp_last) {
if(JoyUp_curr == LOW) {
if(AUTOMATIC) {
Serial.println("Automatic already running ...");
return;
}
AUTOMATIC = true;
if(stateRight_last == LOW) {
RUNNING = LEFT;
} else {
RUNNING = RIGHT;
}
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("Automatic started ...");
}
JoyUp_last = JoyUp_curr;
}
// Joystick down switch
if(JoyDown_curr != JoyDown_last) {
if(JoyDown_curr == LOW) {
if(AUTOMATIC) {
AUTOMATIC = false;
RUNNING = STOP;
stepper.setSpeed(STOP);
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Automatic stopped ...");
}
}
JoyDown_last = JoyDown_curr;
}
}
// Read analog potentiometer / calculate speed
void readSpeed() {
Speed = map((analogRead(SPD)), 0, 1023, MinSpeed, MaxSpeed);
}
/*
// Read analog potentiometer / calculate acceleration
void readAcceleration() {
//Acceleration = map((analogRead(ACC)), 0, 1023, MinAcceleration, MaxAcceleration);
Acceleration = 200;
}
*/
// Left limit-switch ISR
void limitLeft() {
stateLeft_curr = LOW; // slim and clean ISR
}
// Right limit-switch ISR
void limitRight() {
stateRight_curr = LOW; // slim and clean ISR
}
// Neopixel routines
void Neoloop() {
unsigned long currentMillis = millis(); // Update current time
if(currentMillis - pixelPrevious >= pixelInterval) { // Check for expired time
pixelPrevious = currentMillis; // Run current frame
switch (patternCurrent) {
case BLK:
colorWipe(strip.Color(0, 0, 0), 50); // Blackout wipe
break;
case TCRB:
theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant
break;
case RNB:
rainbow(10); // Flowing rainbow cycle along the whole strip
break;
case CWB:
colorWipe(strip.Color(0, 0, 255), 50); // Blue
break;
case CWG:
colorWipe(strip.Color(0, 255, 0), 50); // Green
break;
default:
colorWipe(strip.Color(255, 0, 0), 50); // Red
break;
}
}
}
void colorWipe(uint32_t color, int wait) {
if(pixelInterval != wait)
pixelInterval = wait; // Update delay time
strip.setPixelColor(pixelCurrent, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
pixelCurrent++; // Advance current pixel
if(pixelCurrent >= pixelNumber) // Loop the pattern from the first LED
pixelCurrent = 0;
}
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(uint8_t wait) {
if(pixelInterval != wait)
pixelInterval = wait;
for(uint16_t i=0; i < pixelNumber; i++) {
strip.setPixelColor(i, Wheel((i + pixelCycle) & 255)); // Update delay time
}
strip.show(); // Update strip to match
pixelCycle++; // Advance current cycle
if(pixelCycle >= 256)
pixelCycle = 0; // Loop the cycle back to the begining
}
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
if(pixelInterval != wait)
pixelInterval = wait; // Update delay time
for(int i=0; i < pixelNumber; i+=3) {
strip.setPixelColor(i + pixelQueue, Wheel((i + pixelCycle) % 255)); // Update delay time
}
strip.show();
for(int i=0; i < pixelNumber; i+=3) {
strip.setPixelColor(i + pixelQueue, strip.Color(0, 0, 0)); // Update delay time
}
pixelQueue++; // Advance current queue
pixelCycle++; // Advance current cycle
if(pixelQueue >= 3)
pixelQueue = 0; // Loop
if(pixelCycle >= 256)
pixelCycle = 0; // Loop
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
// sfranzyshen