Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 22

Thread: 3 way toggle switch

  1. #11
    75+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    83

    Re: 3 way toggle switch

    Quote Originally Posted by BuzziBi View Post
    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");}
    I did try it but the middle stage of flaps ("C17027") would not work.

    It's got me stumped .... ad that's not hard.

    Could it be more of a hardware problem...dodgy card....dodgy wiring etc. I have wired it up correctly I am using a toggle switch for both gear and flaps. I have each end of the toggle switch going to the necessary pins and the middle pins wired to ground.

  2. #12
    150+ Forum Groupie BushPilotWannabe's Avatar
    Join Date
    Jan 2014
    Location
    Alberta, Canada
    Posts
    203

    Re: 3 way toggle switch

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

    Track value of KpinNo through your sketch.
    ---CYXD ----- TWR --- GND ------ Closed
    ILS-- NDB -- 119.1 -- 121.9 ---- 11/2013

  3. #13
    75+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    83

    Re: 3 way toggle switch

    Quote Originally Posted by BushPilotWannabe View Post
    if (KpinNo == 11 && KpinStateSTR == "1" && KpinNo == 12 && KpinStateSTR == "1"){Serial.println ("C17057");}

    Track value of KpinNo through your sketch.
    When you say track the value, what specifically am I looking for? Is it the 1 or 0. Sorry I need it in crayon.

    I appreciate your help

  4. #14
    150+ Forum Groupie BuzziBi's Avatar
    Join Date
    Mar 2013
    Location
    Norway
    Posts
    276

    Re: 3 way toggle switch

    If I say something wrong now, someone must feel free to correct me!
    But as I understand it:
    When you press a button, the
    KpinStateSTR == "0" and you serialprint whatever you want to.
    When you release the button, the KpinStateSTR goes back to "1", because of the:
    Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
    So when neither 11 or 12 is pressed, the KpinStateSTR should be "1" on both pinnumbers, and should send "C17027".

    Why the
    if (KpinNo == 11 && KpinStateSTR == "1" && KpinNo == 12 && KpinStateSTR == "1"){Serial.println ("C17027");}
    did not work, I can not explain.
    Maby it must be:
    if ((KpinNo == 11 && KpinStateSTR == "1") && (KpinNo == 12 && KpinStateSTR == "1")){Serial.println ("C17027");}
    (Notise the multiple parenthesis)
    Did you try serial monitor when testing to see what is going on?
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  5. #15
    75+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    83

    Re: 3 way toggle switch

    Quote Originally Posted by BuzziBi View Post
    If I say something wrong now, someone must feel free to correct me!
    But as I understand it:
    When you press a button, the
    KpinStateSTR == "0" and you serialprint whatever you want to.
    When you release the button, the KpinStateSTR goes back to "1", because of the:
    Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
    So when neither 11 or 12 is pressed, the KpinStateSTR should be "1" on both pinnumbers, and should send "C17027".

    Why the
    if (KpinNo == 11 && KpinStateSTR == "1" && KpinNo == 12 && KpinStateSTR == "1"){Serial.println ("C17027");}
    did not work, I can not explain.
    Maby it must be:
    if ((KpinNo == 11 && KpinStateSTR == "1") && (KpinNo == 12 && KpinStateSTR == "1")){Serial.println ("C17027");}
    (Notise the multiple parenthesis)
    Did you try serial monitor when testing to see what is going on?
    Just to clarify it does work. The flap lever works perfectly in all positions, the problem I am getting is that for some reason when my flaps are either fully up or fully down and I select my landing gear up or down it also moves my flaps to the middle position. Here is the code. so somehow ("C17027") is being read from my landing gear.

    I have just checked the serial monitor which has confirmed that whenever I select gear up or down I am also getting ("C17027").

    Both

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

    and

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

    will only work ("C17000") and (C"17059") but not the middle.





    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");} //Sets flaps to UP position
            if (KpinNo == 11, 12){if (KpinStateSTR == "1" ){Serial.println ("C17027");}else {Serial.println ("C000");}}
            if (KpinNo == 12 && KpinStateSTR == "0"){Serial.println ("C17059");} //Sets flaps to DOWN 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

  6. #16
    150+ Forum Groupie BuzziBi's Avatar
    Join Date
    Mar 2013
    Location
    Norway
    Posts
    276

    Re: 3 way toggle switch

    Awesome! Back to start, in other words. Where is the fun when averything goes smooth?

    My first thought, is to move the flapswitch out of the void INPUTS.
    Try to set the switch up in void OTHER, make the switch serialprint in 1. and 3. position.
    When the switch 11 and 12 is LOW, store a value.
    Something like:
    int switch11val
    if 11 is HIGH, serialprint "C17000" , switch11val = 0
    else if 11 is LOW, switch11val = 1
    Same thing with switch 12
    When both switch11val and switch12val ==1, serialprint the middle position "C17027"

    I did something simular with a homemade 3 button lcd menu, with submenus, for a dieselengine to display rpm, pressures and temps.
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  7. #17
    75+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    83

    Re: 3 way toggle switch

    Thank you, I will have a play. It is weird though that what happens with one pin would affect another.

    Failing that I will need more than one card to complete the sim, could I not just put them on separate cards?

  8. #18
    150+ Forum Groupie BushPilotWannabe's Avatar
    Join Date
    Jan 2014
    Location
    Alberta, Canada
    Posts
    203

    Re: 3 way toggle switch

    Quote Originally Posted by Marc1980 View Post
    When you say track the value, what specifically am I looking for? Is it the 1 or 0. Sorry I need it in crayon.

    I appreciate your help
    It is tough learning two different skills that are mutually dependent. Same problem with PCB board CADs. Much screaming and head banging. Learn one computer language and the others come easily. You just have to find a reference free of industry-standard jargon.

    First are we still talking about a SPST on/off/on toggle switch? And has it arrived? If not what are you using?

    Tracking: Print out the code you want to check and go line by line writing down the value stored in the variable you are checking (KpinNo).

    Hugh
    ---CYXD ----- TWR --- GND ------ Closed
    ILS-- NDB -- 119.1 -- 121.9 ---- 11/2013

  9. #19
    75+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    83

    Re: 3 way toggle switch

    Ok, I have had a play with my spare nano card and out the flaps and indications on one card and my gear lever on the other card and it works fine. I think that will be the easiest thing for me to do.

    If anyone does think they have figured it out I would be curious to know how a toggle switch on pin 9 & 10 would move something attached to pin 11 & 12. Anyway... thank you for all of your help I do appreciate the time yo have given. Ill be back within the next day or so. There are couple of things I'm not sure about but I will have a go and if I cant do it Ill holler.
    Thanks again.
    Kind regards
    Marc

  10. #20
    150+ Forum Groupie BuzziBi's Avatar
    Join Date
    Mar 2013
    Location
    Norway
    Posts
    276

    Re: 3 way toggle switch

    Quote Originally Posted by Marc1980 View Post
    Failing that I will need more than one card to complete the sim, could I not just put them on separate cards?
    Not sure what you ment, but. You can take the 11 and 12 pin out of the "void INPUTS" by changing the
    for (int KpinNo = 9; KpinNo < 14; KpinNo++)
    to
    for (int KpinNo = 9; KpinNo < 11; KpinNo++)
    I believe you could remove the
    if (KpinNo != 13) now, and use the pin 13 to other things.
    Have not tryed it though.
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

Page 2 of 3 FirstFirst 123 LastLast