Page 1 of 4 1234 LastLast
Results 1 to 10 of 38
  1. #1
    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

    Question Arduino input & output

    I need help to split the input and output on the arduino mega! It was easy on the Uno card because the options were fewer.Now with a mega card I found it difficult

    Using Jim's pde it list ie like this:

    Quadrature quad1(8, 9); //added .. the RE is connected to pins 8 and 9
    LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

    void setup()

    for (int doutPin = 10; doutPin <= 19; doutPin++) //added.. changed the 8 to a 10,, no need to set it up for "keys" but we still need pin 10 setup as a digital pin.
    void loop() {

    for (int pinNo = 11; pinNo <= 19; pinNo++){ //added... changed the 8 to a 11 to stop "keys" reading the pins as well.
    pinStateSTR = String(digitalRead(pinNo));


    oldpinStateSTR = String(stringoldstate.charAt(pinNo - 11)); //added.. changed the 8 to 11 to keep pins string inactive for pins 8 and 9 and 10




    I will have a setup like this:
    Quadrature quad1(9, 10); //added .. the Heading RE is connected to pins 9 and 10
    Quadrature quad2(11, 12); //added .. the Altitude RE is connected to pins 11 and 12
    Quadrature quad3(13, 14); //added .. the QNH RE is connected to pins 13 and 14
    Lets say I dont wont to use 15 to 21 for now, but maby for future RE's
    LCD on:
    LiquidCrystal lcd1(8, 6, 5, 4, 3, 2);
    LiquidCrystal lcd2(8, 7, 5, 4, 3, 2);
    Pushbuttons from input 22 to 41
    LED's on output 42 to 53

    void setup()
    How do I now change this line?
    for (int doutPin = 10; doutPin <= 19; doutPin++) //added.. changed the 8 to a 10,, no need to set it up for "keys" but we still need pin 10 setup as a digital pin.

    void loop() {
    And this line?
    for (int pinNo = 11; pinNo <= 19; pinNo++){ //added... changed the 8 to a 11 to stop "keys" reading the pins as well.
    pinStateSTR = String(digitalRead(pinNo));
    oldpinStateSTR = String(stringoldstate.charAt(pinNo - 11)); //added.. changed the 8 to 11 to keep pins string inactive for pins 8 and 9 and 10
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  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: Arduino input & output

    One question in this context.
    Is there a difference, where in the "loop" I configures the various buttons and Led's
    I've been sitting for several days with this problem.
    I've got buttons to work, I have LED's to work, but in combination, I have failed.
    When I modified Jim's. "pde_inout_v4" I did'nt get it to work.
    But today I started with the starter2.pde and now it works!
    The only difference I can see is that the Buttons read is at the start of the loop in the starter2.pde
    but at the end in the pde_inout_v4.
    Or can it simply be that I have made an error I could'nt see?
    But seriously. I have occasionally pondered whether there is a difference!
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  3. #3
    150+ Forum Groupie
    Join Date
    Apr 2011
    Location
    United States of America
    Posts
    232
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Arduino input & output

    It doesn't matter. What matters is what happens to the variable until it is assigned a new value.

  4. #4
    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

    Question Re: Arduino input & output

    Stuck again!
    Need help to solve this!
    If I use the keys in pde_inout_v4, and I want to print a line on the display (LCD 16x2)
    every time the button (pinNo) 12 is LOW


    Let's say I want to print "STANDBY" on
    lcd.setCursor (8, 1); every time the button (pinNo) 12 is LOW

    I understand that Link2fs reads the button like: D121 or D120.

    So if the pinNo is 120, I would like to print "STANDBY" on the LCD 16x2, on cursor 8, line 1

    Suggestions?

    Code:
    // Now the bit for the "keys" input  stringnewstate = "";
      for (int pinNo = 8; pinNo <= 19; pinNo++){
      pinStateSTR = String(digitalRead(pinNo)); 
    
    
      oldpinStateSTR = String(stringoldstate.charAt(pinNo - 8));
     
     if (pinStateSTR != oldpinStateSTR)
         {
        Serial.print ("D"); 
        if (pinNo < 10) Serial.print ("0");
        Serial.print (pinNo);
        Serial.print (pinStateSTR);
        delay(11);
         }
     stringnewstate += pinStateSTR;
     }
     
    stringoldstate = stringnewstate;
      delay(11);
    }
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  5. #5
    150+ Forum Groupie
    Join Date
    Apr 2011
    Location
    United States of America
    Posts
    232
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Arduino input & output

    Does this help?

    if (pinNo == 12){
    if (pinStateSTR == "0"){
    lcd.setCursor(8,1);
    lcd.print ("STANDBY");
    }else{
    l//cd.setCursor(8,1);
    // lcd.print ("ACTIVE");

    }
    }

  6. #6
    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: Arduino input & output

    Quote Originally Posted by JMeller View Post
    Does this help?

    if (pinNo == 12){
    Unfortunately JMeller,
    I did not succeed with that one either.
    I wonder if you have to use something like:
    Code:
    if ((pinNo == 12) && (pinStateSTR == "0"))(
    then do action.......
    
    But I can not get it to work :evil:
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  7. #7
    150+ Forum Groupie
    Join Date
    Apr 2011
    Location
    United States of America
    Posts
    232
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Arduino input & output

    Hmmm. If you placed the code between the following brackets, there is not a reason it should not have worked... especially if it is sending the keystroke command to Link2FS.

    if (pinStateSTR != oldpinStateSTR)
    {
    Serial.print ("D");
    // place code somewhere in here
    }



    Do you have an LCD.clear() erasing it and then rewriting the lcd with other data?

    And, yes, you could use - if (pinNo == 12 && pinStateSTR == "0"){ // do something} either approach works the same.

    However, if you are not using "else"and you have a piece of code written after the - if (pinNo == 12 && pinStateSTR == "0"){ // do something} - to write onto the lcd in the same spot (8,1), it will overwrite the STANDBY text you had just told it to write. And since pinNo will equal 19 after it leaves the for-next loop, you cannot use - if (pinNo == 12 && pinStateSTR == "1"){ // do something} later in the void loop().

    I'm sure I covered things you already knew, but I wanted to cover the whole gamut for the readers.

  8. Thanks MLRUSSELL thanked for this post
  9. #8
    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: Arduino input & output

    Here comes the news .... I suppose you've already guessed it, I'm still working With the Transponder
    I have removed the "key,s bit from the starter pde", This one.
    Code:
      // Setup the pins as inputs as used by "Keys" (For the "Keys" program avoid actually using Pin 13 as an input ,, long story)
      for (int doutPin = 8; doutPin <= 19; doutPin++)  // if you have a Mega card and want to use all the pins ,, change the 19 to 69
        { 
       pinMode(doutPin, INPUT);
       digitalWrite(doutPin, HIGH);  
        }
    And now set up the buttons one by one. The Rotary Switch "Mode Selector" on the 47,48,49 and 50 pin. Here is the code If anyone is interested.
    Code:
    // This constant won't change:
    const int  buttonSBY = 47;    // the  Rotary Switch pin 
    const int  buttonTST = 48;    // the  Rotary Switch pin 
    const int  buttonON = 49;    // the  Rotary Switch pin 
    const int  buttonALT = 50;    // the  Rotary Switch pin 
    // Variables will change:
    int buttonState1 = 0;         // current state of the SBY  Rotary Switch
    int lastButtonState1 = 0;     // previous state of the SBY  Rotary Switch
    int buttonState2 = 0;         // current state of the TST  Rotary Switch
    int lastButtonState2 = 0;     // previous state of the TST  Rotary Switch
    int buttonState3 = 0;         // current state of the ON  Rotary Switch
    int lastButtonState3 = 0;     // previous state of the ON  Rotary Switch
    int buttonState4 = 0;         // current state of the ALT  Rotary Switch
    int lastButtonState4 = 0;     // previous state of the ALT  Rotary Switch
    int xx;  
    int pinState; 
    int pinNo;    
    int doutpin;  
    int j1;
    int j2;
    #include 
    LiquidCrystal lcd1(8, 6, 5, 4, 3, 2);
    LiquidCrystal lcd2(8, 7, 5, 4, 3, 2);
    String altit, altitold, apHeading, apHeadingold, Transponder, Transponderold; // String for Buttons
    // The QNH must be given a Key an made funcsionable via FSUIPC "Kohlsman" 
    String gearnose, gearnoseold, gearL, gearLold, gearR, gearRold, flaps, flapsold, AP, APold; //Strings for LED's
    String oldpinStateSTR, pinStateSTR, stringnewstate,stringoldstate; //String for read states
    
    void setup() {
     
      // initialize the Rotary Switch pins as a input:
      pinMode(buttonSBY, INPUT);
       pinMode(buttonTST, INPUT);
        pinMode(buttonON, INPUT);
         pinMode(buttonALT, INPUT); 
    //########## Initialize serial communication:
      Serial.begin(115200);
    //########## set up the 1.LCD's number of columns and rows:
     lcd1.begin(16, 2);
      // Print a message to the LCD.
      lcd1.setCursor(0, 0);
      delay (11);
      lcd1.print("");
      delay (11);
      lcd1.setCursor(0, 1);
      delay (11);
      lcd1.print("");
      delay (11);
    //########## set up the 2.LCD's number of columns and rows:
      lcd2.begin(16, 2);
      // Print a message to the LCD.
      delay (11);
      lcd2.print("");
      delay (11);
      lcd2.setCursor(0, 1);
      delay (11);
      lcd2.print("");
      delay (11);
      
      Serial.flush(); 
    }
    void loop() {
    
    //###Rotary Switch Read ######
    buttonState1 = digitalRead(buttonSBY);
       buttonState2 = digitalRead(buttonTST);
        buttonState3 = digitalRead(buttonON);
         buttonState4 = digitalRead(buttonALT);
    //### SBY Button
      if (buttonState1 != lastButtonState1) {
        if (buttonState1 == HIGH) {
     {   
       lcd2.setCursor(5,1);
         delay (11);
         lcd2.print("SBY");}
        } 
        else {
    {lcd2.clear(); 
    }
      }
    lastButtonState1 = buttonState1;
      }
    //### TST Button
      if (buttonState2 != lastButtonState2) {
        if (buttonState2 == HIGH) {
     {   
       lcd2.setCursor(5,1);
         delay (11);
         lcd2.print("TST");}
        } 
        else {
    {lcd2.clear(); 
    }
      }
    lastButtonState2 = buttonState2;
    }
    //### ON Button
      if (buttonState3 != lastButtonState3) {
        if (buttonState3 == HIGH) {
     {   
       lcd2.setCursor(5,1);
         delay (11);
         lcd2.print("ON");}
        } 
        else {
    {lcd2.clear(); 
    }
      }
    lastButtonState3 = buttonState3;
    }
    //### ALT Button
      if (buttonState4 != lastButtonState4) {
        if (buttonState4 == HIGH) {
     {   
       lcd2.setCursor(5,1);
         delay (11);
         lcd2.print("ALT");}
        } 
        else {
    {lcd2.clear(); 
    }
      }
    lastButtonState4 = buttonState4;
    }
    //### End of Rotary Switch Read ######
    }
    It looks like the solution on the LCD print line was this.
    Code:
    //### SBY Button
      if (buttonState1 != lastButtonState1) {
        if (buttonState1 == HIGH) {
     {   
       lcd2.setCursor(5,1);
         delay (11);
         lcd2.print("SBY");}
        } 
        else {
    {lcd2.clear(); 
    }
    What I do not understand, is that even though I have resistor between input and Ground, I have to use the (buttonstate == HIGH) in the code. Setting only "output" in void setup and "HIGH" in void loop, if not, it caused the lcd to pring while disconnected and remove print while Connected??? Still a newbie, I could not get your suggestion to work inside the "keys" code. I must admit, that you were right in your suspicions . The first attempts min code was outside. I'm still confused by "()" and "{}" Anyway, we have progress ....
    As it is now, so I set SQUAWK code with a arduino one modifyed to act as a usb keyborad, And using a mega card to deal with RE's, Buttons and Lcd's. It still remains to put everything together, but any case, now things are starting to function independently.
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  10. #9
    150+ Forum Groupie
    Join Date
    Apr 2011
    Location
    United States of America
    Posts
    232
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Arduino input & output

    Good to see you got it going, Roy.


    I studied your code. During which, I found excessive use of { & }. I'm not critisizing - I started out doing the same ... heck, we all did. So, in order to get you up and running faster than I did, I took the liberty to modify the code - your code - so you can easily pick out what is going on.


    Firstly, I declared three extra variables at the top - a string and 2 arrays; also deleted the oldbuttonState variables.

    Secondly, I deleted all of the delays in the lcd print; unless your specific chip needs them, they are not necessary.

    Third, I used the arrays to store the button states - newRead near the top of void loop(); oldRead at the bottom of void loop().

    Fourth, I compare the newRead array to the oldRead array to determine if an lcd2 clear is needed.

    Fifth, I removed as many {} that were not necessary and then positioned the lines of code to be easily followed at a glance. This method may not be for you, but I throw there it out as a possible alternative; Personally, I prefer this form - it allows me to easily scan down the code.

    Sixth, I created another loop called "LCD2print()". This allows you to place lengthy code which gets in the way of reading & comprehension out of site. Simply type "LCD2print();" in your code when you want that loop to be fetched. This also allows code which is frequently used to be fetched instead of being re-written all of the time.

    Code:
    #include LiquidCrystal.h (webpage removed greater-than & less-than signs)
    // This constant won't change:
    const int  buttonSBY = 47;    // the pin that the pushbutton is attached to
    const int  buttonTST = 48;    // the pin that the pushbutton is attached to
    const int  buttonON = 49;    // the pin that the pushbutton is attached to
    const int  buttonALT = 50;    // the pin that the pushbutton is attached to
    // Variables will change:
    int buttonState1 = 0;        // current state of the SBYbutton
    int buttonState2 = 0;        // current state of the TSTbutton
    int buttonState3 = 0;        // current state of the ONbutton
    int buttonState4 = 0;        // current state of the ALTbutton
    int xx;  
    int pinState; 
    int pinNo;    
    int doutpin;  
    int j1;
    int j2;
    
    
    LiquidCrystal lcd1(8, 6, 5, 4, 3, 2);
    LiquidCrystal lcd2(8, 7, 5, 4, 3, 2);
    
    
    // ++++  JM ADDED 
    String SWstate;  // used as Switch placement LCD text
    int newRead [4];  // declare newRead as an array w/ 4 slots
    int oldRead [4];  // declare oldRead as an array w/ 4 slots
    // +++++++++++
    
    
    String altit, altitold, apHeading, apHeadingold, Transponder, Transponderold; // String for Buttons
    // The QNH must be given a Key an made funcsionable via FSUIPC "Kohlsman" 
    String gearnose, gearnoseold, gearL, gearLold, gearR, gearRold, flaps, flapsold, AP, APold; //Strings for LED's
    String oldpinStateSTR, pinStateSTR, stringnewstate,stringoldstate; //String for read states
    
    
    void setup() {
       
    for (int doutPin = 54; doutPin <= 69; doutPin++)  
        { 
      pinMode(doutPin, OUTPUT);
      digitalWrite(doutPin, LOW);  
        }
        stringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111"; 
    //########## Setup the pins 21 to 25 as inputs for the REButtons 
      for (int doutPin = 21; doutPin <= 25; doutPin++)  
        { 
      pinMode(doutPin, INPUT);
      digitalWrite(doutPin, HIGH);  
        }  
      // initialize the button pin as a input:
      pinMode(buttonSBY, INPUT);
      pinMode(buttonTST, INPUT);
        pinMode(buttonON, INPUT);
        pinMode(buttonALT, INPUT); 
    //########## Initialize serial communication:
      Serial.begin(115200);
    //########## set up the 1.LCD's number of columns and rows:
      lcd1.begin(16, 2);
      // Print a message to the LCD.
      lcd1.setCursor(0, 0);
      lcd1.print("");
      lcd1.setCursor(0, 1);
      lcd1.print("");
      
    //########## set up the 2.LCD's number of columns and rows:
      lcd2.begin(16, 2);
      // Print a message to the LCD.
        lcd2.print("");
        lcd2.setCursor(0, 1);
        lcd2.print("");
       
      Serial.flush(); 
    }
    
    
    void loop() {
    //###Rotary Switch Read ######
    buttonState1 = digitalRead(buttonSBY);
    buttonState2 = digitalRead(buttonTST);
    buttonState3 = digitalRead(buttonON);
    buttonState4 = digitalRead(buttonALT);
    
    newRead [0] = buttonState1; // Store in newRead slot0
    newRead [1] = buttonState2; // Store in newRead slot1
    newRead [2] = buttonState3; // Store in newRead slot2
    newRead [3] = buttonState4; // Store in newRead slot3
    
    // Compare array slots to determine if LCD clear is needed
    for (int arrayNUM = 0; arrayNUM <= 3; arrayNUM++) {   // 0 based - 4 slots
      if (newRead [arrayNUM] != oldRead [arrayNUM]) //compare slot #s and clear LCD if needed
        {lcd2.clear(); break;}
    }
    
    //### SBY Button
      if (newRead [0] != oldRead [0] && buttonState1 == HIGH) 
      {SWstate = "SBY"; LCD2print();}
    
    
    //### TST Button
      if (newRead [1] != oldRead [1] && buttonState2 == HIGH) 
      {SWstate = "TST"; LCD2print();}
    
    
    //### ON Button
      if (newRead [2] != oldRead [2] && buttonState3 == HIGH) 
      {SWstate = "ON"; LCD2print();}
    
    
    //### ALT Button
      if (newRead [3] != oldRead [3] && buttonState4 == HIGH) 
      {SWstate = "ALT"; LCD2print();}
    
    oldRead [0] = buttonState1; // Store in oldRead slot0
    oldRead [1] = buttonState2; // Store in oldRead slot1
    oldRead [2] = buttonState3; // Store in oldRead slot2
    oldRead [3] = buttonState4; // Store in oldRead slot3
    //### End of Rotary Switch Read ######
    } // End void loop
    
    void LCD2print(){
       lcd2.setCursor(5,1);
       lcd2.print(SWstate);
    }  // End LCD2print

    Hope you found some of this to be beneficial.


    As for the input, ground, and the lcd - you may have to go into a bit more detail; I could not quite understand.

  11. #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: Arduino input & output

    Thank JMeller.
    It was a much cleaner way to set it all up!
    I have to let it sink in a bit, and try and understand what is happening.
    It's a completely different way of doing it, but I might be able to understand it after some studying.
    The code works any case!
    The strange thing about this, is that I become more addicted on the way. Thinking coding all the time
    What I meant by the "input, ground, and lcd, was: I did set the input (47 to 50) "HIGH" with a resistor.
    Normally you mention this in the "void setup" If I did that, it did not work.
    Still asking if the input is "HIGH" or "LOW" in the "void loop" it now Works? Strange?
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

Page 1 of 4 1234 LastLast