]> git.piffa.net Git - sketchbook_andrea/blob - libraries/common/common.cpp
1eed6a2c3a7353115bed2e0c033e09ce0ffe0628
[sketchbook_andrea] / libraries / common / common.cpp
1 /*  Common
2   Collezione di funzioni e oggetti comuni incontrati durante
3   i vari esercizi.
4
5   Source file
6   Contiene il codice C++ delle funzioni e degli oggetti,
7   nel file common.h ci sono gli headers: prototipi.
8
9  */
10
11 #include "Arduino.h"
12 #include "common.h"
13
14
15 //////////////////////
16 // RGB LED
17 // Common anode
18
19 RGBLed::RGBLed(byte pinR, byte pinG, byte pinB) {
20       redPin    = pinR ;
21       greenPin  = pinG ;
22       bluePin   = pinB ;
23
24       // Equvalente del Setup() per inizializzare i PIN
25       pinMode(redPin, OUTPUT);
26       pinMode(greenPin, OUTPUT);
27       pinMode(greenPin, OUTPUT);
28 };
29
30 void RGBLed::Red () {
31 // Accende il LED di rosso
32       analogWrite(redPin,   0);
33       analogWrite(greenPin, 255);
34       analogWrite(bluePin,  255);
35     };
36
37 void RGBLed::Green () {
38 // Accende il LED di verde
39       analogWrite(redPin,   255);
40       analogWrite(greenPin, 0);
41       analogWrite(bluePin,  255);
42     };
43
44 void RGBLed::Blue () {
45 // Accende il LED di blu
46       analogWrite(redPin,   255);
47       analogWrite(greenPin, 255);
48       analogWrite(bluePin,  0);
49     };
50
51 void RGBLed::Magenta () {
52 // Accende il LED di magenta
53       analogWrite(redPin,   0);
54       analogWrite(greenPin, 255);
55       analogWrite(bluePin,  0);
56     };
57
58 void RGBLed::Cyano () {
59 // Accende il LED di Cyano
60       analogWrite(redPin,   255);
61       analogWrite(greenPin, 0);
62       analogWrite(bluePin,  0);
63     };
64
65 void RGBLed::Yellow () {
66 // Accende il LED di giallo
67       analogWrite(redPin,   0);
68       analogWrite(greenPin, 0);
69       analogWrite(bluePin,  255);
70     };
71
72 void RGBLed::White () {
73 // Accende il LED 
74       analogWrite(redPin,   0);
75       analogWrite(greenPin, 0);
76       analogWrite(bluePin,  0);
77     };
78
79 void RGBLed::Off () {
80 // Spegne il LED 
81       analogWrite(redPin,   255);
82       analogWrite(greenPin, 255);
83       analogWrite(bluePin,  255);
84     };
85
86 void RGBLed::SetColor (byte r, byte g, byte b) {
87       // Imposta il colore di un LED RGB
88
89       analogWrite(redPin,   r);
90       analogWrite(greenPin, g);
91       analogWrite(bluePin,  b);
92     };
93
94
95 //////////////////
96 // Funzioni
97
98 void brilla(byte pin) {
99   // Accende e spegne il LED senza un argomento 
100   // per impostare la velocita' con delay().
101   const int velocita = 500;
102
103 pinMode(pin, OUTPUT); 
104   // sequenze di istruzione: accendere e spegnere il LED
105   digitalWrite(pin, HIGH);   // turn the LED on (HIGH is the voltage level)
106   delay(velocita);               // wait for a second
107   digitalWrite(pin, LOW);    // turn the LED off by making the voltage LOW
108   delay(velocita);               // wait for a second
109 };
110
111 void brilla(byte pin, int velocita) {
112   // Accende e spegne il LED accetando un argomento 
113   // per impostare la velocita'.
114
115 pinMode(pin, OUTPUT); 
116   // sequenze di istruzione: accendere e spegnere il LED
117   digitalWrite(pin, HIGH);   // turn the LED on (HIGH is the voltage level)
118   delay(velocita);               // wait for a second
119   digitalWrite(pin, LOW);    // turn the LED off by making the voltage LOW
120   delay(velocita);               // wait for a second
121 };