-
Notifications
You must be signed in to change notification settings - Fork 0
/
RGBTest.ino
148 lines (134 loc) · 3.37 KB
/
RGBTest.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
// RGB Test
/*The purpose of this sketch is to practice generating random variables
and using them to control an RGB LED and perpetually generate new colors.
This effectively allows the generation of 16 million color combinations.
This project was created using the following:
Software
Arduino IDE 2.3.2
Hardware
Arduino Hero Uno
Breadboard
x4 wires
RGB LED
330 resistors
*/
// used to allow seamless hero com
#include "Arduino.h"
// initialize pin variables
const byte BPin = 9; // Orange Wire controlling the blue pin
const byte GPin = 10; // Yellow Wire controlling the green pin
const byte RPin = 11; // White Wire controlling the red pin
// create function to send the brightness levels to the pin on the Hero board
void displayColor(
byte BLum,
byte GLum,
byte RLum
) {
analogWrite(BPin, BLum);
analogWrite(GPin, GLum);
analogWrite(RPin, RLum);
}
// initialize pin control and Serial for printing
void setup() {
pinMode(BPin, OUTPUT);
pinMode(RPin, OUTPUT);
pinMode(GPin, OUTPUT);
Serial.begin(9600);
}
// Below we create our random number generators
int genRanLum(){
int randLum = random(0, 255);
return randLum;
}
int genRanPin() {
int randPin = random(0, 3);
return randPin;
}
int genRanDirection() {
return random(0, 2); // Returns 0 or 1
}
// Initialize our individual pin light level variables
int BLum = 0;
int GLum = 0;
int RLum = 0;
// Initialize pin info variables
const int numPins = 3; // number of pins to choose from
const byte pinNums[numPins] = {11, 10, 9};
void loop() {
// generate our random objects
int randLum = genRanLum();
int randPin = genRanPin();
int randDirection = genRanDirection();
int targetLum;
byte chosenPin = pinNums[randPin];
//Serial.println(randPin);
//Serial.println(BLum);
//Serial.println(GLum);
//Serial.println(RLum);
// logic through which pin it is, whether or not we are decreasing,
// and begin altering the appropriate pin to the desired random level.
// Once finished the loop with start the process again
if (chosenPin == 11) {
if (randDirection == 0) {
targetLum = BLum - randLum; // Change for decrease
} else {
targetLum = BLum + randLum;
}
if (targetLum > 255) {
targetLum = 255;
} else if (targetLum < 0) {
targetLum = 0;
}
while (BLum != targetLum) {
if (BLum < targetLum) {
BLum++;
} else {
BLum--;
}
displayColor(BLum, GLum, RLum);
delay(5);
}
}
if (chosenPin == 10) {
if (randDirection == 0) {
targetLum = GLum - randLum; // Change for decrease
} else {
targetLum = GLum + randLum;
}
if (targetLum > 255) {
targetLum = 255;
} else if (targetLum < 0) {
targetLum = 0;
}
while (GLum != targetLum) {
if (GLum < targetLum) {
GLum++;
} else {
GLum--;
}
displayColor(BLum, GLum, RLum);
delay(5);
}
}
if (chosenPin == 9) {
if (randDirection == 0) {
targetLum = RLum - randLum; // Change for decrease
} else {
targetLum = RLum + randLum;
}
if (targetLum > 255) {
targetLum = 255;
} else if (targetLum < 0) {
targetLum = 0;
}
while (RLum != targetLum) {
if (RLum < targetLum) {
RLum++;
} else {
RLum--;
}
displayColor(BLum, GLum, RLum);
delay(5);
}
}
}