]> git.piffa.net Git - aerei/blob - esempi/base_th_3stati/base_th_3stati.ino
RGB ailerons prototipo: trasformare con millis
[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 ; // Throttle
24 int thrIn ;
25 byte caso;
26
27 void setup() {
28   // I PINs vengono impostati dal constructor al momento
29   // della dichiarazione dell'ogetto.
30
31   right.Invert() ;  // Opzionale: inverte l'ordine del lampeggio da
32   // HI -> LOW --> LOW -> HI
33   // per avere 2 LED che lampeggiano alternativamente
34
35   randomSeed(analogRead(0));
36 }
37
38 void loop() {
39   // Lettura CH3
40   thrIn = pulseIn(thrPin, HIGH, 25000);
41   thr = constrain(map(thrIn, 983, 2000, 0, 255), 0, 255) ;
42
43 // Gestione throttle
44   if (thr > 0 && thr < 15) {
45     // IDLE
46
47     rpwm.UD(2000);
48     lpwm.UD(2000);
49     sotto.lDown(1500);
50   } else if (thr < 245) {
51     // Throttle medio
52
53     right.Blink(1120 - 4 * thr );
54     left.Blink(1120 - 4 * thr );
55     sotto.lSet(thr);   // Luminosita' proporzionale al throttle
56   } else {
57     // Throttle al massimo: LED laterali lampeggiano a caso,
58     // Sotto luminosita' a caso
59
60     caso = random(20, 240) ;
61     right.Swap();
62     left.Swap();
63     sotto.lSet(caso);
64     delay(caso);
65   }
66 }