]> git.piffa.net Git - aerei/blob - esempi/base_th_3stati/base_th_3stati.ino
Alettoni con interrupt + motore
[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 = 12;
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 = 12;
20
21 // Variabili
22 const byte thrPin = 3; // PIN collegato al CH3
23 int thrIn ; // Valore rilevato del 3 Ch della RX 
24 int thr ; // Valore a 16bit per il throttle
25 byte thrBit ; // Valore a 8bit per il throttle
26 const int thMin = 983; // In genere il valore minimo del TH resta costante,
27 // per calcolarlo si puo' usare la funzione di calibrazione nel setup
28
29 byte caso;
30
31 void setup() {
32     // I PINs vengono impostati dal constructor al momento
33     // della dichiarazione dell'ogetto.
34
35     right.Invert() ;  // Opzionale: inverte l'ordine del lampeggio da
36     // HI -> LOW --> LOW -> HI
37     // per avere 2 LED che lampeggiano alternativamente
38
39     randomSeed(analogRead(0));
40 }
41
42 void loop() {
43     // Lettura CH3 con pulsein, per usare interrupts vedi ../snippets.
44     thrIn = pulseIn(thrPin, HIGH, 25000);
45     if (thrIn >= thMin && thrIn < 2000)  { // clean up
46         thr = thrIn ;
47     };
48
49 // Gestione throttle
50     if (thr < 1050) {
51         // IDLE
52         rpwm.UD(2000);
53         lpwm.UD(2000);
54         sotto.lDown(1500);
55     }
56     else if (thr > 1900) {
57         // Throttle al massimo: LED laterali lampeggiano a caso,
58         // Sotto luminosita' a caso
59         caso = random(30, 250) ;
60         right.Swap();
61         left.Swap();
62         sotto.lSet(caso);
63         delay(caso);
64     }
65     else {
66         // Throttle medio
67         thrBit = map(thr,1050, 1900, 0, 255);
68         right.Blink(1220 - 4 * thrBit );
69         left.Blink(1220 - 4 * thrBit );
70         sotto.lSet(thrBit));   // Luminosita' proporzionale al throttle
71     }
72 }