Results 1 to 7 of 7
  1. #1
    25+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    71
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    1 step forward 2 steps back

    Hello,

    I don't know what I find more frustrating golf or arduino coding? So I have slowly been building my most of the code I will need for my planned cockpit. It have spent time ironing out any issues an for the most part it was all working well. I have since had a play with mobiflight (I'm not saying that mobiflight is the problem) but when I loaded my scripts back onto the card I am suddenly plagued with problems I thought I had fixed months ago.

    I have stripped my code all the way back just to include my inputs. The script below will compile and load but when I push the buttons my audio panel button being switched on flashes like it is getting switched on and off really fast. Is this a debounce problem?

    Any help would be great
    thanks in advance
    Marc

    Code:
    int CodeIn;// used on all serial readsint KpinNo; 
    int Koutpin;
    
    
    String KoldpinStateSTR, KpinStateSTR, Kstringnewstate, Kstringoldstate;
    
    
    
    
    void setup() 
    {
      Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
    
    
      for (int KoutPin = 2; KoutPin <=12; KoutPin++)// Get all the pins ready for simconnect codes and "Keys"(all inputs)  
      {
        pinMode(KoutPin, INPUT);
        digitalWrite(KoutPin, HIGH); 
        } 
    
    
     Serial.begin(115200);   
    }
    
    
    void loop() {
      {INPUTS();} //Check the Simconnect and "keys" section for any input pins
      }
    
    
     
    void INPUTS(){  // Simconnect codes and "Keys" section
      Kstringnewstate = "";
      for (int KpinNo = 2; KpinNo <=12; KpinNo++){ //set this to the input pins. (pins 10 to 19)
        KpinStateSTR = String(digitalRead(KpinNo)); //read the state of the pin
        KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 2));// gets the 'old' state of the pin from string
        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 == 12 && KpinStateSTR == "0"){Serial.println ("A45");} //Toggles Comm1
            if (KpinNo == 11 && KpinStateSTR == "0"){Serial.println ("A46");} //Toggles Comm2
            if (KpinNo == 10 && KpinStateSTR == "0"){Serial.println ("A47");} //Toggle Comm1/2
            if (KpinNo == 9 && KpinStateSTR == "0"){Serial.println ("A48");} //Toggle Nav1
            if (KpinNo == 8 && KpinStateSTR == "0"){Serial.println ("A49");} //Toggle Nav2
            if (KpinNo == 7 && KpinStateSTR == "0"){Serial.println ("A52");} //Toggle ADF
         
          }
             Kstringnewstate += KpinStateSTR;
      }
      Kstringoldstate = Kstringnewstate;
    }
    }

  2. #2
    75+ Posting Member
    Join Date
    Jun 2016
    Location
    127.0.0.1
    Posts
    97
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1 step forward 2 steps back

    (accidental double post)

  3. #3
    75+ Posting Member
    Join Date
    Jun 2016
    Location
    127.0.0.1
    Posts
    97
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1 step forward 2 steps back

    Keys need about 10ms to settle. Your code has only one read buffer (previous vs current value) which to work would require a check at least after every 5ms. Instead, your loop function calls INPUT() immediately after it finishes, thousands of times between those 5ms, catching all bouncing values it sees.

    To fix this you can add a delay(5); in your loop(). This would block the arduino from doing anything else during those 5ms.

    Why do you use strings? It doesn't make sense and adds code and memory complexity that is not needed.

    A simpler idea of what I assume you wanted to do https://gist.github.com/anonymous/5f...371ba73a5f2c65

    (removed code from the post and added to a gist because it is displayed terribly here)

  4. #4
    150+ Forum Groupie
    Join Date
    Nov 2013
    Location
    Evansville, Indiana
    Posts
    233
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1 step forward 2 steps back

    spilot,
    You'll see a lot of String use here because its what Jim's original examples use.
    Also a lot of folks are just learning to code for the first time with Arduino.
    I dig your array example, I might have to roll that into my code.
    As for delay(5), I use millis() with a time comparison to debounce my switches/buttons, if needed.
    ~Fess
    I will only ever be, half the Geek that I wished I was.
    TheGeekForge.Com

  5. Thanks BushPilotWannabe thanked for this post
  6. #5
    75+ Posting Member
    Join Date
    Jun 2016
    Location
    127.0.0.1
    Posts
    97
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1 step forward 2 steps back

    I apologize if I came out as arrogant, it wasn't my intention.

    Most arduino boards have 8bit microcontrollers, terribly limited in memory and stack compared to microprocessors but super fast at manipulating 8bit numbers, reading and writing them from ports, etc. Strings are more complex to handle internally, especially when manipulated, it can exhaust memory very fast creating seemingly random behaviour/bugs that will be very difficult to figure out and debug. That's why I was surprised to see them being used for this case.

    But that being said, if a string is 10 times easier for a person to comprehend then I'd be in favour of using it assuming performance was not a factor (for this simple example it is not, but add a lot more things and it might). Still I don't see how that "compare current string value with old string value char at position x minus 2" is more clear than dealing with an array in the first place, but it might just be me.

    Comparing time in the loop function with millis() is obviously a much better way of dealing with delays than blocking everything with delay(). There is a good example of the technique here.

  7. Thanks Marc1980 thanked for this post
    Likes Fess_ter liked this post
  8. #6
    25+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    71
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1 step forward 2 steps back

    Thanks all for taking the time to respond. Spiliot, you did not come across as arrogant, I appreciate people taking the time to help out. As Fess said for a lot of people including me, Jim's examples are the first taste they get of Arduino. Unfortunately I don't have a massive amount of spare time to study code and develop a deeper understand it so I confess I am a copy and paster.

    Do you have an array sketch that you have used with link2fs that you could share on here. Again I understand the principal of using a delay and also why it is better to use millis() rather than delay() but am not sure how to incorporate them into the script.

    Many thanks
    Marc

  9. #7
    75+ Posting Member
    Join Date
    Jun 2016
    Location
    127.0.0.1
    Posts
    97
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1 step forward 2 steps back

    I'm sorry Marc, I don't have anything to share. Link2fs/Fsuipc and (up to a point) arduinos, were too limited for my interest, code abilities and use case, so I ended up designing my own electronics for the fun of it.

    But I've got good experience with arduinos and I'll be happy to help with problems when I can.