Require components:
1x Arduino Uno
4x wires
1x LED RGB HW-479 (K016)
Video:
Code test 1:
int redpin = 11;
int greenpin = 10;
int bluepin = 9;
int val;
void setup() {
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
Serial.begin(9600);
}
void loop() {
for(val = 255; val > 0; val--)
{
analogWrite(11, val);
analogWrite(10, 255 - val);
analogWrite(9, 128 - val);
Serial.println(val, DEC);
delay(5);
}
for(val = 0; val < 255; val++)
{
analogWrite(11, val);
analogWrite(10, 255 - val);
analogWrite(9, 128 - val);
Serial.println(val, DEC);
delay(5);
}
}
Code switch basic colors:
const byte PIN_RED = 11;
const byte PIN_GREEN = 10;
const byte PIN_BLUE = 9;
const byte COLOR_RED[] = {255,0,0};
const byte COLOR_LIME[] = {0,255,0};
const byte COLOR_BLUE[] = {0,0,255};
const byte COLOR_WHITE[] = {255,255,255};
const byte COLOR_YELLOW[] = {255,255,0};
const byte COLOR_CYAN[] = {0,255,255};
const byte COLOR_MAGENTA[] = {255,0,255};
const byte COLOR_SILVER[] = {192,192,192};
const byte COLOR_GRAY[] = {128,128,128};
const byte COLOR_MAROON[] = {128,0,0};
const byte COLOR_OLIVE[] = {128,128,0};
const byte COLOR_GREEN[] = {0,128,0};
const byte COLOR_PURPLE[] = {128,0,128};
const byte COLOR_TEAL[] = {0,128,128};
const byte COLOR_NAVY[] = {0,0,128};
const unsigned short DURATION_COLOR = 1000;
void setup() {
pinMode(PIN_RED, OUTPUT);
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_BLUE, OUTPUT);
}
void setColor(byte colors[]) {
analogWrite(PIN_RED, colors[0]);
analogWrite(PIN_GREEN, colors[1]);
analogWrite(PIN_BLUE, colors[2]);
}
void loop() {
setColor(COLOR_RED); delay(DURATION_COLOR);
setColor(COLOR_LIME); delay(DURATION_COLOR);
setColor(COLOR_BLUE); delay(DURATION_COLOR);
setColor(COLOR_WHITE); delay(DURATION_COLOR);
setColor(COLOR_YELLOW); delay(DURATION_COLOR);
setColor(COLOR_MAGENTA); delay(DURATION_COLOR);
setColor(COLOR_SILVER); delay(DURATION_COLOR);
setColor(COLOR_GRAY); delay(DURATION_COLOR);
setColor(COLOR_MAROON); delay(DURATION_COLOR);
setColor(COLOR_OLIVE); delay(DURATION_COLOR);
setColor(COLOR_GREEN); delay(DURATION_COLOR);
setColor(COLOR_PURPLE); delay(DURATION_COLOR);
setColor(COLOR_TEAL); delay(DURATION_COLOR);
setColor(COLOR_NAVY); delay(DURATION_COLOR);
}
Color switch multi colors
const byte PIN_RED = 11;
const byte PIN_GREEN = 10;
const byte PIN_BLUE = 9;
const byte AMOUNT_INCREASE = 32; // min=1ms, but 255*255*255 = 16,581,375ms = 276 minutes, values should be 8, 16, 32, 64, 128
const unsigned short DURATION_COLOR = 100; // min=1ms, delay next color
void setup() {
pinMode(PIN_RED, OUTPUT);
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_BLUE, OUTPUT);
Serial.begin(9600);
}
void setColor(byte red, byte green, byte blue) {
analogWrite(PIN_RED, red);
analogWrite(PIN_GREEN, green);
analogWrite(PIN_BLUE, blue);
Serial.print(red);
Serial.print(":");
Serial.print(green);
Serial.print(":");
Serial.println(blue);
}
void loop() {
byte red = 0;
byte green = 0;
byte blue = 0;
for(byte red = 0; red <= 255; red = min(255, red + AMOUNT_INCREASE)){
for(byte green = 0; green <= 255; green = min(255, green + AMOUNT_INCREASE)){
for(byte blue = 0; blue <= 255; blue = min(255, blue + AMOUNT_INCREASE)){
setColor(red, green, blue);
delay(DURATION_COLOR);
if(blue == 255) break;
}
if(green == 255) break;
}
if(red == 255) break;
}
}
No comments:
Post a Comment