-
Notifications
You must be signed in to change notification settings - Fork 0
/
Read_RGB_from_Serial.ino.ino
125 lines (113 loc) · 2.4 KB
/
Read_RGB_from_Serial.ino.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
int colorRGB[3];
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int delayVal = 50;
int blnFade = 0;
int h_int;
float h;
int r=0, g=0, b=0;
void h2rgb(float h, int &R, int &G, int &B);
void colorFade();
void setup() {
Serial.begin(57600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
if(Serial.available() >= 2){
switch( byte( Serial.read() )) {
case 'r':
colorRGB[0] = Serial.read();
blnFade = 0;
break;
case 'g':
colorRGB[1] = Serial.read();
blnFade = 0;
break;
case 'b':
colorRGB[2] = Serial.read();
blnFade = 0;
break;
case 'c':
Serial.flush();
blnFade = 0;
break;
case 'f':
delayVal = Serial.read();
Serial.flush();
colorFade();
blnFade = 1;
}
}
analogWrite(redPin, 255 - colorRGB[0]);
analogWrite(greenPin, 255 - colorRGB[1]);
analogWrite(bluePin, 255 - colorRGB[2]);
delay(20);
}
void colorFade() {
while (blnFade == 1) {
if(Serial.available() ){
blnFade = 0;
break;
}
for(int fadeValue = 0 ; fadeValue <= 1024; fadeValue +=5) {
// h = ((float)fadeValue)/1024;
// h_int = (int) 360*h;
// h2rgb(h,r,g,b);
if(Serial.available() ){
blnFade = 0;
break;
}
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
delay(delayVal);
}
}
}
void h2rgb(float H, int& R, int& G, int& B) {
int var_i;
float S=1, V=1, var_1, var_2, var_3, var_h, var_r, var_g, var_b;
if ( S == 0 ) {
R = V * 255;
G = V * 255;
B = V * 255;
} else {
var_h = H * 6;
if ( var_h == 6 ) var_h = 0;
var_i = int( var_h ) ;
var_1 = V * ( 1 - S );
var_2 = V * ( 1 - S * ( var_h - var_i ) );
var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );
if ( var_i == 0 ) {
var_r = V ;
var_g = var_3 ;
var_b = var_1 ;
} else if ( var_i == 1 ) {
var_r = var_2 ;
var_g = V ;
var_b = var_1 ;
} else if ( var_i == 2 ) {
var_r = var_1 ;
var_g = V ;
var_b = var_3 ;
} else if ( var_i == 3 ) {
var_r = var_1 ;
var_g = var_2 ;
var_b = V ;
} else if ( var_i == 4 ) {
var_r = var_3 ;
var_g = var_1 ;
var_b = V ;
} else {
var_r = V ;
var_g = var_1 ;
var_b = var_2 ;
}
R = (1-var_r) * 255;
G = (1-var_g) * 255;
B = (1-var_b) * 255;
}
}