]> git.piffa.net Git - aerei/blob - aerei/yak/interrupts/interrupts.ino
Yak
[aerei] / aerei / yak / interrupts / interrupts.ino
1 /* Yak 980 di A.
2
3    Prototipo:
4
5 OUTPUT:
6    1 PWM motore: 10
7    2 LED ai lati con lampeggio alternato
8    1 LED in coda lampeggio a freq doppia
9
10 INPUT:
11
12    * Thtottle: PIN 2
13    * Ailerons: PIN 3
14
15 Notes: abbondante usi di map().
16 NO FSM.
17
18 */
19
20 #include <common.h>
21 #define dEBUG
22
23 // Variabili per interrupt 0 si PIN 2
24 volatile unsigned int chValue2 = 1500; // Valore computato
25 volatile unsigned int chStart2 = 1500; // Inizio rilevamento
26
27 // Variabili per interrupt 1 su PIN 3
28 volatile unsigned int chValue3 = 1500; // Valore computato
29 volatile unsigned int chStart3 = 1500; // Inizio rilevamento
30
31 // Variabili per autocalibrazione 0
32 const byte chPin2 = 2; // PIN per la calibrazione
33 int mid_point2 = 980;
34
35 // Variabili per autocalibrazione 1
36 const byte chPin3 = 3; // PIN per la calibrazione
37 int mid_point3 = 1500;
38
39 // Instanziamo un LED fuori dal loop
40 Lampeggiatore left = 4;
41 Lampeggiatore right = 11;
42 Lampeggiatore coda = 6;
43 Pwm motore = 10;
44 Lampeggiatore stato = 13;
45 byte pins[] = {  // PIN dei LED che compongono la sequenza
46         7,8,9 
47 }; 
48 Sequenza seq = Sequenza(pins,3);
49
50 void setup() {
51 left.Invert();
52     // Funzione relativa a calibrazione con pulsein:
53     mid_point3 =  calibraTrim(chPin3) + 8; // La calibrazione in genere non serve
54     // su throttle, il valore minimo e' costante e non c'e' TRIM
55
56     attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168
57     attachInterrupt(1, chRise3, RISING); // PIN 3 su 328p / 168
58
59 } ;
60
61
62 void loop() {
63 stato.Blink(); // Onboard LED per vedere se la scheda e' accesa
64
65   right.Blink(map(chValue3,980,2020,800,200));
66   left.Blink(map(chValue3,980,2020,200,800));
67   coda.Blink(chValue2 /4);
68   motore.lSet((chValue2 -980) / 4);
69   seq.Update(map(chValue2,980,2000,300,40));
70
71 }
72
73 // Functions
74 void chRise2() {
75     attachInterrupt(0, chFall2, FALLING);
76     chStart2 = micros();
77 }
78
79 void chFall2() {
80     attachInterrupt(0, chRise2, RISING);
81     chValue2 = micros() - chStart2;
82 }
83 // Seconod iterrupt
84 void chRise3() {
85     attachInterrupt(1, chFall3, FALLING);
86     chStart3 = micros();
87 }
88
89 void chFall3() {
90     attachInterrupt(1, chRise3, RISING);
91     chValue3 = micros() - chStart3;
92 }