-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLumos-arduino.ino
110 lines (86 loc) · 2.31 KB
/
Lumos-arduino.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
#define LUMOS_DEBUG 0
#define LED_COUNT 60
#define LED_PIN 7
#define COLOR_SHIFT_STEP 15
#define HUE_DEVIATION 21
#define MIN_SATURATION_DEVIATION 245
#define MAX_SATURATION_DEVIATION 255
#define MIN_VALUE_DEVIATION 70
#define MAX_VALUE_DEVIATION 200
#define BT_RX_PIN 10
#define BT_TX_PIN 11
#if LUMOS_DEBUG == 1
#define LOG_D(x) Serial.println(x)
#else
#define LOG_D(x)
#endif
#include <FastLED.h>
#include <SoftwareSerial.h>
CRGB leds[LED_COUNT];
SoftwareSerial bluetooth(BT_RX_PIN, BT_TX_PIN); // Arduino RX, Arduino TX
uint8_t hue = 0;
uint8_t brightnessLevel = 0;
void setup()
{
#if LUMOS_DEBUG == 1
Serial.begin(9600);
#endif
LOG_D("Listening for incoming data...");
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, LED_COUNT);
FastLED.setBrightness(brightnessLevel);
bluetooth.begin(9600);
}
void loop()
{
stripeCycle();
if (bluetooth.available())
{
String cmd = "";
while (bluetooth.available())
{
cmd += (char) bluetooth.read();
m_delay(30);
}
LOG_D("Obtained value: " + cmd);
parseCommand(cmd);
}
}
inline void m_delay(int delayMillis)
{
unsigned long prevMillis = millis();
while (millis() - prevMillis <= delayMillis);
}
inline void parseCommand(const String& cmd)
{
if (cmd.charAt(0) == 'c')
{
hue = map(cmd.substring(1).toInt(), 0, 360, 0, 255);
LOG_D(hue);
}
if (cmd.charAt(0) == 'b')
{
int brightness = cmd.substring(1).toInt();
brightnessLevel = map(brightness, 0, 100, 0, 255);
FastLED.setBrightness(brightnessLevel);
LOG_D(brightnessLevel);
}
}
int counter = 0;
inline void stripeCycle()
{
for (int i = 0; i < LED_COUNT; i++)
{
leds[i] = getLEDColor((inoise8(i * COLOR_SHIFT_STEP, counter)));
}
counter += 20;
FastLED.show();
m_delay(20);
}
inline CHSV getLEDColor(uint8_t val)
{
return CHSV(
hue + map(val, 0, 255, 0, HUE_DEVIATION),
constrain(map(val, 0, 255, MAX_SATURATION_DEVIATION, MIN_SATURATION_DEVIATION), 0, 255),
constrain(map(val, 0, 255, MIN_VALUE_DEVIATION, MAX_VALUE_DEVIATION), 0, 255)
);
}