Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1
    Just muddling through...


    John UK's Avatar
    Join Date
    May 2012
    Location
    United Kingdom
    Posts
    25
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Exclamation Code help for beginner...

    I have written the following code to run an Arduino mega board with mixed switches and annunciator lights:




    int CodeIn;// used on all serial reads
    int KpinNo;
    int Koutpin;
    String Exit,Stall,Over,Fuel,Park;
    int Exiti,Stalli,Overi,Fueli,Parki;


    String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;


    void setup()
    {
    Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";

    for (int KoutPin = 2; KoutPin < 70; KoutPin++)// Get all the pins ready for "Keys"
    {
    pinMode(KoutPin, INPUT);
    digitalWrite(KoutPin, HIGH);
    }
    Serial.begin(115200);
    }


    void loop() {
    {KEYS();} //Check the "keys" section


    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 EQUALS(){ // The first identifier was "="
    //Do something
    }


    void LESSTHAN(){ // The first identifier was "<"
    CodeIn = getChar(); // Get another character
    switch(CodeIn) {// Now lets find what to do with it

    case 'v'://Found the second identifier
    Exit = "";
    Exit += getChar();
    Exiti = Exit.toInt(); // convert it to an integer (Thanks Phill)
    if (Exiti != 0){digitalWrite(4, HIGH);}else{digitalWrite(4, LOW);}
    break;

    case 'a':
    Park = "";
    Park += getChar();
    Parki = Park.toInt(); // convert it to an integer (Thanks Phill)
    if (Parki != 0){digitalWrite(2, HIGH);}else{digitalWrite(2, LOW);}
    break;

    case 'M':
    Fuel = "";
    Fuel += getChar();
    Fuel += getChar();
    Fuel += getChar();
    Fueli = Fuel.toInt(); // convert it to an integer (Thanks Phill)
    if (Fueli > 6){digitalWrite(5, HIGH);}else{digitalWrite(5, LOW);}
    break;

    case 'S':
    Stall = "";
    Stall += getChar();
    Stalli = Stall.toInt(); // convert it to an integer (Thanks Phill)
    if (Stalli != 0){digitalWrite(3, HIGH);}else{digitalWrite(3, LOW);} //Do something
    break;
    //etc etc etc
    }
    }


    void QUESTION(){ // The first identifier was "?"
    CodeIn = getChar(); // Get another character
    switch(CodeIn) {// Now lets find what to do with it
    case 'A'://Found the second identifier
    Over = "";
    Over += getChar();
    Overi = Over.toInt(); // convert it to an integer (Thanks Phill)
    if (Overi != 0){digitalWrite(6, HIGH);}else{digitalWrite(6, LOW);}
    break;

    }
    }


    void SLASH(){ // The first identifier was "/" (Annunciator)
    //Do something
    }
    void KEYS()
    {
    Kstringnewstate = "";
    for (int KpinNo = 2; KpinNo < 70; KpinNo++){
    KpinStateSTR = String(digitalRead(KpinNo));
    KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 2));
    if (KpinStateSTR != KoldpinStateSTR)
    {
    if (KpinNo != 13){
    Serial.print ("D");
    if (KpinNo < 10) Serial.print ("0");
    Serial.print (KpinNo);
    Serial.println (KpinStateSTR);
    }
    }
    Kstringnewstate += KpinStateSTR;
    }
    Kstringoldstate = Kstringnewstate;
    }

    The switches work but the lights do not. I have the lights on pins 2 to 6 and the switches on 14 and above.

    I have ticked all the boxes in the Extractions tabs in Link2FS_Multi, successfully complied the code and uploaded it to the board.

    Can anyone tell Me where I am going wrong? It is driving me nuts!

    J.

  2. #2
    75+ Posting Member fsx-cockpit's Avatar
    Join Date
    Jan 2014
    Location
    germany
    Posts
    81
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Code help for beginner...

    FS9 or FSX?

    the LED with resistor and they do?
    Nothing gets me down - until my girlfriend say " Darling...please...garbage!" Homepage: www.fsx-cockpit.com

  3. #3
    Just muddling through...


    John UK's Avatar
    Join Date
    May 2012
    Location
    United Kingdom
    Posts
    25
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Code help for beginner...

    Quote Originally Posted by fsx-cockpit View Post
    FS9 or FSX?
    FSX

    Quote Originally Posted by fsx-cockpit View Post
    the LED with resistor and they do?
    All working and tested.

    J.

  4. #4
    75+ Posting Member fsx-cockpit's Avatar
    Join Date
    Jan 2014
    Location
    germany
    Posts
    81
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Code help for beginner...

    post please what Link2FS Monitor you send....
    Nothing gets me down - until my girlfriend say " Darling...please...garbage!" Homepage: www.fsx-cockpit.com

  5. #5
    Just muddling through...


    John UK's Avatar
    Join Date
    May 2012
    Location
    United Kingdom
    Posts
    25
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Code help for beginner...

    Quote Originally Posted by fsx-cockpit View Post
    post please what Link2FS Monitor you send....
    Not sure what you mean. can you clarify please?

  6. #6
    75+ Posting Member fsx-cockpit's Avatar
    Join Date
    Jan 2014
    Location
    germany
    Posts
    81
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Code help for beginner...

    I want see what Link2FS send to ardu...
    Nothing gets me down - until my girlfriend say " Darling...please...garbage!" Homepage: www.fsx-cockpit.com

  7. #7
    500+ This must be a daytime job Jim NZ's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    764
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Code help for beginner...

    You've done well John ,, except you have fallen for the oldest trick in the trick book :)

    Your code looks fine except you haven't thought of the configuration of the Arduino ,,, getting it set up for what pins are outputs and what pins are inputs.

    Replace all your "void setup" with this ,,,,,
    It will make the pins 2 to 13 all outputs for the LED's ,,, and also the pins 14 to 69 all inputs for the buttons etc.

    Code:
    void setup()
    {
    Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
    
    for (int KoutPin = 14; KoutPin < 70; KoutPin++)//sets up the input pins
    {
    pinMode(KoutPin, INPUT);
    digitalWrite(KoutPin, HIGH);
    }
    for (int KoutPin =2; KoutPin <= 13; KoutPin++)//sets up the output pins
      {  
        pinMode(KoutPin, OUTPUT);
        digitalWrite(KoutPin, LOW);  
      }  
    
    Serial.begin(115200);
    }

    Now you have configured the setup ,, you need to alter the "Keys" void to only read the configured input pins.
    All you need to do is ,,,,
    In "Keys" void there are 2 "2"s ,,, change both 2's to a "14" ,,, as that is the lowest pin number that is configured as an input.

    Your done :) :)

    I never tested your or my (above) code but that oversight jumped out at me.

    One other thing ,,, the park brake is "q" and not an "a"

    Good luck ,,, Jim
    www.jimspage.co.nz/intro.htm
    All this and Liz still loves me ! !

  8. #8
    Just muddling through...


    John UK's Avatar
    Join Date
    May 2012
    Location
    United Kingdom
    Posts
    25
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Code help for beginner...

    Thanks for your help Jim. I am a great fan of what you have done.

    I can see where I went wrong in the set up section.

    My 'Keys' code looks like this now:

    void KEYS()
    {
    Kstringnewstate = "";
    for (int KpinNo = 14; KpinNo < 70; KpinNo++){
    KpinStateSTR = String(digitalRead(KpinNo));
    KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 14));
    if (KpinStateSTR != KoldpinStateSTR)
    {
    if (KpinNo != 13){
    Serial.print ("D");
    if (KpinNo < 10) Serial.print ("0");
    Serial.print (KpinNo);
    Serial.println (KpinStateSTR);
    }
    }
    Kstringnewstate += KpinStateSTR;
    }
    Kstringoldstate = Kstringnewstate;
    }
    Is that correct?

    One other thing....

    What is the purpose of the Keys line:-

    if (KpinNo < 10) Serial.print ("0");
    Its the only bit of the code that I don't fully understand.

    Many thanks,

    John.

  9. #9
    500+ This must be a daytime job Jim NZ's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    764
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Code help for beginner...

    Yep that look pretty cool John ,, all you had to do with your original "Key" code there was to change both the 2's into a 14.

    That line you asked about is like this ,,,
    My program looks for a key code "identifier" within the first 3 characters ie.
    D120
    "D" tells it that it's a "Key" code.
    The 12 is the pin number.
    The 0 is the pins state.

    All's fine with the above until the Arduino wants to send the state of a single character pin number ,,, ie pin's 2 thru to 9

    So for (say) pin 5 the Arduino code needs to send D050.
    If the leading 0 wasn't there (ie D50) my program would just ignore it.

    Keep at it John ,, your doing fine ,,, Jim
    www.jimspage.co.nz/intro.htm
    All this and Liz still loves me ! !

  10. #10
    Just muddling through...


    John UK's Avatar
    Join Date
    May 2012
    Location
    United Kingdom
    Posts
    25
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Code help for beginner...

    Thanks Jim. It all makes sense now.

    Your site has been a great help to me and given me hours of fun building panels and getting them to work!

    Regards,

    John.

Page 1 of 2 12 LastLast