]> git.piffa.net Git - aerei/blob - esempi/state_throttle/state_throttle.ino
common update, reorder
[aerei] / esempi / state_throttle / state_throttle.ino
1 /* Throttle state machine
2
3 Idle, throttle e averburner effect basati sono sul canale throttle.   
4
5
6 */
7
8 #include <common.h>
9
10 enum  { // Stati della FMS
11   idle,    // Throttle a zero
12   normal,  // Th normale
13   full,    // Th massimo
14 } state  ;
15
16 // Due LED con lampeggio alternato:
17 Lampeggiatore right = 3;
18 Lampeggiatore left = 5;
19 Pwm motor = 7;
20
21 const byte thrPin = A3;
22 byte thr ; // Valore a 8bit per il throttle
23 int thrIn ; // Valore rilevato del 3 Ch della RX 
24
25
26
27 void setup() {
28   left.Invert() ; // Parte da stato invertito rispetto al default
29   randomSeed(analogRead(0));
30 }
31
32 void loop() {
33
34  // Lettura Throttle channel: FAKE con un potenziometro
35  //thrIn = analogRead(3);
36  //thr = constrain(thrIn / 4 , 0, 255) ;
37
38  // Lettura Throttle channel
39  thrIn = pulseIn(thrPin, HIGH, 25000);
40  thr = constrain(map(thrIn, 983, 2000, 0, 255), 0, 255) ; // 983 potrebbe campbiare con 
41            // con un altra ricevente, fare una calibrazione nel caso.
42
43  // FMS dispatcher
44  if ( thr < 10 ) {
45    state = idle ;
46  } else if ( thr > 245 ) {
47    state = full ;
48  } else {
49    state = normal ;
50  }
51
52  switch (state) {
53    case idle:
54        // Lampeggia i lati fissi e fa un PWD Up/Down col motore
55      right.Blink();
56      left.Blink();
57      motore.UD(1000);
58      break;
59
60    case normal:
61      // right e left lampeggiano alternativamente in base al th,
62      // il motore e' acceso con correzione di luminosita' in proporzione al th
63      right.Blink(1120 - 4 * thr ); // lampeggio piu' rapido in base al Th
64      left.Blink(1120 - 4 * thr );
65      motore.lSet(thr);
66      break;
67
68    case full:
69      // lampeggi e PWM a caso
70      right.Swap();
71      left.Swap();
72      motore.lSet(random(0,255);
73      delay(random(20, 100));
74      break;
75   }
76