]> git.piffa.net Git - sketchbook_andrea/blobdiff - libraries/common/common.cpp
Common, cambiato il behaiour di RGB LED:
[sketchbook_andrea] / libraries / common / common.cpp
index b6e1e095c3d48606e5ed017f4a3f4b1d88f26c01..4a886b26cc95186a358d0cd66af4abc5a5c27052 100644 (file)
@@ -10,6 +10,7 @@
 #include "Arduino.h"
 #include "common.h"
 
+#define DEBUG_not
 
 //////////////////////
 // RGB LED
@@ -43,49 +44,74 @@ 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(r);
+   Serial.print("-");
+   Serial.print(g);
+   Serial.print("-");
+   Serial.print(b);
+   while(1);
+#endif
+
+};
+
+void RGBLed::Rand () {
+// Imposta il colore di un LED RGB
+    analogWrite(redPin, random(0,256));
+    analogWrite(greenPin, random(0,256));
+    analogWrite(bluePin, random(0,256));
 };
 
 void RGBLed::Red () {
 // Accende il LED di rosso
-    SetColor(0,255,255);
+    SetColor(255,0,0);
 };
 
 void RGBLed::Green () {
 // Accende il LED di verde
-    SetColor(255,0,255);
+    SetColor(0,255,0);
 };
 
 void RGBLed::Blue () {
 // Accende il LED di blu
-    SetColor(255,255,0);
+    SetColor(0,0,255);
 };
 
 void RGBLed::Magenta () {
 // Accende il LED di magenta
-    SetColor(0,255,0);
+    SetColor(255,0,255);
 };
 
 void RGBLed::Cyano () {
 // Accende il LED di Cyano
-    SetColor(255,0,0);
+    SetColor(0,255,255);
 };
 
 void RGBLed::Yellow () {
 // Accende il LED di giallo
-    SetColor(0,0,255);
+    SetColor(255,255,0);
 };
 
 void RGBLed::White () {
 // Accende il LED
-    SetColor(0,0,0);
+    SetColor(255,255,255);
 };
 
 void RGBLed::Off () {
 // Spegne il LED
-    SetColor(255,255,255);
+    SetColor(0,0,0);
 };
 
 
@@ -93,6 +119,8 @@ void RGBLed::Off () {
 /////////////////////////////////////
 // Lampeggiatore
 // Constructor
+//
+// Esempi incrementali: https://lab.piffa.net/sketchbook_andrea/multitasking/
 Lampeggiatore::Lampeggiatore(int pin)
 {
     ledPin = pin;
@@ -112,14 +140,14 @@ void Lampeggiatore::Blink() {
     // Illumina il led a 500ms
 
     if(millis() + shift - 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);
+        // save the last time you blinked the LED
+        previousMillis += interval;
     }
-    // set the LED with the ledState of the variable:
-    digitalWrite(ledPin, ledState);
 };
 
 void Lampeggiatore::Blink(long time, long drift ) {
@@ -127,14 +155,13 @@ void Lampeggiatore::Blink(long time, long drift ) {
 
     shift = drift;
     if(millis() + shift - 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);
+        // save the last time you blinked the LED
+        previousMillis += time;
+    }
 };
 
 void Lampeggiatore::Blink(long up, long down, long drift ) {
@@ -143,11 +170,11 @@ void Lampeggiatore::Blink(long up, long down, long drift ) {
     shift = drift;
     if((ledState == HIGH)&& (millis() + shift - previousMillis > up)) {
         // save the last time you blinked the LED
-        previousMillis = millis();
+        previousMillis += up;
         ledState = LOW  ;
     }
     else if((ledState == LOW)&& (millis() + shift - previousMillis > down)) {
-        previousMillis = millis();
+        previousMillis += down;
         ledState = HIGH  ;
     }
 
@@ -157,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));
 }
 
@@ -256,7 +280,7 @@ void Pwm::Set(byte brightness) {
 
 
 void Pwm::lSet(byte brightness) {
-    // Imposta il valore del PWM
+    // Imposta il valore del PWM con correzione luminosita' LED
     analogWrite(ledPin, lum(brightness));
 }
 
@@ -357,7 +381,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
@@ -367,32 +391,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) {
-            Serial.print(">> Calibrazione annullata: segnale assente.");
-            ail = 1500;
+#ifdef DEBUG
+            Serial.println(">> Calibrazione annullata a causa di assenza di seganle. \nAssicurarsi di accendere radio e ricevente \ne ripetere la procedura.");
+#endif 
+            middle = 15000; // Return value is divided by 10
             break;
         };
-        ailIn = pulseIn(pin, HIGH, 25000);
-        if (ailIn != 0 ) {
-            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(10);
+            delay(50);
         }
     }
-    Serial.println(">> Fine Calibrazione: ");
-    Serial.print(ail / 10);
-    Serial.println("--");
+#ifdef DEBUG
+    Serial.print(">> Fine Calibrazione, media: ");
+    Serial.println(middle / 10 + 10);
     Serial.flush() ;
-    return(ail / 10) ;
+#endif 
+    return(middle / 10 ) ;
 // END calibrazione
-}
+};