]> git.piffa.net Git - aerei/blob - aerei/zeta/zeta_prot/zeta_prot.ino
7243b22c7805a11b8f8c4427adb138a95b50b9a5
[aerei] / aerei / zeta / zeta_prot / zeta_prot.ino
1 /* Zeta prototipo
2
3    Sketch da breadboard, il throttle e' simulato con un potenziometro
4    o con una RX.
5
6 */
7
8 #include <common.h>
9 enum  { // Stati della FMS
10   idle,    // Throttle a zero
11   normal,  // Th normale
12   full,    // Th massimo
13 } state  ;
14
15 // Due LED con lampeggio alternato:
16 Lampeggiatore right = 13;
17 Pwm rpwm = 3;
18
19 // Variabili
20 const byte thrPin = A3;
21 byte thr ;
22 int thrIn ;
23
24
25
26 void setup() {
27   Serial.begin(9600);
28   pinMode(A3, INPUT);
29   randomSeed(analogRead(0));
30 }
31
32 void loop() {
33
34   // Utilizzando un potenziometro
35   //  thrIn = analogRead(3);
36   //  thr = constrain(thrIn / 4 , 0, 255) ;
37
38   // Utilizzando una RX
39   thrIn = pulseIn(thrPin, HIGH, 25000);
40   // Hint: thrIn andrebbe calibrato son un Serial.write
41   thr = constrain(map(thrIn, 960, 2000, 0, 255), 0, 255);
42
43   // FMS dispatcher
44   if ( thr < 10 ) {
45     state = idle ;
46   } else if ( thr > 245 ) {
47     state = full ;
48   } else {
49     state = normal ;
50   }
51
52   switch (state) {
53     case idle:
54       rpwm.UD(2000);
55       right.Blink();
56       break;
57
58     case normal:
59       // Due LED con lampeggio alternato:
60       right.Blink(1120 - 4 * thr );
61       rpwm.lSet(thr);
62       break;
63
64     case full:
65       digitalWrite(3, HIGH);
66       rpwm.Set(0);
67       right.Swap();
68       delay(50);
69       rpwm.Set(255);
70       right.Swap();
71       delay(50);
72       break;
73   }
74
75
76   Serial.print(thrIn);
77   Serial.print("\t thr:");
78   Serial.print(thr);
79   Serial.print("\t state:");
80   Serial.println(state);
81   //  delay(200);
82 }