]> git.piffa.net Git - aerei/blob - esempi/snippets/lettura_servo_interrupt_doppio/lettura_servo_interrupt_doppio.ino
Merge branch 'master' of kim.andreamanni.com:/home/git/aerei
[aerei] / esempi / snippets / lettura_servo_interrupt_doppio / lettura_servo_interrupt_doppio.ino
1
2 /* Lettura di 2 canali servo della RX con interrupts
3
4    Lettura tramite i due external interrupt ala Arduino
5    Utilizzabile solo sui PIN 2-3
6
7 */
8
9 #include <common.h>
10 #define DEBUG
11
12
13 // Variabili per interrupt 0 si PIN 2
14 volatile unsigned int chValue2 = 1500; // Valore computato
15 volatile unsigned int chStart2 = 1500; // Inizio rilevamento
16
17 // Variabili per interrupt 1 su PIN 3
18 volatile unsigned int chValue3 = 1500; // Valore computato
19 volatile unsigned int chStart3 = 1500; // Inizio rilevamento
20
21 // Variabili per autocalibrazione 0
22 const byte chPin2 = 2; // PIN per la calibrazione
23 int mid_point2 = 1500;
24
25 // Variabili per autocalibrazione 1
26 const byte chPin3 = 3; // PIN per la calibrazione
27 int mid_point3 = 1500;
28
29 void setup() {
30     // Funzione relativa a calibrazione con pulsein:
31     mid_point2 =  calibraTrim(chPin2) + 8 ; // Calibrazione del TRIM attivo sul canale
32     mid_point3 =  calibraTrim(chPin3) + 8; // La calibrazione in genere non serve
33     // su throttle, il valore minimo e' costante e non c'e' TRIM
34
35     attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168
36     attachInterrupt(1, chRise3, RISING); // PIN 3 su 328p / 168
37
38 #ifdef DEBUG
39 Serial.begin(9600); 
40 Serial.flush();
41 #endif
42 } ;
43
44 void loop() {
45 // Il loop fa solo debug
46 #ifdef DEBUG
47     Serial.print("PIN2: ");
48     Serial.print(chValue2);
49     Serial.print(" -base: ");
50     Serial.print(mid_point2);
51
52     Serial.print(" |-| PIN3:");
53     Serial.print(chValue3);
54     Serial.print(" -base: ");
55     Serial.println(mid_point3);
56     delay(200);
57 #endif
58 }
59
60 // ISRs:
61 void chRise2() {
62     attachInterrupt(0, chFall2, FALLING);
63     chStart2 = micros();
64 }
65
66 void chFall2() {
67     attachInterrupt(0, chRise2, RISING);
68     chValue2 = micros() - chStart2;
69 }
70 // Secondo iterrupt
71 void chRise3() {
72     attachInterrupt(1, chFall3, FALLING);
73     chStart3 = micros();
74 }
75
76 void chFall3() {
77     attachInterrupt(1, chRise3, RISING);
78     chValue3 = micros() - chStart3;
79 }