]> git.piffa.net Git - sketchbook_andrea/blobdiff - libraries/common/common.cpp
PWM Pulse
[sketchbook_andrea] / libraries / common / common.cpp
index 4a2c914731d01742eca4bae12c33f6f0c784da3b..1611ec10550beac17f0213e27941018c0f75f823 100644 (file)
@@ -10,6 +10,7 @@
 #include "Arduino.h"
 #include "common.h"
 
+#define DEBUG
 
 //////////////////////
 // RGB LED
@@ -20,7 +21,7 @@ RGBLed::RGBLed(byte pinR, byte pinG, byte pinB) {
     redPin    = pinR ;
     greenPin  = pinG ;
     bluePin   = pinB ;
-    common    = 255 ;
+    common    = 0 ;
 
     // Equvalente del Setup() per inizializzare i PIN
     pinMode(redPin, OUTPUT);
@@ -43,17 +44,26 @@ RGBLed::RGBLed(byte pinR, byte pinG, byte pinB, byte com) {
 
 void RGBLed::SetColor (byte r, byte g, byte b) {
 // Imposta il colore di un LED RGB
-    analogWrite(redPin,   common - r);
-    analogWrite(greenPin, common - g);
-    analogWrite(bluePin,  common - b);
+  if (common == 0) {
+    analogWrite(redPin,    r);
+    analogWrite(greenPin,  g);
+    analogWrite(bluePin,   b);
+  } else {
+    analogWrite(redPin,   255 - r);
+    analogWrite(greenPin, 255 - g);
+    analogWrite(bluePin,  255 - b);
+  }
+
 
 // Debug
-//   Serial.print(common - r);
-//   Serial.print("-");
-//   Serial.print(common - g);
-//   Serial.print("-");
-//   Serial.print(common - b);
-//   while(1);
+#ifdef DEBUG
+   Serial.print(r);
+   Serial.print("-");
+   Serial.print(g);
+   Serial.print("-");
+   Serial.print(b);
+   while(1);
+#endif
 
 };
 
@@ -109,6 +119,8 @@ void RGBLed::Off () {
 /////////////////////////////////////
 // Lampeggiatore
 // Constructor
+//
+// Esempi incrementali: https://lab.piffa.net/sketchbook_andrea/multitasking/
 Lampeggiatore::Lampeggiatore(int pin)
 {
     ledPin = pin;
@@ -172,19 +184,16 @@ void Lampeggiatore::Blink(long up, long down, long drift ) {
 
 void Lampeggiatore::High() {
     // Accende il LED
-
     digitalWrite(ledPin, HIGH);
 }
 
 void Lampeggiatore::Low() {
     // Spegne  il LED
-
     digitalWrite(ledPin, LOW);
 }
 
 void Lampeggiatore::Swap() {
     // Inverte lo stato del LED
-
     digitalWrite(ledPin, !digitalRead(ledPin));
 }
 
@@ -201,6 +210,8 @@ Pwm::Pwm(int pin)
     previousMillis = 0;
     byte brightness = 0 ;
     increment = 1;
+    int step = 0;              // Pulse: stato del loop PWM
+    int interval = 1;
 };
 
 void Pwm::Up(long speed, long drift) {
@@ -270,6 +281,50 @@ void Pwm::Set(byte brightness) {
 }
 
 
+void Pwm::Pulse(int interval, int pausa) {
+    // PWM up/down + un ciclo di pausa
+    // Interval e' la pausa tra uno step di 255 e l'altro
+
+    if ((millis() - previousMillis) >= interval) {
+      previousMillis = millis();
+      if (step < 256) { //UP
+        analogWrite(ledPin, lum(step));
+        step++ ;
+      } else if (step < 512 ) { // Down
+        analogWrite(ledPin, lum(511 - step));
+        step++ ;
+      } else if (step < 512 + pausa ){ // Pausa
+        analogWrite(ledPin, lum(0));
+        step++ ;
+      } else if (step >= 512 + pausa) { // Reset 
+      step = 0 ;
+      }
+    }
+} 
+
+void Pwm::RandomPulse(int min , int max, int pausa ) {
+    // PWM up/down + un ciclo di pausa
+    // L'intervallo e' random a ogni nuovo ciclo, 
+    // compreso tra un min - max passato come argomento
+
+    if ((millis() - previousMillis) >= interval) {
+      previousMillis = millis();
+      if (step < 256) { //UP
+        analogWrite(ledPin, lum(step));
+        step++ ;
+      } else if (step < 512 ) { // Down
+        analogWrite(ledPin, lum(511 - step));
+        step++ ;
+      } else if (step < (512 + pausa )){ // Pausa
+        analogWrite(ledPin, lum(0));
+        step++ ;
+      } else if ((step >= 512 + pausa )) {
+      step = 0 ;
+      interval = random(min,max);
+      }
+    }
+} 
+
 void Pwm::lSet(byte brightness) {
     // Imposta il valore del PWM con correzione luminosita' LED
     analogWrite(ledPin, lum(brightness));
@@ -372,7 +427,7 @@ byte lum(byte val) {
 };
 
 
-int calibraTrim(int pin, byte ledPin) {
+int calibraTrim(int pin, const byte ledPin) {
     /* START Calibrazione TRIM canale:
        Lettura di 10 smaple
        calcolo del valore medio esclusi gli 0
@@ -382,31 +437,40 @@ int calibraTrim(int pin, byte ledPin) {
        questa funzione nel setup serve per trovare il punto medio
        all'avvio dello sketch.
      */
+    pinMode(ledPin,OUTPUT);
     byte a             = 0;
-    int ail    = 0;
-    int ailIn  = 0;
+    int servoValue     = 0;
+    int middle = 0; 
+#ifdef DEBUG
     Serial.println(">> Calibrazione: ");
+#endif 
     while (a < 10) {
         if (millis() > 10000) {
+#ifdef DEBUG
             Serial.println(">> Calibrazione annullata a causa di assenza di seganle. \nAssicurarsi di accendere radio e ricevente \ne ripetere la procedura.");
-            ail = 15000; // Return value is divided by 10
+#endif 
+            middle = 15000; // Return value is divided by 10
             break;
         };
-        ailIn = pulseIn(pin, HIGH, 25000);
-        if (ailIn != 0 && ailIn > 1000 && ailIn <2000)  {
-            ail = ail + ailIn ;
+        servoValue = pulseIn(pin, HIGH, 25000);
+        if (servoValue != 0 && servoValue > 950 && servoValue <2000)  {
+            middle = middle + servoValue ;
             a++ ;
-            Serial.print(a);
+#ifdef DEBUG
+            Serial.print(servoValue);
             Serial.print(": ");
-            Serial.println(ail);
+            Serial.println(middle / a);
+#endif 
             digitalWrite(ledPin, !digitalRead(ledPin));
-            delay(100);
+            delay(50);
         }
     }
+#ifdef DEBUG
     Serial.print(">> Fine Calibrazione, media: ");
-    Serial.println(ail / 10);
+    Serial.println(middle / 10 + 10);
     Serial.flush() ;
-    return(ail / 10) ;
+#endif 
+    return(middle / 10 ) ;
 // END calibrazione
-}
+};