본문 바로가기

아두이노-스케치

4 pin RGB Led로 모든 색깔 표현 하고 fade in 구현



4 pin RGB Led로 모든 색깔 표현 하고 fade in 구현



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
/* YourDuinoStarter Example: RGB_LED_Fade
 - Red-Green-Blue LED fades between colors automatically
 - SEE the comments after "//" on each line below
 - CONNECTIONS: NOTE: Hold RGB LED with pins down and longer pin
                second from the Left. 4 Pins in order left-right:
   - Left:  RED
   - Second: +5V "Common Anode"
   - Third: BLUE
   - Right: Green
 - V1.00 09/17/12
   Based on code by Matthew L Beckler
   Questions: terry@yourduino.com */
 
/*-----( Import needed libraries )-----*/
//none
/*-----( Declare Constants and Pin Numbers )-----*/
 
#define redPin   11  
#define bluePin 10
#define greenPin   9
 
#define brighter  1  //Used by SetLED_brightness function
#define dimmer    0
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
int  redValue ;  // Hold the current brightness value
int  greenValue;
int  blueValue;
 
int  updateDelay; // Milliseconds delay on changes
 
 
void setup()   /****** SETUP: RUNS ONCE ******/
{
    /* NOTE: Because the LED is "common Anode", the LEDs get brighter
       as the Arduino pin goes towards 0 volts. (It is "Inverted").
       So value=255 of OFF and value=0 in brightest.  */
   
      redValue = 255;  // Start with all off
      greenValue = 255;
      blueValue = 255;
      updateDelay = 2// Ms delay. Higher will be slower. Try 2 also.
      SetLED_brightness();  // This is defined at the end of this text
}//--(end setup )---
 
 
void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
 
      ColorChange(&redValue, brighter ); // Red only
      ColorChange(&redValue, dimmer ); 
      delay(updateDelay * 100);  
 
      ColorChange(&greenValue, brighter ); // Green only
      ColorChange(&greenValue, dimmer );   
      delay(updateDelay * 100);    
  
      ColorChange(&blueValue, brighter ); // Blue only
      ColorChange(&blueValue, dimmer );   
      delay(updateDelay * 100);      
 
      ColorChange(&redValue, brighter ); // Red plus Green = Yellow
      ColorChange(&greenValue, brighter );  
      delay(updateDelay * 100);  
      ColorChange(&blueValue, brighter ); // plus Blue 
      delay(updateDelay * 100);  
      ColorChange(&redValue, dimmer );   
      ColorChange(&greenValue, dimmer ); 
      ColorChange(&blueValue, dimmer );   
      delay(updateDelay * 200);
}//--(end main loop )---
 
/*-----( Declare User-written Functions )-----*/
/*------( This function updates the LED outputs)----*/
 
void SetLED_brightness()
{
    analogWrite(redPin, redValue);
      analogWrite(greenPin, greenValue);
      analogWrite(bluePin, blueValue);
// End SetLED_brightness
 
/*--(Update one of the Colors )--------------------------*/ 
 
void ColorChange(int* LEDvalue, int howBright)
/*--( Brighten an LED to full, or dim to dark )---*/
{
      for (int i = 0; i < 255; i++)
      {
        if (howBright == dimmer)    (*LEDvalue)++
        if (howBright == brighter)  (*LEDvalue)--;
        SetLED_brightness();
        delay(updateDelay);
      }
}
//*********( THE END )***********
 
 
cs