]> git.piffa.net Git - aerei/blob - esempi/base_th_3stati/base_th_3stati.ino
Stati / RGB
[aerei] / esempi / base_th_3stati / base_th_3stati.ino
1 /* Esempio
2
3    Esempio base:
4    2 LED / Strisce laterali che lampeggiano alternativamente
5    1 LED / Striscia sotto in FADE
6
7    Lettura del canale Throttle (3) con la funzione Pulsein
8 */
9
10 #include <common.h>
11
12 // LED disponibili
13 Lampeggiatore left = 10;
14 Lampeggiatore right = 11;
15 Pwm sotto = 9;
16
17 // Quando il Throttle e' in IDE facciamo un PWM anche sui laterali
18 Pwm lpwm = 10 ;
19 Pwm rpwm = 11;
20
21 // Variabili
22 const byte thrPin = 3; // PIN collegato al CH3
23 byte thr ; // Valore a 8bit per il throttle
24 int thrIn ; // Valore rilevato del 3 Ch della RX 
25 const int thMin = 983; // In genere il valore minimo del TH resta costante,
26 // per calcolarlo si puo' usare la funzione di calibrazione nel setup
27
28 byte caso;
29
30 void setup() {
31     // I PINs vengono impostati dal constructor al momento
32     // della dichiarazione dell'ogetto.
33
34     right.Invert() ;  // Opzionale: inverte l'ordine del lampeggio da
35     // HI -> LOW --> LOW -> HI
36     // per avere 2 LED che lampeggiano alternativamente
37
38     randomSeed(analogRead(0));
39 }
40
41 void loop() {
42     // Lettura CH3 con pulsein, per usare interrupts vedi ../snippets.
43     thrIn = pulseIn(thrPin, HIGH, 25000);
44     if (thrIn >= thMin && thrIn < 2000)  { // clean up
45         thr = thrIn ;
46     };
47
48 // Gestione throttle
49     if (thr < 1050) {
50         // IDLE
51
52         rpwm.UD(2000);
53         lpwm.UD(2000);
54         sotto.lDown(1500);
55     }
56     else if (thr < 1900) {
57         // Throttle medio
58         right.Blink(1120 - 4 * thr );
59         left.Blink(1120 - 4 * thr );
60         sotto.lSet(map(thrIn, thMin, 2000, 0, 255));   // Luminosita' proporzionale al throttle
61     }
62     else {
63         // Throttle al massimo: LED laterali lampeggiano a caso,
64         // Sotto luminosita' a caso
65         caso = random(20, 240) ;
66         right.Swap();
67         left.Swap();
68         sotto.lSet(caso);
69     }
70 }