]> git.piffa.net Git - aerei/blob - zeta_5_state_no_aserial/zeta_5_state_no_aserial.ino
pwm float
[aerei] / zeta_5_state_no_aserial / zeta_5_state_no_aserial.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 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 // Inserire necro delay
46 delay(10); // Se si abilita il serial debug
47             // togliere il delay
48
49   // FMS dispatcher
50   if ( thr < 10 ) {
51     state = idle ;
52   } else if ( thr > 245 ) {
53     state = full ;
54   } else {
55     state = normal ;
56   }
57
58   switch (state) {
59     case idle:
60       rwhite.UD(2);
61       lwhite.UD(2);
62       ltp.UD(2);
63       rtp.UD(2);
64       break;
65
66     case normal:
67       // Due LED con lampeggio alternato:
68       right.Blink(1120 - 4 * thr );
69       left.Blink(1120 - 4 * thr );
70       analogWrite(rtail, thr);
71       analogWrite(ltail, thr);
72       break;
73
74     case full:
75       // Due LED con lampeggio alternato:
76       right.Blink(1120 - 4 * thr );
77       left.Blink(1120 - 4 * thr );
78       digitalWrite(rtail, !digitalRead(rtail));
79       digitalWrite(ltail, !digitalRead(ltail));
80       delay(random(20, 100));
81       break;
82   }
83
84
85   //  Serial.println(thrIn);
86   //  Serial.print("\t thr:");
87   //  Serial.print(thr);
88   //  Serial.print("\t state:");
89   //  Serial.println(state);
90
91 }