forked from GadgetReboot/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChaser.ino
126 lines (105 loc) · 3.23 KB
/
Chaser.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
// ----------------------------------------------------------------------------
// How to use the FastLED library for simple control of WS2812B RGB LEDs
// Two lines are drawn, one Green and one Blue, each 3 LEDs in length
// Each line moves end to end along the strip at slightly different rates
// If the pot is moved high enough from the ground terminal, the green
// line can be moved manually by the pot wiper position along the strip.
//
// This example code is unlicensed and is released into the public domain
//
// Gadget Reboot
// ----------------------------------------------------------------------------
#include <FastLED.h>
//#define DEBUG
#define NUM_LEDS 60
#define DATA_PIN 2
#define COLOR_ORDER GRB
#define LED_TYPE WS2812B
#define POT A0
// Define the array of leds
CRGB leds[NUM_LEDS];
int onePos = 2;
int twoPos = 2;
byte oneDir = 0;
byte twoDir = 0;
void setup() {
#ifdef DEBUG
Serial.begin(9600);
#endif
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
}
void loop() {
// if pot is above ground, override green line position based on pot position
if (analogRead(POT) > 10) {
onePos = map(analogRead(POT), 0, 1023, 0, NUM_LEDS - 1);
}
#ifdef DEBUG
Serial.print("Potentiometer: ");
Serial.println(analogRead(POT));
#endif
// turn on the green and blue LEDs to draw the current
// position of each line along the strip
leds[onePos] = CRGB(0, 255, 0);
leds[max(onePos - 1, 0)] = CRGB(0, 255, 0);
leds[min(onePos + 1, NUM_LEDS - 1)] = CRGB(0, 255, 0);
leds[twoPos] = CRGB(0, 0, 255);
leds[max(twoPos - 1, 0)] = CRGB(0, 0, 255);
leds[min(twoPos + 1, NUM_LEDS - 1)] = CRGB(0, 0, 255);
FastLED.show();
delay(150);
// turn all LEDs off so they can be re-drawn on the next loop
// in the next calculated position
leds[onePos] = CRGB(0, 0, 0);
leds[max(onePos - 1, 0)] = CRGB(0, 0, 0);
leds[min(onePos + 1, NUM_LEDS - 1)] = CRGB(0, 0, 0);
leds[twoPos] = CRGB(0, 0, 0);
leds[max(twoPos - 1, 0)] = CRGB(0, 0, 0);
leds[min(twoPos + 1, NUM_LEDS - 1)] = CRGB(0, 0, 0);
FastLED.show();
#ifdef DEBUG
Serial.print("One Direction: ");
Serial.println(oneDir);
Serial.print("One Position: ");
Serial.println(onePos);
//Serial.println();
Serial.print("Two Direction: ");
Serial.println(twoDir);
Serial.print("Two Position: ");
Serial.println(twoPos);
Serial.println();
#endif
// calculate the next position of each LED line by advancing the
// LED position in the forward or reverse direction as required.
// if the line reaches the end of the LED strip, it is time to
// change its direction.
if (oneDir == 0) { // if going forward
onePos += 4;
if (onePos >= NUM_LEDS) {
onePos = NUM_LEDS - 1;
oneDir = 1; // go in reverse direction
}
}
// else { //if going reverse
if (oneDir == 1) {
onePos -= 4;
if (onePos <= 0) {
onePos = 0;
oneDir = 0; // go in forward direction
}
}
if (twoDir == 0) { // if going forward
twoPos += 3;
if (twoPos >= NUM_LEDS) {
twoPos = NUM_LEDS - 1;
twoDir = 1; // go in reverse direction
}
}
//else { //if going reverse
if (twoDir == 1) {
twoPos -= 3;
if (twoPos <= 0) {
twoPos = 0;
twoDir = 0; // go in forward direction
}
}
}