]> git.piffa.net Git - sketchbook_andrea/blob - RGB_LED/rgb_6_obj/rgb_6_obj.ino
723b8cbd78b39b0ea2a8f5ffaf021be218aa5f7c
[sketchbook_andrea] / RGB_LED / rgb_6_obj / rgb_6_obj.ino
1 /*
2     Adafruit Arduino - Lesson 3. RGB LED
3
4  RGB LED: mpostare i colori per un LED RGB
5  common anode
6
7  Schema: http://lab.piffa.net/schemi/rgb.jpg
8  */
9
10 class RGBLed {
11     const byte redPin ;
12     const byte greenPin ;
13     const byte bluePin ;
14     byte redValue ;
15     byte greenValue ;
16     byte blueValue ;
17
18     // Constructor: come viene instanziato un oggetto facente parte della classe
19   public:
20     RGBLed(byte pinR, byte pinG, byte pinB)
21     {
22       // Carichiamo i valori dei PIN dentro alle proprieta'
23       redPin    = pinR ;
24       greenPin  = pinG ;
25       bluePin   = pinB ;
26
27       // Equvalente del Setup() per inizializzare i PIN
28       pinMode(redPin, OUTPUT);
29       pinMode(greenPin, OUTPUT);
30       pinMode(greenPin, OUTPUT);
31     }
32
33     void Color (byte r, byte g, byte b) {
34       // Imposta il colore di un LED RGB
35       byte redValue   = r;
36       byte greenValue = g;
37       byte blueValue  = b;
38
39       analogWrite(redPin,   redValue);
40       analogWrite(greenPin, greenValue);
41       analogWrite(bluePin,  blueValue);
42     }
43 };
44
45 // Instanziamo un LED
46 RGBLed led(11, 10, 9); 
47 /* L'oggetto viene istanziato qui e non nella funzione di setup()
48  *  perche' altrimenti la sua esistenza sarebbe legata solo 
49  *  al contesto (scope) del setup(), non sarebbe disponibile nel loop()
50  */
51
52 void setup()  {
53       // I PIN mode vengono settati dal constructor    
54       }
55
56
57 void loop(){
58 led.Color(0,255,255) ; // Mettiamo il LED in Rosso
59     }