]> git.piffa.net Git - aerei/blob - aerei/zeta/zeta/zeta.ino
45e1f2e3e9adc6af5f57bf47fc42b1400b448c26
[aerei] / aerei / zeta / zeta / zeta.ino
1 /* Zeta test
2
3 Nota: rovedere i PWM per come calcolano le pause:
4 non possono schendere sotto a 1ms
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 pausa ;
22 byte thr ;
23 int thrIn ;
24
25 Pwm rwhite = 3;
26 Pwm lwhite = 5;
27 Pwm rtp = 6 ;
28 Pwm ltp = 9 ;
29
30 RGBLed rgb(7,8,4,255);
31
32 void setup() {
33   left.Invert() ; // Parte da stato invertito rispetto al default
34   pinMode(rtail, OUTPUT);
35   pinMode(ltail, OUTPUT);
36   pinMode(thrPin, INPUT);
37   // Warning: serial breaks PWM
38   // Serial.begin(9600);
39
40   randomSeed(analogRead(0));
41 }
42
43 void loop() {
44
45
46   thrIn = pulseIn(thrPin, HIGH, 25000);
47   thr = constrain(map(thrIn, 983, 2000, 0, 255), 0, 255) ;
48   // Inserire necro delay
49   delay(10); // Se si abilita il serial debug
50   // togliere il delay
51
52   // FMS dispatcher
53   if ( thr < 10 ) {
54     state = idle ;
55   } else if ( thr > 245 ) {
56     state = full ;
57   } else {
58     state = normal ;
59   }
60
61   switch (state) {
62     case idle:
63     rgb.Red();
64       rwhite.UD(2000);  // Utilizza il coseno
65       lwhite.UD(2000);  // Bisognerebbe evitare di calcolarlo 4 volte uguale
66       ltp.UD(2000);
67       rtp.UD(2000);
68       break;
69
70     case normal:
71     rgb.Green();
72       // Due LED con lampeggio alternato:
73       right.Blink(1120 - 4 * thr );
74       left.Blink(1120 - 4 * thr );
75       analogWrite(rtail, lum(thr));
76       analogWrite(ltail, lum(thr));
77       break;
78
79     case full:
80     rgb.Blue();
81       pausa = random(30, 125);
82       // Due LED con lampeggio alternato:
83       right.Blink(1120 - 4 * thr );
84       digitalWrite(ltail, !digitalRead(ltail));
85       digitalWrite(3, !digitalRead(3));
86   
87
88       left.Blink(1120 - 4 * thr );
89       digitalWrite(rtail, !digitalRead(rtail));
90       digitalWrite(5, !digitalRead(5));
91       delay(pausa);
92       break;
93   }
94
95
96   //  Serial.println(thrIn);
97   //  Serial.print("\t thr:");
98   //  Serial.print(thr);
99   //  Serial.print("\t state:");
100   //  Serial.println(state);
101
102 }