3 Pilotare un LED RGB in base al canale degli alettoni:
4 Questo sketch usa la funzione pulseIn(),
5 per versione con interrupts vedere esempio successivo.
7 = 3 stati + 2 transizioni:
13 * clean up magic numbers
18 #define dEBUG // Cambiare in DEBUG per il debug
21 unsigned long currentMillis; // timestamp reference per millis per tutto il loop
24 RGBLed ailerons(9,10,7,255); // Common Cat
27 Lampeggiatore sxLamp(10); // Lampeggiatore
28 Lampeggiatore dxLamp(9); // Lampeggiatore
31 // Variabili per lettura canale servo
32 int ail ; // Valore per ailerons
33 int ailIn ; // Valore rilevato del 4 Ch della RX
34 unsigned long ailTimer ; // millis per ail
37 // FSM gestione alettoni
38 enum { // Stati della FMS
40 sxin, // transizione a sx
42 dxin, // transizione a dx
47 unsigned long FSM_lastMillis = 0 ; // Timestamp per la FSM degli alettoni
48 unsigned long pausa = 1000; // Pausa per la transizione durante gli stati 2, 4 della FSM
50 // Variabili per interrupt 0 si PIN 2: Throttle
51 volatile unsigned int chValue2 = 1500; // Valore computato
52 volatile unsigned int chStart2 = 1500; // Inizio rilevamento
54 // Variabili per interrupt 1 su PIN 3: Ailerons
55 volatile unsigned int ail = 1500; // Valore computato
56 volatile unsigned int mid_point = 1560; // Inizio rilevamento
59 // Variabili per autocalibrazione 1
60 const byte chPin3 = 3; // PIN per la calibrazione
61 int mid_point3 = 1000;
64 int mid_point = 1560 ; // centro del segnale, trimmato nel setup
65 const int deviation = 50 ; // deviazione dal punto medio
66 //per entrare nello stato successivo dal centro
68 ///////////////////////////////////////////////////////////
77 // Funzione relativa a calibrazione:
78 mid_point = calibraTrim(ailPin) ; // + LED di servizio per monitor calibrazione
82 currentMillis = millis(); // Timestamp per tutto il loop
90 if (ail > mid_point + deviation + deviation /3) {
91 // extra margine per avere un po' di gioco
93 FSM_lastMillis = currentMillis;
95 else if (ail < mid_point - deviation - deviation / 3) {
97 FSM_lastMillis = currentMillis ;
104 if (currentMillis - pausa > FSM_lastMillis ) {
112 if (ail < mid_point + deviation) {
115 else if (ail < mid_point - deviation) {
116 FSM_lastMillis = currentMillis;
124 if (currentMillis - pausa > FSM_lastMillis ) {
132 if (ail > mid_point - deviation) {
135 else if (ail > mid_point + deviation) {
136 FSM_lastMillis = currentMillis;
142 Serial.print("ailIn: ");
144 Serial.print("\tail: ");
146 Serial.print("\t ailstate:");
147 Serial.println(ailstate);
152 attachInterrupt(0, chFall2, FALLING);
157 attachInterrupt(0, chRise2, RISING);
158 chValue2 = micros() - chStart2;
162 attachInterrupt(1, chFall3, FALLING);
167 attachInterrupt(1, chRise3, RISING);
168 ail = micros() - chStart3;