]> git.piffa.net Git - sketchbook_andrea/blobdiff - libraries/common/common.cpp
PWM con float / coseno
[sketchbook_andrea] / libraries / common / common.cpp
index 1eed6a2c3a7353115bed2e0c033e09ce0ffe0628..d9c0233b0027a3b30844d26137b56d1b4767cfb4 100644 (file)
@@ -1,11 +1,6 @@
 /*  Common
-  Collezione di funzioni e oggetti comuni incontrati durante
-  i vari esercizi.
-
-  Source file
-  Contiene il codice C++ delle funzioni e degli oggetti,
-  nel file common.h ci sono gli headers: prototipi.
-
+ *
+ *  Oggetti di uso comune
  */
 
 #include "Arduino.h"
@@ -27,88 +22,174 @@ RGBLed::RGBLed(byte pinR, byte pinG, byte pinB) {
       pinMode(greenPin, OUTPUT);
 };
 
+void RGBLed::SetColor (byte r, byte g, byte b) {
+// Imposta il colore di un LED RGB
+      analogWrite(redPin,   r);
+      analogWrite(greenPin, g);
+      analogWrite(bluePin,  b);
+    };
+
 void RGBLed::Red () {
 // Accende il LED di rosso
-      analogWrite(redPin,   0);
-      analogWrite(greenPin, 255);
-      analogWrite(bluePin,  255);
+      SetColor(0,255,255);
     };
 
 void RGBLed::Green () {
 // Accende il LED di verde
-      analogWrite(redPin,   255);
-      analogWrite(greenPin, 0);
-      analogWrite(bluePin,  255);
+      SetColor(255,0,255);
     };
 
 void RGBLed::Blue () {
 // Accende il LED di blu
-      analogWrite(redPin,   255);
-      analogWrite(greenPin, 255);
-      analogWrite(bluePin,  0);
+      SetColor(255,255,0);
     };
 
 void RGBLed::Magenta () {
 // Accende il LED di magenta
-      analogWrite(redPin,   0);
-      analogWrite(greenPin, 255);
-      analogWrite(bluePin,  0);
+      SetColor(0,255,0);
     };
 
 void RGBLed::Cyano () {
 // Accende il LED di Cyano
-      analogWrite(redPin,   255);
-      analogWrite(greenPin, 0);
-      analogWrite(bluePin,  0);
+      SetColor(255,0,0);
     };
 
 void RGBLed::Yellow () {
 // Accende il LED di giallo
-      analogWrite(redPin,   0);
-      analogWrite(greenPin, 0);
-      analogWrite(bluePin,  255);
+      SetColor(0,0,255);
     };
 
 void RGBLed::White () {
 // Accende il LED 
-      analogWrite(redPin,   0);
-      analogWrite(greenPin, 0);
-      analogWrite(bluePin,  0);
+      SetColor(0,0,0);
     };
 
 void RGBLed::Off () {
 // Spegne il LED 
-      analogWrite(redPin,   255);
-      analogWrite(greenPin, 255);
-      analogWrite(bluePin,  255);
+      SetColor(255,255,255);
     };
 
-void RGBLed::SetColor (byte r, byte g, byte b) {
-      // Imposta il colore di un LED RGB
 
-      analogWrite(redPin,   r);
-      analogWrite(greenPin, g);
-      analogWrite(bluePin,  b);
+
+/////////////////////////////////////
+// Lampeggiatore
+// Constructor
+Lampeggiatore::Lampeggiatore(int pin)
+{
+    ledPin = pin;
+    pinMode(ledPin, OUTPUT);
+    ledState = LOW;
+    previousMillis = 0;
+    interval = 500;
+};
+
+
+
+
+// Una funzione facente parte di una classe prende il nome di "metodo" della stessa:
+void Lampeggiatore::Invert() {
+    // Inverte il lampeggio
+    ledState = HIGH ;
+}
+
+void Lampeggiatore::Blink() {
+    // Illumina il led a 500ms
+
+    if(millis() - previousMillis > interval) {
+        // save the last time you blinked the LED
+        previousMillis = millis();
+
+        // if the LED is off turn it on and vice-versa:
+        ledState = !ledState ; // Inverti il LED
+    }
+    // set the LED with the ledState of the variable:
+    digitalWrite(ledPin, ledState);
+};
+
+void Lampeggiatore::Blink(long time) {
+    // Illumina il led secondo un intervallo passato come argomento
+
+    if(millis() - previousMillis > time) {
+        // save the last time you blinked the LED
+        previousMillis = millis();
+
+        // if the LED is off turn it on and vice-versa:
+        ledState = !ledState ; // Inverti il LED
+    }
+    // set the LED with the ledState of the variable:
+    digitalWrite(ledPin, ledState);
+};
+
+void Lampeggiatore::Blink(long up, long down) {
+    // Illumina il ledB precisando ontime e downtime
+
+    if((ledState == HIGH)&& (millis() - previousMillis > up)) {
+    // save the last time you blinked the LED
+       previousMillis = millis();
+        ledState = LOW  ;
+    }
+    else if((ledState == LOW)&& (millis() - previousMillis > down)) {
+       previousMillis = millis();
+        ledState = HIGH  ;
+    }
+
+    // set the LED with the ledState of the variable:
+    digitalWrite(ledPin, ledState);
+};
+
+/////////////////////////////////////
+// Pwm
+// Constructor
+Pwm::Pwm(int pin)
+    // Gestione del PWM utilizzando millis
+    // per non bloccare il processore con delay
+    // Warning: serialWrite puo' interferire con i tempi.
+{
+    ledPin = pin;
+    pinMode(ledPin, OUTPUT);
+    previousMillis = 0;
+    byte brightness = 0 ;
+    increment = 1;
+};
+
+void Pwm::Up(long speed) {
+    // Aumenta progressivamente la luminosita' usanndo millis()
+    // quindi senza bloccare il processore
+    // Viene usato un float, in alternativa un coseno
+
+    if (millis() != previousMillis)  { // si potrebbe togliere
+            brightness = 255.0 /(float)speed * millis() ;
+            analogWrite(ledPin, brightness);
+
+        previousMillis = millis();
+    };
+}
+
+void Pwm::Down(long speed ) {
+    // Riduce progressivamente la luminosita' usanndo millis()
+    // quindi senza bloccare il processore
+
+    if (millis() != previousMillis)  {
+            brightness = 255 - 255.0 /(float)speed * millis() ;
+            analogWrite(ledPin, brightness);
+
+        previousMillis = millis();
     };
+}
+
+void Pwm::UD(long speed ) {
+    // Aumenta e riduce in sequenza la luminosita' usanndo millis()
+    brightness = 128 + 127 * cos(2 * PI / speed * millis());
+    analogWrite(ledPin, brightness);  
+}
+
 
 
 //////////////////
 // Funzioni
 
-void brilla(byte pin) {
-  // Accende e spegne il LED senza un argomento 
-  // per impostare la velocita' con delay().
-  const int velocita = 500;
-
-pinMode(pin, OUTPUT); 
-  // sequenze di istruzione: accendere e spegnere il LED
-  digitalWrite(pin, HIGH);   // turn the LED on (HIGH is the voltage level)
-  delay(velocita);               // wait for a second
-  digitalWrite(pin, LOW);    // turn the LED off by making the voltage LOW
-  delay(velocita);               // wait for a second
-};
 
-void brilla(byte pin, int velocita) {
+void brilla(byte pin, int velocita ) { // Defalt value di velocita' solo nell'Header
   // Accende e spegne il LED accetando un argomento 
   // per impostare la velocita'.
 
@@ -119,3 +200,4 @@ pinMode(pin, OUTPUT);
   digitalWrite(pin, LOW);    // turn the LED off by making the voltage LOW
   delay(velocita);               // wait for a second
 };
+