Results 1 to 8 of 8
  1. #1
    25+ Posting Member
    Join Date
    Jun 2014
    Location
    france
    Posts
    42
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Post pushbutton state change

    Hello to all ,
    Im little lost the way to make a pushbutton as a toggle and write it into the sketck at the input section .
    I wanted to pushbutton work as a toggle ON on first press, OFF on second press so sending 2 different simconnect code ,
    If someone can show me because i get lost add it there :

    stringnewstate = ""; for (int pinNo = 22; pinNo < 40; pinNo++){
    pinStateSTR = String(digitalRead(pinNo));
    oldpinStateSTR = String(stringoldstate.charAt(pinNo - 22));
    if (pinStateSTR != oldpinStateSTR){

    if (KpinNo == 40) .....
    Thanks

  2. #2
    75+ Posting Member



    Join Date
    Sep 2013
    Location
    California, USA
    Posts
    109
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: pushbutton state change

    Xflight,
    Converting digital reads to string variables makes the comparisons hard to follow and probably why you are getting lost. It isn’t necessary to perform your logic using strings, digital reads are boolean (true or false). It will also help keep things clear if you name variables with their function. Here is an example for a pushbutton to toggle landing lights:


    boolean ldgLights;
    boolean btn_ldgLights;
    boolean btn_old_ldgLights;

    void loop() {
    btn_old_ldgLights = btn_ldgLights;
    btn_ldgLights = digitalRead(22);
    if ((btn_old_ldgLights == false)&&(btn_ldgLights == true)) {
    ldgLights = !ldgLights; //(when button goes from not pressed to pressed, ldgLights will change state)
    if (ldgLights == true) Serial.println("C431");
    else Serial.println("C430");
    delay(10); // pause after first time going high to avoid contact bounce
    } //end if
    } //end void loop



  3. Thanks xflight38 thanked for this post
  4. #3
    25+ Posting Member
    Join Date
    Jun 2014
    Location
    france
    Posts
    42
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: pushbutton state change

    Hi ,
    Thanks Steve it help me a lot .
    code work great and i can add as many push i need

    Quote Originally Posted by SteveL View Post
    Xflight,
    Converting digital reads to string variables makes the comparisons hard to follow and probably why you are getting lost.
    I used JIM code example input section , can you tell me why does Jim make this comparaison ??( to learn )


    Its a little hard to add it into an existing Void pushbutton i used , ill post the code when i m back at home °-)

  5. #4
    75+ Posting Member



    Join Date
    Sep 2013
    Location
    California, USA
    Posts
    109
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: pushbutton state change

    Good question. It’s because the protocol for serial communication transmits characters using a 2 byte ASCII code. It does not recognize numbers as numbers, only as numeric characters.
    If you issue the command: Serial.println(“3”); you will transmit the character “3”.
    If you issue the command: Serial.println(3); you will transmit a variable named “3”, and if the function can’t find a variable named “3” to transmit, it will give you an error message.
    So, if you want to transmit a number you first have to convert it to a string of characters which is then decoded back to a number at the receiving end.


  6. Thanks xflight38 thanked for this post
    Likes BuzziBi liked this post
  7. #5
    25+ Posting Member
    Join Date
    Jun 2014
    Location
    france
    Posts
    42
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: pushbutton state change

    Thanks Steve for that Great Answer ..
    So In our example the transmit number is the input pin number ? (right ?..)

    Well here is where im lost so now : I used this code and add
    boolean ldgLights, it work great but it change state for every others Pushbutton also ..i've try to do into an array ( boolean ldgLights[NUMBUTTONS]; ) but that didnt work ..maybe a boolean array cant be size of NUMBUTTONS ??..i should did it wrong for sure ..
    I.E (I've keep the name ldgLights for the exemple )
    //===============================
    byte buttons[] = {40,41,42,43}; //define the buttons that we'll use.
    #define NUMBUTTONS sizeof(buttons) //determine how big the array up above is, by checking the size
    //track if a button is just pressed, just released, or 'currently pressed'
    byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
    byte previous_keystate[NUMBUTTONS], current_keystate[NUMBUTTONS];
    #define DEBOUNCE 10 // how many ms to debounce, 5+ ms is usually plenty
    boolean ldgLights;
    //===============================
    void check_switches()
    {
    static byte previousstate[NUMBUTTONS];
    static byte currentstate[NUMBUTTONS];
    static long lasttime;
    byte index;
    if (millis() < lasttime) {
    // we wrapped around, lets just try again
    lasttime = millis();
    }
    if ((lasttime + DEBOUNCE) > millis()) {
    // not enough time has passed to debounce
    return;
    }
    // ok we have waited DEBOUNCE milliseconds, lets reset the timer
    lasttime = millis();
    for (index = 0; index < NUMBUTTONS; index++) {
    justpressed[index] = 0; //when we start, we clear out the "just" indicators
    justreleased[index] = 0;
    currentstate[index] = digitalRead(buttons[index]); //read the button
    if (currentstate[index] == previousstate[index]) {
    if ((pressed[index] == LOW) &&(currentstate[index] == LOW)) {
    // just pressed
    justpressed[index] = 1;ldgLights =!ldgLights;
    }
    else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
    justreleased[index] = 1; // just released
    }
    pressed[index] = !currentstate[index]; //remember, digital HIGH means NOT pressed
    }
    previousstate[index] = currentstate[index]; //keep a running tally of the buttons
    }
    }
    byte thisSwitch_justPressed() {
    byte thisSwitch = 255;
    check_switches(); //check the switches & get the current state
    for (byte i = 0; i < NUMBUTTONS; i++) {
    current_keystate[i]=justpressed[i];
    if (current_keystate[i] != previous_keystate[i]) {
    if (current_keystate[i]) thisSwitch=i;
    }
    previous_keystate[i]=current_keystate[i];
    }
    return thisSwitch;
    }

    //=================================================

    void INPUTS() {
    byte thisSwitch=thisSwitch_justPressed();
    switch(thisSwitch)
    {
    case 0: // SPD INTV

    if (ldgLights==true) Serial.println ("ZZ700190");else Serial.println ("ZZ700191");
    break;
    }
    Anyway thanks Steve °=)

  8. #6
    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: pushbutton state change

    This might do it the same way and by using existing text we learners only have to insert 11 lines of code into Jim's Multi_Radiohead_Annun_Mega.ino.

    General description of INPUTKEYS() function as I see it to help newcomers who are now where I was some time ago. The level of each input pin is recorded as a series of characters either '0' or '1' "0" meaning Low and "1" meaning High for each input pin in order. On the next pass through the function, each pin is examined in order and the level is put in String (?) format so as to compare it to the character stored on the last pass. If they match the current character is added to the end of a new string (stringnewstate) and the sketch progresses on to the next loop. If the two do not match, the input is re-examined and if it meets a more demanding comparision the code that follows is executed. Again a character is added to "stringnewstate" before moving on to the next loop. On completion of the last loop "stringnewstate" is complete and archived as "stringoldstate". This is a function running in real time and the likelihood of any change in any given pass is unlikely, so this procedure keeps time spent in the function to a minimum and then returns to void loop().

    Instruction “oldpinStateSTR += String(stringoldstate.charAt(pinNo - 10));” is key to making the output toggle. The above condition acts only when digital input D27 is grounded before "stringoldstate" is updated to include character (pinNO - 10) equal to '0'.

    The following will only toggle from "one" to "zero" the first time through because once situationA is equal to zero, line two immediately reverses line one, and situationA is equal to "one" only as long as it takes to process the second instruction.

    int situationA ;
    if(situationA==0) situationA=1 ;
    if(situationA==1) situationA=0 ;

    But if we park the result of the above into a second (holding) variable the second if statement does not see the results of the first.

    int situationA, holding ;
    if(situationA==0) holding=1 ;
    if(situationA==1) holding=0 ;
    situationA = holding ;


    [QUOTE]



    /* PushOn/PushOff_MEGA.ino
    Where possible the code duplicates Jim's code for Multi_Radiohead_Annun_Mega from Link2fs_Multi_Radio (Nav Radio Project).zip. Insert the the code between the comments starting "NEW " into INPUTPINS() function ahead of line if (pinNo > 26){ // start of "Keys" bit. And change "26" to "27" in the above quoted line. Finally declare lines 2 & 3 to in with variables of their type.
    */

    String pinStateSTR, oldpinStateSTR, stringnewstate, stringoldstate = "11111111111111111111111111111111111111111" ;
    String lightStatus = "OFF" ; // The boss fired the last pilot that left the lights on.
    int lightHolding ;


    void setup()
    {
    for (int doutPin = 10; doutPin <= 53; doutPin++)
    { pinMode(doutPin, INPUT) ;
    digitalWrite(doutPin, HIGH) ;
    }
    Serial.begin(115200) ;
    }

    void loop()
    {
    INPUTPINS() ;
    }


    void INPUTPINS()
    { stringnewstate = "" ;
    for( int pinNo = 10; pinNo <= 53; pinNo++)
    { pinStateSTR = String(digitalRead(pinNo)) ;
    oldpinStateSTR = "" ;
    oldpinStateSTR += String(stringoldstate.charAt(pinNo - 10)) ;
    if( pinStateSTR != oldpinStateSTR)
    {

    // NEW pushOn/pushOFF switch START
    if( pinNo == 27)
    { // delay(15) ; // not required with my tactile switch or very noisy push button.
    if( digitalRead(27 )== LOW && stringoldstate.charAt(pinNo - 10) == '1')
    { if( lightStatus == "OFF") { Serial.println("C431") ; lightHolding = 1 ; }
    if( lightStatus == "ON") { Serial.println("C430") ; lightHolding = 0 ; }
    if( lightHolding == 1) { lightStatus = "ON" ; }
    else { lightStatus = "OFF" ; }
    }
    }
    // NEW pushOn/pushOFF switch start ENDS

    }
    stringnewstate += pinStateSTR ;
    }
    stringoldstate = stringnewstate ;
    }// end of inputpins

    [QUOTE]
    Last edited by BushPilotWannabe; 11-02-2014 at 01:59 AM. Reason: Changed pin D8 to D7. 8's produced phantom happy faces. You had to be there to believe it.

  9. #7
    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: pushbutton state change

    Lots of time this winter to look over xflight38's approach. Working with String functions certainly is time consuming. About 2.4 ms to convert 43 digitalReads into characters, and just over 6 ms. to complete the above sketch during switch transition while the remainder of the Multi_Radio sketch takes @ 80 micro seconds. Again this is during a period of no input. And then you have a default 11 ms. delays between setting the cursor and writing anything to the LCD.

    How about using an unsigned long variable reported to be 64 bits long on the Ubuntu IDE as a register. Just found bitSet() and others in the playground to replace basic bitwise functions.
    Last edited by BushPilotWannabe; 11-02-2014 at 01:40 AM. Reason: Cannot get rid of those smiley faces. This editor hates me.

  10. #8
    25+ Posting Member
    Join Date
    Jun 2014
    Location
    france
    Posts
    42
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: pushbutton state change

    Quote Originally Posted by BushPilotWannabe View Post
    Lots of time this winter to look over xflight38's approach.
    LOL...
    Thanks for the code and explain it for noob im ...