-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastLED_sensorInput.ino
More file actions
53 lines (38 loc) · 1.41 KB
/
FastLED_sensorInput.ino
File metadata and controls
53 lines (38 loc) · 1.41 KB
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
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 21
#define DATA_PIN 3
// Define the array of leds
CRGB leds[NUM_LEDS];
int led = -1; // which LED to light up in our background animation (start at -1 because we increment +1 as soon as animation starts)
int sensorLED; // which LED to light up in response to sensor
void setup() {
// GRB ordering is assumed
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(50);
Serial.begin(57600);
}
void loop() {
// SENSOR CONTROLLED MOVING DOT (on top of the animation):
int val = analogRead(A0);
// A pot would be mapped from 0-1023, but an IR distance sensor might be 5-695
sensorLED = map(val, 0, 1023, 0, NUM_LEDS);
// make sure numbers stay within range:
sensorLED = constrain(sensorLED, 0, NUM_LEDS);
Serial.print(val);
Serial.print(" = ");
Serial.println(sensorLED);
leds[sensorLED] = CRGB::Green;
FastLED.show();
// clear that LED for next time, but don't show right away
leds[sensorLED] = CRGB::Black;
// background animation, increment a dot through the whole strip:
EVERY_N_MILLISECONDS(200) { // change number to speed up or slow down
// clear the *most recent* LED
leds[led] = CRGB::Black;
// increment to next LED
led = led + 1;
if (led > NUM_LEDS) led = 0; // at the end of the strip, go back to the start
leds[led] = CRGB::Blue;
}
}