1. RGB LED 디지털 출력 테스트
#include <PWMLed.h>
PWMLed red(9);
PWMLed green(10);
PWMLed blue(11);
void setup(){
Serial.begin(115200);
}
void loop(){
one_color();
two_color();
three_color();
}
void led_off(int d_time = 2000){
delay(d_time);
red.off();
green.off();
blue.off();
Serial.println("---------------------------------------")
}
void one_color()
{
Serial.println("R ON");
red.on();
led_off();
Serial.println("G ON");
green.on();
led_off();
Serial.println("B ON");
blue.on();
led_off();
}
void two_color() {
Serial.println("R and G ON");
red.on();
green.on();
led_off();
Serial.println("G and B ON");
green.on();
blue.on();
led_off();
Serial.println("B and R ON");
blue.on();
red.on();
led_off();
}
void three_color()
{
Serial.println("R and G and B ON");
red.on();
green.on();
blue.on();
led_off();
}
2. 실습: ColorRed 클래스
#pragma once
#include <PWMLed.h>
class ColorLed{
protected:
PWMLed red;
PWMLed green;
PWMLed blue;
public:
ColorLed(in red, int green, int blue);
void rgb(int r, int g, int b);
void off(int delay_time = 0);
void random();
};
#include "ColorLed.h"
ColorLed::ColorLed(int red, int green, int blue)
: red(red), green(green), blue(blue) {}
void ColorLed::rgb(int r, int g, int b){
red.setValue(r);
green.setValue(g);
blue.setValue(b);
}
void ColorLed::off(int delay_time){
rgb(0, 0, 0);
if(delay_time > 0){
delay(delay_time);
}
}
void ColorLed::random() {
int r = ::random(256);
int g = ::random(256);
int b = ::random(256);
rgb(r, g, b);
}
#include <ColorLed.h>
ColorLed leds(9, 10, 11);
void setup(){
Serial.begin(115200);
}
void loop() {
pwm_one_color();
pwm_two_color();
pwm_three_color();
}
void pwm_one_color()
{
Serial.println("R PWM OUTPUT");
for(int r = 0; r < 255; r += 50){
leds.rgb(r, 0, 0);
delay(500);
}
leds.off(1000);
Serial.println("-----------------------------");
Serial.println("G PWM OUTPUT");
for(int g = 0;g < 255;g += 50) {
leds.rgb(0, g, 0);
delay(500);
}
leds.off(1000);
Serial.println("------------------------------------");
Serial.println("B PWM OUTPUT");
for(int b = 0; b < 255; b += 50){
leds.rgb(0, b, 0);
delay(500);
}
leds.off(1000);
Serial.println("-----------------------------");
}
void pwm_two_color()
{
Serial.println("R and G PWM OUTPUT");
for(int r = 0;r < 255;r += 50) {
for(int g = 0;g < 255;g += 50) {
leds.rgb(r, g, 0);
delay(500);
}
}
leds.off(1000);
Serial.println("------------------------------------");
Serial.println("G and B PWM OUTPUT");
for(int g = 0;g < 255;g += 50) {
for(int b = 0;b < 255;b += 50) {
leds.rgb(0, g, b);
delay(500);
}
}
leds.off(1000);
Serial.println("------------------------------------");
Serial.println("B and R PWM OUTPUT");
for(int b = 0;b < 255;b += 50) {
for(int r = 0;r < 255;r += 50) {
leds.rgb(r, 0, b);
delay(500);
}
}
leds.off(1000);
Serial.println("------------------------------------");
}
void pwm_three_color()
{
int r, g, b;
Serial.println("R and G and B PWM OUTPUT");
for(int r = 0;r < 255;r += 50) {
for(int g = 0;g < 255;g += 50) {
for(int b = 0;b < 255;b += 50) {
leds.rgb(r, g, b);
delay(300);
}
}
}
leds.off(1000);
Serial.println("------------------------------------");
}