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