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