-
Notifications
You must be signed in to change notification settings - Fork 0
/
light_uartC.ino
163 lines (140 loc) · 3.74 KB
/
light_uartC.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
#include <FastLED.h>
#include "Arduino.h"
#include "stdio.h"
#include "hardware/uart.h"
#define LED_PIN 2
#define NUM_LEDS 20
#define IRQ_PIN 3
CRGB leds[NUM_LEDS];
char mode, brightness, r, g, b, stringy[5];
bool irq_flag = false, direction = true, isToBeTurnedOn = true;
int i, bytes_transmitted;
void emitStaticColourAll(int r, int g, int b, int brightness) {
for (int i = 0; i != 20; i++) {
leds[i] = CRGB(r, g, b);
}
FastLED.show(brightness);
}
void emitStaticColour(int r, int g, int b, int brightness, int ledIndex) {
leds[ledIndex] = CRGB(r, g, b);
FastLED.show(brightness);
}
void flashingLights(int r, int g, int b, int brightness) {
emitStaticColourAll(0, 0, 0, 0);
while (irq_flag == false) {
emitStaticColourAll(r, g, b, brightness);
delay(500);
emitStaticColourAll(0, 0, 0, 0);
delay(500);
}
}
void rainbowLights(int brightness) {
i = 0;
emitStaticColourAll(0, 0, 0, 0);
while (irq_flag == false) {
i = (direction == true) ? (i + 1) : (i - 1);
emitStaticColourAll(i, 0, 255 - i, brightness);
delay(30);
if (i == 253) {
direction = false;
}
else if (i == 3) {
direction = true;
}
}
}
void fadingLights(int r, int g, int b, int brightness) {
i = 0;
emitStaticColourAll(0, 0, 0, 0);
while (irq_flag == false) {
i = (direction == true) ? (i + 1) : (i - 1);
emitStaticColourAll(r, g, b, i);
delay(10);
if (i == 255) {
direction = false;
}
else if (i == 0) {
direction = true;
}
}
}
void patternLights(int r, int g, int b, int brightness) {
i = 0;
emitStaticColourAll(0, 0, 0, 0);
while (irq_flag == false) {
if (isToBeTurnedOn == true) {
emitStaticColour(r, g, b, 100, i);
}
else {
emitStaticColour(0, 0, 0, 100, i);
}
delay(100);
if (i == 20) {
isToBeTurnedOn = !(isToBeTurnedOn);
i = 0;
}
else {
i++;
}
}
}
void interrupt() {
irq_flag = true;
}
void setup() {
delay(2200);
//Serial.begin(9600); //computer connection
//delay(200);
Serial1.begin(300, SERIAL_8N1); //pico connection
//Serial.println("SERIAL1_TX: " + String(SERIAL1_TX));
//Serial.println("SERIAL1_RX: " + String(SERIAL1_RX));
pinMode(IRQ_PIN, INPUT_PULLDOWN);
attachInterrupt(digitalPinToInterrupt(IRQ_PIN), interrupt, RISING);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
emitStaticColourAll(0, 0, 0, 0);
}
void loop() {
if (Serial1.available()) {
brightness = mode = r = g = b = 0;
bytes_transmitted = Serial1.available();
//Serial.println(bytes_transmitted);
irq_flag = false;
brightness = (bytes_transmitted >= 0) ? (Serial1.read()) : (0);
mode = (bytes_transmitted >= 1) ? (Serial1.read()) : (0);
r = (bytes_transmitted >= 2) ? (Serial1.read()) : (0);
g = (bytes_transmitted >= 3) ? (Serial1.read()) : (0);
b = (bytes_transmitted >= 4) ? (Serial1.read()) : (0);
delay(5);
/*
sprintf(stringy, "%d", brightness);
Serial.println(stringy);
sprintf(stringy, "%d", mode);
Serial.println(stringy);
sprintf(stringy, "%d", r);
Serial.println(stringy);
sprintf(stringy, "%d", g);
Serial.println(stringy);
sprintf(stringy, "%d", b);
Serial.println(stringy);
*/
if (brightness == 0) {
emitStaticColourAll(0, 0, 0, 0);
}
else if (mode == 1) {
emitStaticColourAll(0, 0, 0, 0);
emitStaticColourAll(r, g, b, brightness);
}
else if (mode == 2) {
flashingLights(r, g, b, brightness);
}
else if (mode == 3) {
rainbowLights(brightness);
}
else if (mode == 4) {
fadingLights(r, g, b, brightness);
}
else if (mode == 5) {
patternLights(r, g, b, brightness);
}
}
}