-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-color full-color LED SMD modules KY-009
42 lines (37 loc) · 1.28 KB
/
3-color full-color LED SMD modules KY-009
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
// this constant won't change:
const int redpin = 11; // the PWM pin the red LED is attached to
const int bluepin = 10; // the PWM pin the blue LED is attached to
const int greenpin = 9; // the PWM pin the green LED is attached to
// variables will change:
int val = 0; // initial LED intensity
int color = 0; // current LED color (0 = red, 1 = green, 2 = blue)
unsigned long lastChange = 0; // time of last color change
void setup() {
// declare PWM pin to be an output:
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
Serial.begin(9600);
Serial.println("Arduino Examples - SMD RGB LED");
}
void loop() {
// Cycle through the red, green, and blue colors every 2 seconds
if (millis() - lastChange >= 2000) {
lastChange = millis();
color = (color + 1) % 3; // switch to the next color (0, 1, or 2)
// Set the PWM values for the current color
if (color == 0) {
analogWrite(redpin, 255);
analogWrite(bluepin, 0);
analogWrite(greenpin, 0);
} else if (color == 1) {
analogWrite(redpin, 0);
analogWrite(bluepin, 0);
analogWrite(greenpin, 255);
} else if (color == 2) {
analogWrite(redpin, 0);
analogWrite(bluepin, 255);
analogWrite(greenpin, 0);
}
}
}