X-Git-Url: http://git.piffa.net/web?p=sketchbook_andrea;a=blobdiff_plain;f=libraries%2Fcommon%2Fcommon.cpp;h=1611ec10550beac17f0213e27941018c0f75f823;hp=4a886b26cc95186a358d0cd66af4abc5a5c27052;hb=631831fa763d6133c724343bb16a888909db5f85;hpb=b4d82b0010d746f1a0f0b4da698b26d217f1ab1f diff --git a/libraries/common/common.cpp b/libraries/common/common.cpp index 4a886b2..1611ec1 100644 --- a/libraries/common/common.cpp +++ b/libraries/common/common.cpp @@ -10,7 +10,7 @@ #include "Arduino.h" #include "common.h" -#define DEBUG_not +#define DEBUG ////////////////////// // RGB LED @@ -210,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) { @@ -279,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));