3 FSM: il throttle e' a posto
7 2 LED PWM ai lati con lampeggio alternato
8 2 LED PWM alle estremita ali
11 2 interrupts per th e alettone
17 * Vedere la calibrazione automatica
18 * Min e max a 1000 - 2000 per alettone
21 Aggiungere FSM per alettone: lampeggi alternati
28 // Instanziamo un LED fuori dal loop
29 Lampeggiatore left = 6;
30 Lampeggiatore right = 9;
31 Lampeggiatore codasx = 5;
32 Lampeggiatore codadx = 10;
39 // Variabili per interrupt 0 si PIN 2
40 volatile unsigned int chValue2 = 1500; // Valore computato
41 volatile unsigned int chStart2 = 1500; // Inizio rilevamento
43 // Variabili per interrupt 1 su PIN 3
44 volatile unsigned int chValue3 = 1500; // Valore computato
45 volatile unsigned int chStart3 = 1500; // Inizio rilevamento
47 // Variabili per autocalibrazione 0
48 const byte chPin2 = 2; // PIN per la calibrazione alettone
49 int mid_point2 = 1500;
51 // Variabili per autocalibrazione 1
52 const byte chPin3 = 3; // PIN per la calibrazione
53 int mid_point3 = 1000;
58 unsigned long pausa = 2000; // Pausa per la transizione durante gli stati 2, 4 della FSM
61 const byte chPin = A1; // PIN su cui e' collegato il canale
62 unsigned int chIn = 1500; // Valore catturato
63 long unsigned chStamp = 0; // Timestamp per
64 unsigned int freq = 400 ; // Ogni quanti millisecondi leggere il valore
65 unsigned long currentMillis; // timestamp reference per millis per tutto il loop
66 const int soglia = 1500; // soglia per scatto toggle a 2 posizioni
67 unsigned long FSM_lastMillis = 0 ; // Timestamp per la FSM degli alettoni
70 // FSM gestione interruttore luci
71 enum { // Stati della FMS
73 toOff, // Trans On -> Off
75 toOn // Trans OFF -> On
79 // I PINs vengono impostati dal constructor al momento
80 // della dichiarazione dell'ogetto.
84 // HI -> LOW --> LOW -> HI
85 // per avere 2 LED che lampeggiano alternativamente
86 // Funzione relativa a calibrazione con pulsein:
87 mid_point2 = calibraTrim(chPin2) ; // Calibrazione del TRIM attivo sul canale
88 //mid_point3 = calibraTrim(chPin3) ; // Calibrazione del TRIM attivo sul canale
89 attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168
90 attachInterrupt(1, chRise3, RISING); // PIN 3 su 328p / 168
97 currentMillis = millis(); // Timestamp per tutto il loop
101 // Lettura ailerons channel ogni 200ms
102 if (currentMillis - chStamp >= freq) {
104 chIn = pulseIn(chPin, HIGH, 25000);
105 if (chIn != 0 && chIn > 1000 && chIn <2000) {
106 // get only resonable values
107 chStamp = currentMillis ;
119 FSM_lastMillis = currentMillis;
124 // Interruttore on / off
129 if (chValue3 < 1050) {
140 else if (chValue3 > 1900) {
141 // Throttle al massimo: LED laterali lampeggiano a caso,
142 // Sotto luminosita' a caso
143 caso = random(30, 250) ;
152 thrBit = map(chValue3,1050, 1900, 0, 255);
153 codasx.Blink(1220 - 4 * thrBit );
154 codadx.Blink(1220 - 4 * thrBit );
155 left.Blink(map(chValue2,1000,2000,200,800 ));
156 right.Blink(map(chValue2,1000,2000,800,200 ));
168 if (chIn > soglia && currentMillis - pausa > FSM_lastMillis ) {
170 } else if (chIn <= soglia) {
179 pcodasx.lDown(pausa);
180 pcodadx.lDown(pausa);
182 if (chIn <= soglia && currentMillis - pausa > FSM_lastMillis ) {
184 } else if (chIn > soglia) {
189 Serial.print("PIN2: ");
190 Serial.print(chValue2);
191 Serial.print(" -base: ");
192 Serial.print(mid_point2);
194 Serial.print(" |-| PIN3:");
195 Serial.print(chValue3);
196 Serial.print(" -base: ");
197 Serial.print(mid_point3);
198 Serial.print(" switch: ");
199 Serial.println(chIn);
210 attachInterrupt(0, chFall2, FALLING);
215 attachInterrupt(0, chRise2, RISING);
216 chValue2 = micros() - chStart2;
220 attachInterrupt(1, chFall3, FALLING);
225 attachInterrupt(1, chRise3, RISING);
226 chValue3 = micros() - chStart3;