]> git.piffa.net Git - aerei/blob - aerei/zeta/zeta/zeta.ino
9d0b2fa0eee907dc5add8544aa0c12994667d797
[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     // Test iniziale dei LED per verifica contatti:
42   left.High();
43   right.High();
44   rgb.White();
45   rtp.Set(255);
46   ltp.Set(255);
47   delay(4000);
48 }
49
50 void loop() {
51
52
53   thrIn = pulseIn(thrPin, HIGH, 25000);
54   thr = constrain(map(thrIn, 983, 2000, 0, 255), 0, 255) ;
55   // Inserire necro delay
56   delay(10); // Se si abilita il serial debug
57   // togliere il delay
58
59   // FMS dispatcher
60   if ( thr < 10 ) {
61     state = idle ;
62   } else if ( thr > 245 ) {
63     state = full ;
64   } else {
65     state = normal ;
66   }
67
68   switch (state) {
69     case idle:
70     rgb.Green();
71     
72       rwhite.UD(2000);  // Utilizza il coseno
73       lwhite.UD(2000);  // Bisognerebbe evitare di calcolarlo 4 volte uguale
74       ltp.shift = 500;
75       ltp.lUp(1000);
76       rtp.lUp(1000);
77       break;
78
79     case normal:
80     rgb.Blue();
81       // Due LED con lampeggio alternato:
82       right.Blink(1120 - 4 * thr );
83       left.Blink(1120 - 4 * thr );
84       analogWrite(rtail, lum(thr));
85       analogWrite(ltail, lum(thr));
86       break;
87
88     case full:
89     rgb.Red();
90       pausa = random(30, 125);
91       // Due LED con lampeggio alternato:
92       right.Blink(1120 - 4 * thr );
93       digitalWrite(ltail, !digitalRead(ltail));
94       digitalWrite(3, !digitalRead(3));
95   
96
97       left.Blink(1120 - 4 * thr );
98       digitalWrite(rtail, !digitalRead(rtail));
99       digitalWrite(5, !digitalRead(5));
100       delay(pausa);
101       break;
102   }
103
104
105   //  Serial.println(thrIn);
106   //  Serial.print("\t thr:");
107   //  Serial.print(thr);
108   //  Serial.print("\t state:");
109   //  Serial.println(state);
110
111 }