]> git.piffa.net Git - aerei/blob - aerei/zeta/zeta_5_state/zeta_5_state.ino
common update, reorder
[aerei] / aerei / zeta / zeta_5_state / zeta_5_state.ino
1 /* Zeta test
2  *  le funzioni finali sono da controllare
3
4
5 */
6
7 #include <common.h>
8 enum states_available { // Stati della FMS
9   idle,    // Throttle a zero
10   normal,  // Th normale
11   full,    // Th massimo
12 };
13 states_available state  ;
14
15 // Due LED con lampeggio alternato:
16 Lampeggiatore right = 3;
17 Lampeggiatore left = 5;
18 const byte rtail = 6;
19 const byte ltail = 9;
20 const byte thrPin = A3;
21 byte thr ;
22 int thrIn ;
23
24 Pwm rwhite = 3;
25 Pwm lwhite = 5;
26 Pwm rtp = 6 ;
27 Pwm ltp = 9 ;
28
29
30 void setup() {
31   left.Invert() ; // Parte da stato invertito rispetto al default
32   pinMode(rtail, OUTPUT);
33   pinMode(ltail, OUTPUT);
34   pinMode(thrPin, INPUT);
35  // Warning: serial breaks PWM
36    Serial.begin(9600);
37
38   randomSeed(analogRead(0));
39 }
40
41 void loop() {
42
43   thrIn = pulseIn(thrPin, HIGH, 25000);
44   thr = constrain(map(thrIn, 983, 2000, 0, 255), 0, 255) ;
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 rwhite.Up(200);
58 lwhite.Up(200);
59 ltp.Up(200);
60 rtp.Up(200);
61       break;
62
63     case normal:
64       // Due LED con lampeggio alternato:
65       right.Blink(1120 - 4 * thr );
66       left.Blink(1120 - 4 * thr );
67       analogWrite(rtail, thr);
68       analogWrite(ltail, thr);
69       break;
70       
71     case full:
72       // Due LED con lampeggio alternato:
73       right.Blink(1120 - 4 * thr );
74       left.Blink(1120 - 4 * thr );
75       digitalWrite(rtail, !digitalRead(rtail));
76       digitalWrite(ltail, !digitalRead(ltail));
77       delay(random(20, 100));
78       break;
79   }
80
81   
82   Serial.println(thrIn);
83   Serial.print("\t thr:");
84   Serial.print(thr);
85   Serial.print("\t state:");
86   Serial.println(state);
87
88 }