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