]> git.piffa.net Git - sketchbook_andrea/blobdiff - libraries/common/common.cpp
PWM Pulse
[sketchbook_andrea] / libraries / common / common.cpp
index e99f202804a45134155d354388719b26f8b1d0de..1611ec10550beac17f0213e27941018c0f75f823 100644 (file)
@@ -10,7 +10,7 @@
 #include "Arduino.h"
 #include "common.h"
 
-#define DEBUG_not
+#define DEBUG
 
 //////////////////////
 // RGB LED
@@ -21,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);
@@ -44,17 +44,24 @@ 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
 #ifdef DEBUG
-   Serial.print(common - r);
+   Serial.print(r);
    Serial.print("-");
-   Serial.print(common - g);
+   Serial.print(g);
    Serial.print("-");
-   Serial.print(common - b);
+   Serial.print(b);
    while(1);
 #endif
 
@@ -203,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) {
@@ -272,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));