Page 1 of 3 123 LastLast
Results 1 to 10 of 22
  1. #1
    75+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    77
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    3 way toggle switch

    Hi guys,

    I have ordered a 3 way toggle switch which will be my flap lever. It will have 3 positions up, approach and down. What is the best way to programme that? I have the ends wired to D8 & D9 and the center to ground. I did have a play with some if & else statements but it is not really working.

    Any thoughts as always would be great.

    Regards
    Marc

  2. #2
    150+ Forum Groupie BuzziBi's Avatar
    Join Date
    Mar 2013
    Location
    Norway
    Posts
    275
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 3 way toggle switch

    This setup can be difficult to get to work. First, the different aircraft types have different numbers of positions on its flaps. IE. Cessna C172SP has 4 positions (flaps fully retracted = 0, 10,20 and flaps fully extended=35, and Boeing 747-400 has 7 positions. If the switch you use has 3 positions and will be used for Cessna, you must go from 0 to 35 (for landing) by sending multiple "C14" and "C15" to extend/retract. I think you also must take into account that your sketchet must be able to monitor the position of the flaps at any given time. If you ie goes from 0 to position 1, and 2. position, and then back to 1, position. How many Serial.printlns have you sendt, and where are the flaps
    Personally, I created a simple flaps switch, using 2 micro switches. One tap at the handle, sends one Serial.println, up or down. Then use leds to monitor the position. This way I can use the switch at any aircraft.
    Attached Images Attached Images
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  3. #3
    150+ Forum Groupie


    BushPilotWannabe's Avatar
    Join Date
    Jan 2014
    Location
    Alberta, Canada
    Posts
    176
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 3 way toggle switch

    [EDIT] Hot Rod version.

    A delay is needed to debounce switch for this demo sketch. Extend to 4 mS. if needed and remove altogether if the code is inserted into a longer sketch

    char* flaps[4] = {"null", "FlapCodeDown", "FlapCodeUp", "FlapCodeApproach"};
    int oldFlapsPos = 0;

    void setup() {
    pinMode(8, INPUT_PULLUP);
    pinMode(9, INPUT_PULLUP);
    Serial.begin(115200);
    }

    void loop() {
    delay(1);
    int flapsPos = (digitalRead( + (digitalRead(9)*2));
    if (flapsPos != oldFlapsPos){
    Serial.println(flaps[flapsPos]);
    oldFlapsPos = flapsPos;
    }
    }
    Last edited by BushPilotWannabe; 09-13-2015 at 07:42 PM. Reason: Simplify and add lightness.
    ---CYXD ----- TWR --- GND ------ Closed
    ILS-- NDB -- 119.1 -- 121.9 ---- 11/2013

  4. #4
    75+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    77
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 3 way toggle switch

    Thanks guys for the reply. Link2fs does allow for any pre-defined flap setting. I think it's done as a percentage for example Serial.println("C17000") would be no flaps and ("C17100") would be full flaps. The aircraft I am doing is based on a bonanza which goes from up to approach to down. If I remember correctly that's 0, 27, 59.

    I am wondering though, the toggle switch I bought is a single pole switch so it means that the 1st stage of flaps is set when the switch is in the off/ground position. Is that even possible?

    Liking the flap switch, nice one.

  5. #5
    150+ Forum Groupie BuzziBi's Avatar
    Join Date
    Mar 2013
    Location
    Norway
    Posts
    275
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 3 way toggle switch

    Try if it can be done with:
    if D8 ==
    HIGH Serial.println C17000 (no flaps)
    if D8 && D9 == LOW Serial.println C17xxx (approach)
    if D9 == HIGH Serial.println C17100 (down)
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  6. #6
    75+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    77
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 3 way toggle switch

    Thanks guys,

    Try if it can be done with:
    if D8 ==
    HIGH Serial.println C17000 (no flaps)
    if D8 && D9 == LOW Serial.println C17xxx (approach)
    if D9 == HIGH Serial.println C17100 (down)
    To me this would be the easiest thing to do. I therefore tried this



    Code:
    void setup() {
    Serial.begin(115200);
    pinMode(11, INPUT_PULLUP);
    pinMode(12, INPUT_PULLUP);
    }
    
    
    void loop() {
    
    
    
    
      if (11 == HIGH) { Serial.println("C17000"); } 
    
    
        if (11 && 12 == LOW) {Serial.println("C17027");}
    
    
        if(12 == HIGH) {Serial.println("C17059");}
    That did not work. This is the code I am using at the moment for my gear and the flaps including indications. Is there a way I could tweak the Void inputs towards the bottom to allow for the middle/off position on the switch to Serial.println ("C17027")?



    Code:
    int CodeIn;// used on all serial reads
    int KpinNo; 
    int Koutpin;
    String flaps;
    
    
    String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;
    String gearN,gearL,gearR;
    int gearNi,gearLi,gearRi;
    
    
    void setup() 
    {
      Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
      
      for (int KoutPin = 9; KoutPin < 14; KoutPin++)// Get all the pins ready for simconnect codes and "Keys"(all inputs)  
      {
        pinMode(KoutPin, INPUT);
        digitalWrite(KoutPin, HIGH);  
      }
      pinMode(2, OUTPUT); // Flaps are in transition LED
      pinMode(3, OUTPUT); // Flaps are down 1st position LED
      pinMode(4, OUTPUT); // Flaps are down 2nd position LED
      pinMode(5, OUTPUT); // gear nose LED
      pinMode(6, OUTPUT); // gear left LED
      pinMode(7, OUTPUT); // gear right LED
      pinMode(8, OUTPUT); // gear in transition LED
     Serial.begin(115200);   
    }
    
    
    void loop() {
      {INPUTS();} //Check the Simconnect and "keys" section
      {OTHER();}// Check for "Other" things to do. (Non extraction stuff)
      if (Serial.available()) {
        CodeIn = getChar();
        if (CodeIn == '=') {EQUALS();} // The first identifier is "="
        if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<"
        if (CodeIn == '?') {QUESTION();}// The first identifier is "?"
        if (CodeIn == '/') {SLASH();}// The first identifier is "/" (Annunciators)
      }
    
    
    }
    
    
    char getChar()// Get a character from the serial buffer
    {
      while(Serial.available() == 0);// wait for data
      return((char)Serial.read());// Thanks Doug
    }
    void OTHER(){
    /* In here you would put code that uses other data that
    cant be put into an "extraction void" that references something else.
    Also in here you would put code to do something that was not
    relying on a current extraction trigger.
    */
    
    
    //Sort out the gear "in transition" LED
    int gearTot = (gearNi + gearLi + gearRi);
    if (gearTot == 300){digitalWrite(8, LOW);} 
    else if (gearTot < 1){digitalWrite(8, LOW);}
    else {digitalWrite(8, HIGH);}
    }
    
    
    void EQUALS(){      // The first identifier was "="
     CodeIn = getChar(); // Get the second identifier
      switch(CodeIn) {// Now lets find what to do with it
        case 'A'://Found the second identifier
           //Do something
        break;
         
        case 'B':
           //Do something
        break;
         
        case 'C':
           //Do something
        break;
        
             //etc etc etc
         }
    }
    
    
    void LESSTHAN(){    // The first identifier was "<"
    CodeIn = getChar(); // Get the second identifier
      switch(CodeIn) {// Now lets find what to do with it
        case 'A'://Found the second identifier "GearN"
          gearN = "";
          gearN += getChar();
          gearN += getChar();
          gearN += getChar();
          gearNi = gearN.toInt(); // convert it to an integer (Thanks Phill)
          if (gearNi > 99){digitalWrite(5, HIGH);}else{digitalWrite(5, LOW);}
          // See "OTHER" for more uses of gearni
        break;
         
        case 'B':
          gearL = "";
          gearL += getChar();
          gearL += getChar();
          gearL += getChar();
          gearLi = gearL.toInt(); // convert it to an integer (Thanks Phill)
          if (gearLi > 99){digitalWrite(6, HIGH);}else{digitalWrite(6, LOW);}
          // See "OTHER" for more uses of gearLi
        break;
         
        case 'C':
            gearR = "";
          gearR += getChar();
          gearR += getChar();
          gearR += getChar();
          gearRi = gearR.toInt(); // convert it to an integer (Thanks Phill)
          if (gearRi > 99){digitalWrite(7, HIGH);}else{digitalWrite(7, LOW);}
          // See "OTHER" for more uses of gearRi
        break;
    
    
        case 'G'://Found the second identifier ("G" Flaps position)
          flaps = "";
          flaps += getChar();
          flaps += getChar();
          flaps += getChar();
          int flapsInt = flaps.toInt(); // convert it to an integer (Thanks Phill)
          if (flapsInt > 0 && flapsInt < 29) {digitalWrite(2, HIGH);}
          else if (flapsInt >29 && flapsInt <57){digitalWrite(2,HIGH);}else {digitalWrite(2, LOW);}
          if (flapsInt == 29) {digitalWrite(3, HIGH);} else {digitalWrite(3, LOW);}
          if (flapsInt > 56) {digitalWrite(4, HIGH);} else {digitalWrite(4, LOW);}
           }
    }
    
    
    void QUESTION(){    // The first identifier was "?"
    CodeIn = getChar(); // Get the second identifier
      switch(CodeIn) {// Now lets find what to do with it
        case 'A'://Found the second identifier
           //Do something
        break;
         
        case 'B':
           //Do something
        break;
         
        case 'C':
           //Do something
        break;
           
             //etc etc etc
         }
    }
    void SLASH(){    // The first identifier was "/" (Annunciator)
      //Do something
    }
    void INPUTS() // Simconnect codes and "Keys" section
    {
      Kstringnewstate = "";
      for (int KpinNo = 9; KpinNo < 14; KpinNo++){ //set to the input pins. (pins 6 to 70 ,, change for Uno cards)
        KpinStateSTR = String(digitalRead(KpinNo)); 
        KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 9));// set to the first pin read (The 6)
        if (KpinStateSTR != KoldpinStateSTR)// checks if it's different to the last reading of that pinNo
        {
          if (KpinNo != 13){ // avoid using pin 13 as an input unless you know the tricks.
            if (KpinNo == 9 && KpinStateSTR == "0"){Serial.println ("C01");} //sets gear handle up
            if (KpinNo == 10 && KpinStateSTR == "0"){Serial.println ("C02");} //sets gear handle down
            if (KpinNo == 11 && KpinStateSTR == "0"){Serial.println ("C17000");} //Decrements flap handle position
            if (KpinNo == 12 && KpinStateSTR == "0"){Serial.println ("C17057");} //Increments flap handle position
            //  Add more here but remember to change the figure in the next line down. (the 12)
            if (KpinNo > 12){ //Change pinNo number to same as the highest one used for simconnect codes.(the 12)
            Serial.print ("D"); 
          if (KpinNo < 10) Serial.print ("0");
          Serial.print (KpinNo);
          Serial.println (KpinStateSTR);
            }//end of 'its pinNo is greater than 12' 
          }//end of 'its not pin 13'
        }//end of 'its different'       
        Kstringnewstate += KpinStateSTR;
      }//end of 'for' loop (read the pins)
      Kstringoldstate = Kstringnewstate;
    }//end of INPUTS void
    Thanks again

  7. #7
    150+ Forum Groupie BuzziBi's Avatar
    Join Date
    Mar 2013
    Location
    Norway
    Posts
    275
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 3 way toggle switch

    I am not able to test at this moment, but try with this.

    if (KpinNo == 11, 12 && KpinStateSTR == "1"){Serial.println ("C17027");}

    or

    if (KpinNo == 11 && KpinStateSTR == "1" && KpinNo == 12 && KpinStateSTR == "1"){Serial.println ("C17057");}

    Both compiles.
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  8. #8
    75+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    77
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 3 way toggle switch

    Thank you. Ill give it a try later and let you know how it goes.

  9. #9
    75+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    77
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 3 way toggle switch

    if (KpinNo == 11, 12 && KpinStateSTR == "1"){Serial.println ("C17027");}
    So I added this line and the flap handle works a treat. Thank you. But what is weird now is when I select my gear up or down it will always move my flaps to the middle position. I'm not sure how what is happening on pins 9 & 10 is affecting pins 11 & 12.

    Nearly there!
    Thanks again

  10. #10
    150+ Forum Groupie BuzziBi's Avatar
    Join Date
    Mar 2013
    Location
    Norway
    Posts
    275
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 3 way toggle switch

    I do not know what is going on
    Maybe someone else can explain this.
    Do you get the same result with the:
    if (KpinNo == 11 && KpinStateSTR == "1" && KpinNo == 12 && KpinStateSTR == "1"){Serial.println ("C17057");}
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

Page 1 of 3 123 LastLast