Results 1 to 2 of 2
  1. #1
    New Member
    Join Date
    Nov 2015
    Location
    Iatly
    Posts
    4
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Please, help me with this sketch!!

    Hello everyone!
    I'm trying to create a "generic desktop cockpit".
    I was able to command twelve buttons with analog input.
    Code:
    int analogPin = 0; 
    int buttonValue = 0; 
    
    void setup() {
      Serial.begin(115200);  
      Serial.println("Ready.");
    }
    
    void loop() {
      buttonValue = analogRead(analogPin);
      if (buttonValue > 1000)     {Serial.print("1"); while (buttonValue > 1000) {buttonValue = analogRead(analogPin); } }
      else if (buttonValue > 893) {Serial.print("2");while (buttonValue > 893) {buttonValue = analogRead(analogPin);} }
      else if (buttonValue > 806) {Serial.print("3");while (buttonValue > 806) {buttonValue = analogRead(analogPin);} }
      else if (buttonValue > 692) {Serial.print("4");while (buttonValue > 692) {buttonValue = analogRead(analogPin);} }
      else if (buttonValue > 634) {Serial.print("5");while (buttonValue > 634) {buttonValue = analogRead(analogPin);} }
      else if (buttonValue > 548) {Serial.print("6");while (buttonValue > 548) {buttonValue = analogRead(analogPin);} } 
      else if (buttonValue > 464) {Serial.print("7");while (buttonValue > 464) {buttonValue = analogRead(analogPin);} } 
      else if (buttonValue > 378) {Serial.print("8");while (buttonValue > 378) {buttonValue = analogRead(analogPin);} } 
      else if (buttonValue > 294) {Serial.print("9"); while (buttonValue > 294) {buttonValue = analogRead(analogPin);} } 
      else if (buttonValue > 209) {Serial.print("10");while (buttonValue > 209) {buttonValue = analogRead(analogPin);} } 
      else if (buttonValue > 124) {Serial.print("11");while (buttonValue > 124) {buttonValue = analogRead(analogPin);} } 
      else if (buttonValue > 60)  {Serial.print("12");while (buttonValue > 60) {buttonValue = analogRead(analogPin);} } 
      delay(25);
    }
    I would like to add it to the rest of the code.
    Code:
    /* 
    
    */
    #include "math.h"
    #include "Quadrature.h"
    Quadrature quad1(34, 35);  // HDG  Acc
    Quadrature quad2(36, 37);  // CRS1 Acc
    Quadrature quad3(38, 39);  // CRS2 Acc 
    Quadrature quad4(40, 41);  // Alt Acc
    Quadrature quad5(42, 43);  // SPD Acc
    Quadrature quad6(44, 45);  // VS Acc
    Quadrature quad7(46, 47);  // NAV1 1MhZ  normali
    Quadrature quad8(48, 49);  // NAV1 25Khz 
    Quadrature quad9(50, 51);  // NAV2 1MhZ  normali
    Quadrature quad10(52, 53);  // NAV2 25Khz 
    Quadrature quad11(54, 55);  // COM1 1MhZ  normali
    Quadrature quad12(56, 67);  // COM1 25Khz 
    Quadrature quad13(58, 59);  // COM2 1MhZ  normali
    Quadrature quad14(60, 61);  // COM2 25Khz 
    
    
    
    long Xolde1;// variabili encoder normali
    long Xolde2;
    long Xolde3;
    long Xolde4;
    long Xolde5;
    long Xolde6;
    long Xolde7;
    long Xolde8;
    int X;//the RE's present state (RE = rotary encoder)
    int Xold1;// variabili encoder acc
    int Xold2;
    int Xold3;
    int Xold4;
    int Xold5;
    int Xold6;
    int Xdif;// the difference since last loop
    int doutpin;
    long timePrevious;
    long timeNow;
    long timeInterval = 80;//you adjust this for "your" feel of fast
    int fastTurn = 0;//variable for turn-speed state
    int CodeIn;//variabile Led
    int KpinNo;//variabili pulsanti
    int Koutpin;
    
    String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;
    String gearSimple; 
    
    void setup() 
    {
      Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
      // Lets set the output pin modes (longhand)
      pinMode(2, OUTPUT); // gear nose LED
      pinMode(3, OUTPUT); // gear left LED
      pinMode(4, OUTPUT); // gear right LED
      pinMode(5, OUTPUT); // gear nose in transition LED
      pinMode(6, OUTPUT); // gear left in transition LED
      pinMode(7, OUTPUT); // gear right in transition LED
      // now we will do all the inputs in a routine ,,,
      for (int KoutPin = 8; KoutPin < 70; 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
       timeNow = millis();//get the milli count. (for speed check)
       {ROTARYS();} // go and check the rotary encoder
        if (Serial.available()) {//checks the serial read buffer
        CodeIn = getChar();// reads one charactor via it's own routine (char getChar)
           if (CodeIn == '?') {QUESTION();}// The first identifier is "?", goto QUESTION void
      }//end of yes there is something to read
    
     // inserisco void encoder normali----------------------------------ENCODER NORMALI-----------------------------
     // ----------------------------------------------------------------NAV 1--------------------------------
       long X1 =(quad7.position())/2;  // divide by 2 for half cycle encoders
        if (X1 != Xolde1){  
          if (X1 > Xolde1) {Serial.println("A13");} // NAV1 1Mhz Dec
          if (X1 < Xolde1) {Serial.println("A14");} // NAV1 1MHz Inc
          Xolde1 = X1;     // overwrites the old reading with the new one.
          }//end of quad7 read   
    
       long X2 =(quad8.position())/2;  // divide by 2 for half cycle encoders
        if (X2 != Xolde2){  
          if (X2 > Xolde2) {Serial.println("A15");} // NAV1 25hz Dec
          if (X2 < Xolde2) {Serial.println("A16");} // NAV1 25Hz Inc
          Xolde2 = X2;     // overwrites the old reading with the new one.
          }//end of quad8 read  
    
     //--------------------------------------------------------NAV2------------------------------------
          
       long X3 =(quad9.position())/2;  // divide by 2 for half cycle encoders
        if (X3 != Xolde3){  
          if (X3 > Xolde3) {Serial.println("A15");} // NAV1 25hz Dec
          if (X3 < Xolde3) {Serial.println("A16");} // NAV1 25Hz Inc
          Xolde3 = X3;     // overwrites the old reading with the new one.
          }//end of quad8 read
            
       long X4 =(quad10.position())/2;  // divide by 2 for half cycle encoders
        if (X4 != Xolde4){  
          if (X4 > Xolde4) {Serial.println("A15");} // NAV1 25hz Dec
          if (X4 < Xolde4) {Serial.println("A16");} // NAV1 25Hz Inc
          Xolde4 = X4;     // overwrites the old reading with the new one.
          }//end of quad10 read  
    
    //----------------------------------------------------COM1-----------------------------------------
    
     long X5 =(quad11.position())/2;  // divide by 2 for half cycle encoders
        if (X5 != Xolde5){  
          if (X5 > Xolde5) {Serial.println("A15");} // NAV1 25hz Dec
          if (X5 < Xolde5) {Serial.println("A16");} // NAV1 25Hz Inc
          Xolde5 = X5;     // overwrites the old reading with the new one.
          }//end of quad8 read  
          
     long X6 =(quad12.position())/2;  // divide by 2 for half cycle encoders
        if (X6 != Xolde6){  
          if (X6 > Xolde6) {Serial.println("A15");} // NAV1 25hz Dec
          if (X6 < Xolde6) {Serial.println("A16");} // NAV1 25Hz Inc
          Xolde6 = X6;     // overwrites the old reading with the new one.
          }//end of quad8 read  
    
    //---------------------------------------------------COM2----------------------------------------
    
     long X7 =(quad13.position())/2;  // divide by 2 for half cycle encoders
        if (X7 != Xolde7){  
          if (X7 > Xolde7) {Serial.println("A15");} // NAV1 25hz Dec
          if (X7 < Xolde7) {Serial.println("A16");} // NAV1 25Hz Inc
          Xolde7 = X7;     // overwrites the old reading with the new one.
          }//end of quad8 read  
    
     long X8 =(quad14.position())/2;  // divide by 2 for half cycle encoders
        if (X8 != Xolde8){  
          if (X8 > Xolde8) {Serial.println("A15");} // NAV1 25hz Dec
          if (X8 < Xolde8) {Serial.println("A16");} // NAV1 25Hz Inc
          Xolde8 = X8;     // overwrites the old reading with the new one.
          }//end of quad8 read  
          
       
    } // end of void loop (The main one)
    
    char getChar()// Get a character from the serial buffer
    {
      while(Serial.available() == 0);// wait for data
      return((char)Serial.read());// Thanks Doug
    }// end of getchar void. 
    
    
    //----------------------------------------------------------------------Led Landing Gear-----------------------------------------
    
    void QUESTION(){    // The first identifier was "?"
    CodeIn = getChar(); // Get the second identifier
      switch(CodeIn) {// Now lets find what to do with it
        case 'Y': // found the second identifier (the "Gear simple")
           gearSimple = "";
          gearSimple += getChar();// get first charactor (Nose gear)
          if (gearSimple == "2"){digitalWrite(2, HIGH);}else{digitalWrite(2, LOW);}
          if (gearSimple == "1"){digitalWrite(5, HIGH);}else{digitalWrite(5, LOW);}
          gearSimple += getChar(); // get the second charactor (gear left)
          if (gearSimple.endsWith("2")){digitalWrite(3, HIGH);}else{digitalWrite(3, LOW);}
          if (gearSimple.endsWith("1")){digitalWrite(6, HIGH);}else{digitalWrite(6, LOW);}
          gearSimple += getChar(); // get the third charactor  (gear right)
          if (gearSimple.endsWith("2")){digitalWrite(4, HIGH);}else{digitalWrite(4, LOW);}
          if (gearSimple.endsWith("1")){digitalWrite(7, HIGH);}else{digitalWrite(7, LOW);}
        break;
         }
    }// end of question void
    
    //------------------------------------------------------------------------------Switch & buttons---------------------------------------------
    
    void INPUTS(){ // Simconnect codes and "Keys" section
      Kstringnewstate = "";
      for (int KpinNo = 8; KpinNo < 70; KpinNo++){ //set this to the input pins. (pins 10 to 50)
        KpinStateSTR = String(digitalRead(KpinNo)); //read the state of the pin
        KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 8));// 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){ 
            if (KpinNo == 8){if (KpinStateSTR == "1" ){Serial.println ("C01");}else {Serial.println ("C02");}}//  Landing Gear
            if (KpinNo == 9){if (KpinStateSTR == "1" ){Serial.println ("C040");}else {Serial.println ("C041");}}//Parcking Brake
            if (KpinNo == 10 && KpinStateSTR == "0"){Serial.println ("C14");} //                                  Flap Inc
            if (KpinNo == 11 && KpinStateSTR == "0"){Serial.println ("C15");} //                                  Flap dec
            if (KpinNo == 12){if (KpinStateSTR == "1" ){Serial.println ("E330");}else {Serial.println ("E331");}}// Prop Sync
            //-------------------------------AP----------------------------------------------------------------------------------------
            if (KpinNo == 22 && KpinStateSTR == "0"){Serial.println ("B04");} //HDG mode
            if (KpinNo == 23 && KpinStateSTR == "0"){Serial.println ("B08");} //APR mode
            if (KpinNo == 24 && KpinStateSTR == "0"){Serial.println ("B09");} // BC mode
            if (KpinNo == 25 && KpinStateSTR == "0"){Serial.println ("B10");} //NAV mode
            if (KpinNo == 26 && KpinStateSTR == "0"){Serial.println ("B30");} //AP  FD
            if (KpinNo == 27 && KpinStateSTR == "0"){Serial.println ("B30");} //Bank---------------------------
            if (KpinNo == 28 & KpinStateSTR == "0"){Serial.println ("B01");} //AP  AP
            if (KpinNo == 29 && KpinStateSTR == "0"){Serial.println ("Y011");} //AP  YD
            if (KpinNo == 30 && KpinStateSTR == "0"){Serial.println ("B05");} //AP  ALT
            if (KpinNo == 31 & KpinStateSTR == "0"){Serial.println ("B01");} //VS--------------------------
            if (KpinNo == 32 && KpinStateSTR == "0"){Serial.println ("Y011");} //FLC-----------------------
            if (KpinNo == 33 && KpinStateSTR == "0"){Serial.println ("B05");} //SPD--------------------------
            //  Add more here but remember to also change the figure in the next line down. (the 14)
            // now the "Keys" bit ,,,
            if (KpinNo > 70){ //i Pin oltre a questo divenbtano D
            Serial.print ("D"); 
          if (KpinNo < 8) Serial.print ("0");
          Serial.print (KpinNo);
          Serial.println (KpinStateSTR);
            }//end of 'its pinNo is greater than 14' 
          }//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
    
     // Acc encoder------------------------------------------------ENCODER ACCEL-----------------------------------------
     //-------------------------------------------------------------HDB--------------------------------------------------- 
    void ROTARYS(){
      // Now the bit for the rotary encoder input
      X =(quad1.position()/2);
      if (X != Xold1) { // checks to see if it different
       if(timeNow - timePrevious < timeInterval) {fastTurn = 1;}else{ fastTurn = 0;} //is it being turned fast or not ? 
        (Xdif = (X-Xold1));// finds out the RE's difference (going backwards or forwards)
        
        if (Xdif == 1) {//knob is going forward
           if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
          for (int accel = 1; accel <= 10; accel++){//loop to send "A57" 10 times ,, use more or less to suit.
            Serial.println ("A57");}// heading increase fast
           }else{//it's turned slow
           Serial.println ("A57");}// heading increase slow (sent once)
        }//end of forward
        
        if (Xdif == -1) {//knob is going backwards
          if (fastTurn == 1){  //yes it's being turned fast ,, else ,, it's being turned slow
          for (int accel = 1; accel <= 10; accel++){//loop to send "A58" 10 times
            Serial.println ("A58");}// heading decrease fast
           }else{//it's turned slow
           Serial.println ("A58");}// heading decrease slow (sent once)
        }//end of backwards
        
      if (quad1.position() > 1000){ // zero the rotary encoder count if too high or low
          quad1.position(0);
        }
        if (quad1.position() < -1000){
          quad1.position(0);
        }
        Xold1 = X; // overwrites the old reading with the new one.
        timePrevious = timeNow;//resets the previous time to timenow
      }//end of yes its different 
    
    //----------------------------------------------------------------------CRS1-------------------------------------------
    
        X =(quad2.position()/2);
      if (X != Xold2) { // checks to see if it different
       if(timeNow - timePrevious < timeInterval) {fastTurn = 1;}else{ fastTurn = 0;} //is it being turned fast or not ? 
        (Xdif = (X-Xold2));// finds out the RE's difference (going backwards or forwards)
        
        if (Xdif == 1) {//knob is going forward
           if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
          for (int accel = 1; accel <= 10; accel++){//loop to send "A57" 10 times ,, use more or less to suit.
            Serial.println ("A55");}// CRS1 increase fast
           }else{//it's turned slow
           Serial.println ("A55");}//  CRS1  increase slow (sent once)
        }//end of forward
        
        if (Xdif == -1) {//knob is going backwards
          if (fastTurn == 1){  //yes it's being turned fast ,, else ,, it's being turned slow
          for (int accel = 1; accel <= 10; accel++){//loop to send "A58" 10 times
            Serial.println ("A56");}// CRS1 decrease fast
           }else{//it's turned slow
           Serial.println ("A56");}// CRS1 decrease slow (sent once)
        }//end of backwards
        
      if (quad2.position() > 1000){ // zero the rotary encoder count if too high or low
          quad2.position(0);
        }
        if (quad2.position() < -1000){
          quad2.position(0);
        }
        Xold2 = X; // overwrites the old reading with the new one.
        timePrevious = timeNow;//resets the previous time to timenow
      }//end of yes its different 
    
      // 3 encoder--------------------------------------------------------CRS2---------------------------------
      
       X =(quad3.position()/2);
      if (X != Xold3) { // checks to see if it different
       if(timeNow - timePrevious < timeInterval) {fastTurn = 1;}else{ fastTurn = 0;} //is it being turned fast or not ? 
        (Xdif = (X-Xold3));// finds out the RE's difference (going backwards or forwards)
        
        if (Xdif == 1) {//knob is going forward
           if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
          for (int accel = 1; accel <= 10; accel++){//loop to send "A57" 10 times ,, use more or less to suit.
            Serial.println ("B37");}// CRS2 increase fast
           }else{//it's turned slow
           Serial.println ("B37");}//  CRS2  increase slow (sent once)
        }//end of forward
        
        if (Xdif == -1) {//knob is going backwards
          if (fastTurn == 1){  //yes it's being turned fast ,, else ,, it's being turned slow
          for (int accel = 1; accel <= 10; accel++){//loop to send "A58" 10 times
            Serial.println ("B36");}// CRS2 decrease fast
           }else{//it's turned slow
           Serial.println ("B36");}// CRS2 decrease slow (sent once)
        }//end of backwards
        
      if (quad3.position() > 1000){ // zero the rotary encoder count if too high or low
          quad3.position(0);
        }
        if (quad3.position() < -1000){
          quad3.position(0);
        }
        Xold3 = X; // overwrites the old reading with the new one.
        timePrevious = timeNow;//resets the previous time to timenow
      }//end of yes its different 
    
       // 4 encoder---------------------------------------------ALT--------------------------------------------
    
     X =(quad4.position()/2);
      if (X != Xold4) { // checks to see if it different
       if(timeNow - timePrevious < timeInterval) {fastTurn = 1;}else{ fastTurn = 0;} //is it being turned fast or not ? 
        (Xdif = (X-Xold4));// finds out the RE's difference (going backwards or forwards)
        
        if (Xdif == 1) {//knob is going forward
           if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
          for (int accel = 1; accel <= 10; accel++){//loop to send "A57" 10 times ,, use more or less to suit.
            Serial.println ("B11");}// ALT increase fast
           }else{//it's turned slow
           Serial.println ("B11");}//  ALT  increase slow (sent once)
        }//end of forward
        
        if (Xdif == -1) {//knob is going backwards
          if (fastTurn == 1){  //yes it's being turned fast ,, else ,, it's being turned slow
          for (int accel = 1; accel <= 10; accel++){//loop to send "A58" 10 times
            Serial.println ("B12");}// ALT decrease fast
           }else{//it's turned slow
           Serial.println ("B12");}// ALT decrease slow (sent once)
        }//end of backwards
        
      if (quad4.position() > 1000){ // zero the rotary encoder count if too high or low
          quad4.position(0);
        }
        if (quad4.position() < -1000){
          quad4.position(0);
        }
        Xold4 = X; // overwrites the old reading with the new one.
        timePrevious = timeNow;//resets the previous time to timenow
      }//end of yes its different 
    
         // 5 encoder----------------------------------------------SPD-------------------------------------------
    
     X =(quad5.position()/2);
      if (X != Xold5) { // checks to see if it different
       if(timeNow - timePrevious < timeInterval) {fastTurn = 1;}else{ fastTurn = 0;} //is it being turned fast or not ? 
        (Xdif = (X-Xold5));// finds out the RE's difference (going backwards or forwards)
        
        if (Xdif == 1) {//knob is going forward
           if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
          for (int accel = 1; accel <= 10; accel++){//loop to send "A57" 10 times ,, use more or less to suit.
            Serial.println ("B15");}// SPD increase fast
           }else{//it's turned slow
           Serial.println ("B15");}//  SPD  increase slow (sent once)
        }//end of forward
        
        if (Xdif == -1) {//knob is going backwards
          if (fastTurn == 1){  //yes it's being turned fast ,, else ,, it's being turned slow
          for (int accel = 1; accel <= 10; accel++){//loop to send "A58" 10 times
            Serial.println ("B16");}// SPD decrease fast
           }else{//it's turned slow
           Serial.println ("B16");}// SPD decrease slow (sent once)
        }//end of backwards
        
      if (quad5.position() > 1000){ // zero the rotary encoder count if too high or low
          quad5.position(0);
        }
        if (quad5.position() < -1000){
          quad5.position(0);
        }
        Xold5 = X; // overwrites the old reading with the new one.
        timePrevious = timeNow;//resets the previous time to timenow
      }//end of yes its different 
    
      // 6 encoder----------------------------------------------VS-------------------------------------------
    
     X =(quad6.position()/2);
      if (X != Xold6) { // checks to see if it different
       if(timeNow - timePrevious < timeInterval) {fastTurn = 1;}else{ fastTurn = 0;} //is it being turned fast or not ? 
        (Xdif = (X-Xold6));// finds out the RE's difference (going backwards or forwards)
        
        if (Xdif == 1) {//knob is going forward
           if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
          for (int accel = 1; accel <= 10; accel++){//loop to send "A57" 10 times ,, use more or less to suit.
            Serial.println ("B13");}// SPD increase fast
           }else{//it's turned slow
           Serial.println ("B13");}//  SPD  increase slow (sent once)
        }//end of forward
        
        if (Xdif == -1) {//knob is going backwards
          if (fastTurn == 1){  //yes it's being turned fast ,, else ,, it's being turned slow
          for (int accel = 1; accel <= 10; accel++){//loop to send "A58" 10 times
            Serial.println ("B14");}// SPD decrease fast
           }else{//it's turned slow
           Serial.println ("B14");}// SPD decrease slow (sent once)
        }//end of backwards
        
      if (quad6.position() > 1000){ // zero the rotary encoder count if too high or low
          quad6.position(0);
        }
        if (quad6.position() < -1000){
          quad6.position(0);
        }
        Xold6 = X; // overwrites the old reading with the new one.
        timePrevious = timeNow;//resets the previous time to timenow
      }//end of yes its different 
    
      
    }//end of Rotarys!!!!--------------------------------------------------------------------------------------
    Both they work individually, but because my poor experience, I have difficulty to link each other together!
    Someone could kindly help me?




    Thanks in advance from Italy (sorry for my english)

  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: Please, help me with this sketch!!

    As you say: "but because my poor experience", you should use the Demo.ino's that comes with the Link2FS.
    This way you will understand what is going on.
    To begin the learning process with others quite sophisticated sketches, is not the way to go.
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------