#include "Arduino.h"
#include "common.h"
+#define DEBUG
//////////////////////
// RGB LED
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);
};
/////////////////////////////////////
// Lampeggiatore
// Constructor
+//
+// Esempi incrementali: https://lab.piffa.net/sketchbook_andrea/multitasking/
Lampeggiatore::Lampeggiatore(int pin)
{
ledPin = pin;
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));
}
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) {
}
+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));
};
-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
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
-}
+};