X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=esempi%2Fstate_throttle%2Fstate_throttle.ino;fp=esempi%2Fstate_throttle%2Fstate_throttle.ino;h=c2636427a2d2b3fefdd76e51db89e7a2275f3dcd;hb=ad6370083abcb50cbedafd46e5b6cd1a20a078d9;hp=0000000000000000000000000000000000000000;hpb=2c5cfa0f982eb622c051cf24d9efbe1bbd3203f6;p=aerei diff --git a/esempi/state_throttle/state_throttle.ino b/esempi/state_throttle/state_throttle.ino new file mode 100644 index 0000000..c263642 --- /dev/null +++ b/esempi/state_throttle/state_throttle.ino @@ -0,0 +1,76 @@ +/* Throttle state machine + +Idle, throttle e averburner effect basati sono sul canale throttle. + + +*/ + +#include + +enum { // Stati della FMS + idle, // Throttle a zero + normal, // Th normale + full, // Th massimo +} state ; + +// Due LED con lampeggio alternato: +Lampeggiatore right = 3; +Lampeggiatore left = 5; +Pwm motor = 7; + +const byte thrPin = A3; +byte thr ; // Valore a 8bit per il throttle +int thrIn ; // Valore rilevato del 3 Ch della RX + + + +void setup() { + left.Invert() ; // Parte da stato invertito rispetto al default + randomSeed(analogRead(0)); +} + +void loop() { + + // Lettura Throttle channel: FAKE con un potenziometro + //thrIn = analogRead(3); + //thr = constrain(thrIn / 4 , 0, 255) ; + + // Lettura Throttle channel + thrIn = pulseIn(thrPin, HIGH, 25000); + thr = constrain(map(thrIn, 983, 2000, 0, 255), 0, 255) ; // 983 potrebbe campbiare con + // con un altra ricevente, fare una calibrazione nel caso. + + // FMS dispatcher + if ( thr < 10 ) { + state = idle ; + } else if ( thr > 245 ) { + state = full ; + } else { + state = normal ; + } + + switch (state) { + case idle: + // Lampeggia i lati fissi e fa un PWD Up/Down col motore + right.Blink(); + left.Blink(); + motore.UD(1000); + break; + + case normal: + // right e left lampeggiano alternativamente in base al th, + // il motore e' acceso con correzione di luminosita' in proporzione al th + right.Blink(1120 - 4 * thr ); // lampeggio piu' rapido in base al Th + left.Blink(1120 - 4 * thr ); + motore.lSet(thr); + break; + + case full: + // lampeggi e PWM a caso + right.Swap(); + left.Swap(); + motore.lSet(random(0,255); + delay(random(20, 100)); + break; + } +