// Variabili
const byte thrPin = 3; // PIN collegato al CH3
-byte thr ; // Throttle
-int thrIn ;
+byte thr ; // Valore a 8bit per il throttle
+int thrIn ; // Valore rilevato del 3 Ch della RX
+const int thMin = 983; // In genere il valore minimo del TH resta costante,
+// per calcolarlo si puo' usare la funzione di calibrazione nel setup
+
byte caso;
void setup() {
- // I PINs vengono impostati dal constructor al momento
- // della dichiarazione dell'ogetto.
+ // I PINs vengono impostati dal constructor al momento
+ // della dichiarazione dell'ogetto.
- right.Invert() ; // Opzionale: inverte l'ordine del lampeggio da
- // HI -> LOW --> LOW -> HI
- // per avere 2 LED che lampeggiano alternativamente
+ right.Invert() ; // Opzionale: inverte l'ordine del lampeggio da
+ // HI -> LOW --> LOW -> HI
+ // per avere 2 LED che lampeggiano alternativamente
- randomSeed(analogRead(0));
+ randomSeed(analogRead(0));
}
void loop() {
- // Lettura CH3
- thrIn = pulseIn(thrPin, HIGH, 25000);
- thr = constrain(map(thrIn, 983, 2000, 0, 255), 0, 255) ;
+ // Lettura CH3 con pulsein, per usare interrupts vedi ../snippets.
+ thrIn = pulseIn(thrPin, HIGH, 25000);
+ if (thrIn >= thMin && thrIn < 2000) { // clean up
+ thr = thrIn ;
+ };
// Gestione throttle
- if (thr > 0 && thr < 15) {
- // IDLE
-
- rpwm.UD(2000);
- lpwm.UD(2000);
- sotto.lDown(1500);
- } else if (thr < 245) {
- // Throttle medio
-
- right.Blink(1120 - 4 * thr );
- left.Blink(1120 - 4 * thr );
- sotto.lSet(thr); // Luminosita' proporzionale al throttle
- } else {
- // Throttle al massimo: LED laterali lampeggiano a caso,
- // Sotto luminosita' a caso
+ if (thr < 1050) {
+ // IDLE
- caso = random(20, 240) ;
- right.Swap();
- left.Swap();
- sotto.lSet(caso);
- delay(caso);
- }
+ rpwm.UD(2000);
+ lpwm.UD(2000);
+ sotto.lDown(1500);
+ }
+ else if (thr < 1900) {
+ // Throttle medio
+ right.Blink(1120 - 4 * thr );
+ left.Blink(1120 - 4 * thr );
+ sotto.lSet(map(thrIn, thMin, 2000, 0, 255)); // Luminosita' proporzionale al throttle
+ }
+ else {
+ // Throttle al massimo: LED laterali lampeggiano a caso,
+ // Sotto luminosita' a caso
+ caso = random(20, 240) ;
+ right.Swap();
+ left.Swap();
+ sotto.lSet(caso);
+ }
}