Arduino, RGB LED

이도현·2023년 8월 9일
0

아두이노 학습

목록 보기
11/34
post-custom-banner

1. RGB LED 디지털 출력 테스트

#include <PWMLed.h>

PWMLed red(9);
PWMLed green(10);
PWMLed blue(11);

void setup(){
	Serial.begin(115200);
}

void loop(){
	one_color();   // LED 1개씩 ON
	two_color();   // LED 2개씩 ON
	three_color();   // LED 3개씩 ON
}

// 3개의 LED 모두 OFF
void led_off(int d_time = 2000){
	delay(d_time);
	
	red.off();
	green.off();
	blue.off();
	
	Serial.println("---------------------------------------")
}// LED 1개씩(R, G, B) ON
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(); 
}

// LED 2개씩(RG, GB, BR) ON
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();
}

// LED 3개(RGB) ON
void three_color()
{
	Serial.println("R and G and B ON");
	red.on();
	green.on();
	blue.on();
	led_off();
}

2. 실습: ColorRed 클래스

// ColorLed.h
#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();
};
// ColorLed.cpp
#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);  //0~256사이의 정수 값
	int g = ::random(256);
	int b = ::random(256);
	rgb(r, g, b);
}
// app.ino

#include <ColorLed.h>

ColorLed leds(9, 10, 11);

void setup(){
	Serial.begin(115200);
}

void loop() {
	pwm_one_color();  // LED 1개씩 ON
	pwm_two_color();  // LED 2개씩 ON
	pwm_three_color();  // LED 3개씩 ON
}
// LED 1개씩 (R, G, B) 50간격으로 PWM 출력
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("-----------------------------");
}
// LED 2개씩(RG, GB, BR) 50간격으로 PWM 출력
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("------------------------------------");

	// 청색 LED와 적색 LED 50 간격으로 PWM 출력
	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("------------------------------------");
}
// LED 3개(RGB) 50간격으로 PWM 출력
void pwm_three_color()
{
		int r, g, b;
		// 적색 LED와 녹색 LED 50 간격으로 PWM 출력
		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("------------------------------------");
}
	
profile
좋은 지식 나누어요
post-custom-banner

0개의 댓글