Results 1 to 1 of 1
  1. #1
    New Member
    Join Date
    Sep 2014
    Location
    Indiana
    Posts
    3
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Rotary Accelleration help

    Hi everyone,
    After about 100 hours of learning, experimenting and lots of coffee I have finally assembled a working Auto pilot and Radio Stack. It's based off of PandragoQ's code mixed with several things from Jim's radio code. It's not yet complete but all radio and AP functions are working and displaying properly on my 20x4 LCD(I2C).
    Where I'm having problems is... I would like to add acceleration to two of the rotary encoders. I have tried for quite some time to integrate Jim's example code but it's not working. The rotarys are still functioning properly but no acceleration.

    Any help would be appreciated... I have the original code in place right now with the attempted acceleration code right below the original code

    Thanks in advance....

    /*
    This sketch is a complete radio head and Auto Pilot for FSX
    It needs to be used with Link2fs_Multi
    It was compiled with Arduino version 1.1

    Thanks to Jim and pandragoq for the framework and code
    http://www.jimspage.co.nz/intro.htm
    https://github.com/pandragoq/fscontroller/

    This example code is in the public domain.
    */

    #include "wire.h"
    #include "liquidcrystal_i2c.h"
    #include "keypad.h"
    #include "math.h"
    #include "Quadrature.h"
    Quadrature quad1(3, 2);
    Quadrature quad2(5, 4);
    Quadrature quad3(7, 6);
    #define I2C_ADDR 0x27 // I2C address of PCF8574A
    #define BACKLIGHT_PIN 3
    #define En_pin 2
    #define Rw_pin 1
    #define Rs_pin 0
    #define D4_pin 4
    #define D5_pin 5
    #define D6_pin 6
    #define D7_pin 7
    #define WINDOWS 3
    #define DEBUG 0

    int CodeIn;// used on all serial reads
    long timePrevious;
    long timeNow;
    long timeInterval = 80;//you adjust this for "your" feel of fast
    int fastTurn = 0;//variable for turn-speed state

    int KpinNo;
    int Koutpin;
    // Rotary variables
    int R;// first rotary
    int Rold;// the old reading
    int Rdif;// the difference since last loop
    int R2;// second rotary
    int R3;// third rotary
    int Rold2;// a second loop old reading
    int Rdif2; // the second test difference
    int Rold3;// a third loop old reading
    int Rdif3; // the third test difference
    // Menu variables
    byte mainActiveCursor; //Control which menu to activate
    boolean firstRotaryPressed;
    boolean gotData;
    //Autopilot variables
    byte autopilotCursor;
    String hdg;
    String alt;
    String ias;
    String crs;
    String vsp;
    String apmode;
    String apon;
    String aton;
    String buffhdg;
    String buffalt;
    //Radio variables
    byte radioCursor;
    String com1mhz;
    String com1stb;
    String com2mhz;
    String com2stb;
    String nav1mhz;
    String nav1stb;
    String nav2mhz;
    String nav2stb;

    // Dme Varables
    byte dmeCursor;
    String dme1;
    String dme2;
    String dme1speed;
    String dme2speed;

    //Navigation variables
    byte navCursor;
    String squawk;
    String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;
    LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin, BACKLIGHT_PIN, POSITIVE);


    //------------------------------------------------------------------------------------------------------------------------------

    void setup() {
    //Switch the backlight on (this doesn't work....)
    //pinMode ( BACKLIGHT_PIN, OUTPUT );
    //digitalWrite ( BACKLIGHT_PIN, HIGH );
    lcd.begin(20,4);
    lcd.clear();
    lcd.home();
    lcd.setCursor(5,0);
    lcd.print("AP + RADIO");
    delay (1000);
    lcd.setCursor(3,1);
    lcd.print("by Chuck DeBow");
    delay (1000);
    lcd.setCursor(7,2);
    lcd.print("v0.01");
    delay(1000);
    lcd.setCursor(6,3);
    lcd.print("Loading");
    delay(5000);
    mainActiveCursor = 0;
    firstRotaryPressed = false;
    gotData == false;


    // Initialize autopilot variables------------------------------------
    autopilotCursor = 0;
    hdg = "000";
    alt = "00000";
    ias = "000";
    crs = "000";
    vsp = "0000";
    apmode = "NAV";
    apon = "OFF";
    aton = "OFF";


    // Initialize radio variables--------------------------------------
    radioCursor = 0;
    com1mhz = "000.00";
    com1stb = "000.00";
    com2mhz = "000.00";
    com2stb = "000.00";
    nav1mhz = "000.00";
    nav1stb = "000.00";
    nav2mhz = "000.00";
    nav2stb = "000.00";

    // Intialize DME variables-----------------------------------------
    dmeCursor = 0;
    dme1 = "000.0";
    dme2 = "000.0";
    dme1speed = "000";
    dme2speed = "000";

    //----------------------------------------------------------------

    lcd.clear();
    //printAutopilot();
    printDebug();
    for (int KoutPin = 8; KoutPin < 70; KoutPin++)// Get all the pins ready for "Keys"
    {
    pinMode(KoutPin, INPUT);
    digitalWrite(KoutPin, HIGH);
    }
    Serial.begin(115200);
    }//-----------------------------------END of SETUP-------------------------------------------------------------------------------


    void printData(String a, String b, String c, String d){
    lcd.clear();
    printText(0,0,a);
    printText(0,1,b);
    printText(0,2,c);
    printText(0,3,d);
    }
    //-----------------------------------------------------------------------------------------------------------------------------------
    void printText(int col, int line, String text){
    lcd.setCursor(col,line);
    lcd.print(text);
    }
    //-----------------------------------------------------------------------------------------------------------------------------------

    String setAPLine(String a, String b, String c, String d){
    String aux = a;
    aux += b;
    aux += c;
    aux += d;
    return aux;
    }
    void printAutopilot(){
    String line1 = setAPLine(" HDG ", hdg, " ALT ",alt);
    String line2 = setAPLine(" CRS ", crs, " VSP ",vsp);
    String line3 = setAPLine("","","",""); // (" IAS ", ias, " MODE ",apmode); - Original Code for line
    String line4 = setAPLine(" AP ", apon, " MODE ",apmode); // (" AP ", apon, " AT ", aton); - Original Code for line
    printData(line1, line2, line3, line4);
    chooseItem(0);
    }
    //---------------------------------------------------------------------------------------------------------------------------------------
    String setRadioLine(String title, String active, String standby){
    String aux = title;
    aux += " ";
    aux += active;
    aux += "/";
    aux += standby;
    return aux;
    }
    void printRadio(){
    String line1 = setRadioLine(" COM1",com1mhz, com1stb);
    String line2 = setRadioLine(" NAV1",nav1mhz, nav1stb);
    String line3 = setRadioLine(" COM2",com2mhz, com2stb);
    String line4 = setRadioLine(" NAV2",nav2mhz, nav2stb);
    printData(line1, line2, line3, line4);
    chooseItem(0);
    }
    //-------------------------------------------------------------------------------------------------------------------------------------


    String setDmeLine(String e, String f,String g,String h){
    String aux = e;
    aux += f;
    aux += g;
    aux += h;
    return aux;
    }
    void printDme(){
    String line1 = setDmeLine("DME 1 ","",""," Speed");
    String line2 = setDmeLine(" ",dme1," ",dme1speed);
    String line3 = setDmeLine("DME 2 ","",""," Speed");
    String line4 = setDmeLine(" ",dme2," ",dme2speed);
    printData(line1, line2, line3, line4);
    chooseItem(0);
    }

    //-----------------------------------------------------------------------------------------------------------------------------------
    void printInfo(){
    }
    void printDebug(){
    lcd.clear();
    lcd.setCursor(0,0);
    }

    //--------------------------------------------------Menu Cursor Control ------------------------------------------------------------
    void moveMenu(int direction) {
    uint8_t position;
    mainActiveCursor = (mainActiveCursor + direction + WINDOWS) % WINDOWS;
    switch (mainActiveCursor) {
    case 0: // Autopilot menu
    printAutopilot();
    break;
    case 1: // Radio menu
    printRadio();
    break;


    //----------------------------------------------------------------
    case 2: // DME menu
    printDme ();
    break;
    //--------------------------------------------------------------

    case 3:

    printDebug();
    break;
    default:
    // do something
    break;
    }
    }

    void chooseItem(int direction){
    switch (mainActiveCursor) {
    case 0:
    lcd.setCursor(19,autopilotCursor); lcd.print(" ");
    autopilotCursor = (autopilotCursor + direction + 4) % 4;
    lcd.setCursor(19,autopilotCursor); lcd.print(char(127));
    break;
    case 1:
    lcd.setCursor(19,radioCursor); lcd.print(" ");
    radioCursor = (radioCursor + direction + 4) % 4;
    lcd.setCursor(19,abs(radioCursor)); lcd.print(char(127));
    break;
    //--------------------------------------------------------------------------------

    case 2:
    lcd.setCursor(19,dmeCursor); lcd.print(" ");
    dmeCursor = (dmeCursor + direction + 4) % 4;
    lcd.setCursor(19,dmeCursor); lcd.print(char(127));
    break;
    break;

    //--------------------------------------------------------------------------------

    default:
    // do something
    break;
    }
    }

    //----------------------------------------Menu Rotary Button Control----------------------------------------
    void firstRotary(int direction){
    if (firstRotaryPressed == false){
    moveMenu(direction);
    } else {
    chooseItem(direction);
    }
    }
    void sendRotaryControl(int direction, String pos, String neg, String push){
    if (push == "B04" and direction == 0) { buffhdg = hdg; }
    if (push == "B05" and direction == 0) { buffalt = alt; }
    if (direction == 1 and pos != "") { Serial.println(pos); } else {
    if (direction == -1 and neg != "") { Serial.println(neg); } else {
    if (direction == 0 and push != "") { Serial.println(push); }
    }
    }
    if (push == "B04" and direction == 0) { Serial.print("A59"); Serial.println(buffhdg); }
    if (push == "B05" and direction == 0) { Serial.print("B32"); Serial.println(buffalt); }
    }
    void secondRotary(int direction){
    switch (mainActiveCursor) {
    case 0:
    if (autopilotCursor == 0) { sendRotaryControl(direction,"A57","A58","B04"); }
    if (autopilotCursor == 1) { sendRotaryControl(direction,"A56","A55","B10"); }
    if (autopilotCursor == 2) { sendRotaryControl(direction,"","",""); } // "B15","B16","B26" - Original Settings for this line
    if (autopilotCursor == 3) { sendRotaryControl(direction,"","","B01"); } // "","","B01" - Original Settings for this line
    break;
    case 1:
    if (radioCursor == 0) { sendRotaryControl(direction,"A02","A01","A06"); }
    if (radioCursor == 1) { sendRotaryControl(direction,"A14","A13","A18"); } //A08","A07","A12 - ORIGINAL SETTINGS FOR THIS LINE
    if (radioCursor == 2) { sendRotaryControl(direction,"A08","A07","A12"); } //A14","A13","A18 - ORIGINAL SETTINGS FOR THIS LINE
    if (radioCursor == 3) { sendRotaryControl(direction,"A20","A19","A24"); }
    break;
    //------------------------------------------------------------------------------------------------------------------------------
    /*
    case 2:
    if (dmeCursor == 0) { sendRotaryControl(direction,"","",""); }
    if (dmeCursor == 1) { sendRotaryControl(direction,"","",""); }
    if (dmeCursor == 2) { sendRotaryControl(direction,"","",""); }
    if (dmeCursor == 3) { sendRotaryControl(direction,"","",""); }
    break;
    */
    //-----------------------------------------------------------------------------------------------------------------------------

    default:
    // do something
    break;
    }
    }
    void thirdRotary(int direction){
    switch (mainActiveCursor) {
    case 0:
    if (autopilotCursor == 0) { sendRotaryControl(direction,"B11","B12","B05"); }
    if (autopilotCursor == 1) { sendRotaryControl(direction,"B13","B14",""); }
    if (autopilotCursor == 2) { sendRotaryControl(direction,"","",""); }
    if (autopilotCursor == 3) { sendRotaryControl(direction,"","","A54"); }
    break;
    case 1:
    if (radioCursor == 0) { sendRotaryControl(direction,"A04","A03","A45"); }
    if (radioCursor == 1) { sendRotaryControl(direction,"A16","A15","A18"); } //A10","A09","A46 - ORIGINAL SETTINGS FOR THIS LINE
    if (radioCursor == 2) { sendRotaryControl(direction,"A10","A09","A46"); } //A16","A15","A18 - ORIGINAL SETTINGS FOR THIS LINE
    if (radioCursor == 3) { sendRotaryControl(direction,"A22","A21","A24"); }
    break;
    /*
    case 2:
    if (dmeCursor == 0) { sendRotaryControl(direction,"","",""); }
    if (dmeCursor == 1) { sendRotaryControl(direction,"","",""); }
    if (dmeCursor == 2) { sendRotaryControl(direction,"","",""); }
    if (dmeCursor == 3) { sendRotaryControl(direction,"","",""); }
    break;
    */

    default:
    // do something
    break;
    }
    }

    //---------------------------------------------Reading Extractions-----------------------------------------------------------
    void readSerial(){
    if (DEBUG == 1){
    if(Serial.available()){
    lcd.print(getChar());
    } else {
    // lcd.print("!");
    }
    } else {
    if (Serial.available()) {// Check if serial data has arrived from PC
    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)
    gotData == true;
    }
    }
    }

    //--------------------------------------------------Program Loop--------------------------------------------------------------

    void loop() {
    {KEYS();}
    timeNow = millis();//get the milli count. (for speed check)
    {ENCODER();}
    {readSerial();}
    }
    char getChar()// Get a character from the serial buffer
    {
    while(Serial.available() == 0);// wait for data
    return((char)Serial.read());// Thanks Doug
    }
    String getString(int chars){
    String aux = "";
    for(int i=0; i aux += getChar();
    }
    return aux;
    }
    void EQUALS(){
    String aux;
    CodeIn = getChar();
    switch (CodeIn) {
    case 'A':
    com1mhz = getString(6); if (mainActiveCursor == 1) { printText(6,0,com1mhz);}
    break;
    case 'B':
    com1stb = getString(6); if (mainActiveCursor == 1) { printText(13,0,com1stb);}
    break;
    case 'C':
    com2mhz = getString(6); if (mainActiveCursor == 1) { printText(6,2,com2mhz);} //6,1,com2mhz - ORIGINAL SETTINGS FOR THIS LINE
    break;
    case 'D':
    com2stb = getString(6); if (mainActiveCursor == 1) { printText(13,2,com2stb);} //13,1,com2stb - ORIGINAL SETTINGS FOR THIS LINE
    break;
    case 'E':
    nav1mhz = getString(6); if (mainActiveCursor == 1) { printText(6,1,nav1mhz);} //6,2,nav1mhz - ORIGINAL SETTINGS FOR THIS LINE
    break;
    case 'F':
    nav1stb = getString(6); if (mainActiveCursor == 1) { printText(13,1,nav1stb);} //13,2,nav1stb - ORIGINAL SETTINGS FOR THIS LINE
    break;
    case 'G':
    nav2mhz = getString(6); if (mainActiveCursor == 1) { printText(6,3,nav2mhz);}
    break;
    case 'H':
    nav2stb = getString(6); if (mainActiveCursor == 1) { printText(13,3,nav2stb);}
    break;


    // -------------------------------------------------------------------------------------------------------------

    case 'K':
    dme1 = getString(5); if (mainActiveCursor == 2) { printText(1,1,dme1+"NM"); }
    break;
    case 'L':
    dme2 = getString(5); if (mainActiveCursor == 2) { printText(1,3,dme2+"NM"); }
    break;
    case 'X':
    dme1speed = getString(3); if (mainActiveCursor == 2) { printText(12,1,dme1speed+"KT"); }
    break;
    case 'Y':
    dme2speed = getString(3); if (mainActiveCursor == 2) { printText(12,3,dme2speed+"KT"); }
    break;


    //-----------------------------------------------------------------------------------------------------
    case 'a':
    aux = getString(1); if (aux == "0") { apon = "OFF"; } else { apon = " ON"; }
    if (mainActiveCursor == 0) { printText(5,3,apon); }
    break;
    case 'b':
    alt = getString(5); if (mainActiveCursor == 0) { printText(14,0,alt); }
    break;
    case 'c':
    vsp = getString(5); if (mainActiveCursor == 0) { printText(14,1,vsp); }
    break;
    case 'd':
    hdg = getString(3); if (mainActiveCursor == 0) { printText(5,0,hdg); }
    break;
    case 'e':
    crs = getString(3); if (mainActiveCursor == 0) { printText(5,1,crs); }
    break;


    case 'f':
    /*ias = getString(3); if (mainActiveCursor == 0) { printText(5,2,ias); }
    break;*/
    case 'l':
    aux = getString(1); if (aux == "0") { apmode = "NAV"; } else { apmode = "GPS"; }
    if (mainActiveCursor == 0) { printText(15,3,apmode); }
    break;
    /*
    case 't':
    aux = getString(1); if (aux == "0") { aton = "OFF"; } else { aton = " ON"; }
    if (mainActiveCursor == 0) { printText(15,3,aton); }
    break;
    */
    default:
    // do something
    break;
    }
    }


    void KEYS()
    {
    Kstringnewstate = "";
    for (int KpinNo = 8; KpinNo < 70; KpinNo++){
    KpinStateSTR = String(digitalRead(KpinNo));
    KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - );
    if (KpinStateSTR != KoldpinStateSTR)
    {
    if (KpinNo != 13){
    //Serial.print ("D");
    //if (KpinNo < 10) Serial.print ("0");
    //Serial.print (KpinNo);
    //Serial.println (KpinStateSTR);
    }
    switch (KpinNo) {
    case 8:
    firstRotaryPressed = !firstRotaryPressed;
    break;
    case 9:
    if (KpinStateSTR == "0") { secondRotary(0); }
    break;
    case 10:
    if (KpinStateSTR == "0") { thirdRotary(0); }
    break;
    default:
    // do something
    break;
    }
    }
    Kstringnewstate += KpinStateSTR;
    }
    Kstringoldstate = Kstringnewstate;
    }

    // ------------------------------------------------------------------------------------------

    void ENCODER(){
    // This is the original code for Rotary 1

    R =(quad1.position()/2); //The /2 is to suit the encoder
    if (R != Rold) { // checks to see if it different
    (Rdif = (R-Rold));// finds out the difference

    if (Rdif == 1) firstRotary(1);
    if (Rdif == -1) firstRotary(-1);;
    Rold = R; // overwrites the old reading with the new one.
    } //End of Rotary1



    // This is the code with accelleration (Not Working!!!!)
    /*
    R =(quad1.position()/2); //The /2 is to suit the encoder(See my website)
    if (R != Rold) { // checks to see if it different
    if(timeNow - timePrevious < timeInterval) {fastTurn = 1;}else{ fastTurn = 0;} //is it being turned fast or not ?
    (Rdif = (R-Rold));// finds out the RE's difference (going backwards or forwards)


    if (Rdif == 1) firstRotary(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++);}
    }//end of forward

    if (Rdif == -1) firstRotary(-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++);}
    }//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);
    }
    Rold = R; // overwrites the old reading with the new one.
    timePrevious = timeNow;//resets the previous time to timenow
    }
    */



    // ----------------------------------------------------------------------------------------


    // This is the oringinal code for Rotary 2

    R2 =(quad2.position()/2);
    if (R2 != Rold2) {
    (Rdif2 = (R2-Rold2));
    if (Rdif2 == 1) secondRotary(1);;
    if (Rdif2 == -1) secondRotary(-1);;
    Rold2 = R2;
    } //End of Rotary2

    // This is the code with Accelleration (Not Working!!!)
    /*
    R2 =(quad2.position()/2); //The /2 is to suit the encoder(See my website)
    if (R2 != Rold2) { // checks to see if it different
    if(timeNow - timePrevious < timeInterval) {fastTurn = 1;}else{ fastTurn = 0;} //is it being turned fast or not ?
    (Rdif2 = (R2-Rold2));// finds out the RE's difference (going backwards or forwards)


    if (Rdif2 == 1) secondRotary(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++);}
    }//end of forward

    if (Rdif2 == -1) secondRotary(-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++);
    }else{ fastTurn = 0;}
    }//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);
    }
    Rold2 = R2; // overwrites the old reading with the new one.
    timePrevious = timeNow;//resets the previous time to timenow
    }
    */


    //-----------------------------------------------------------------------------------------

    // This is the oringinal code for Rotary 3

    R3 =(quad3.position()/2);
    if (R3 != Rold3) {
    (Rdif3 = (R3-Rold3));
    if (Rdif3 == 1) thirdRotary(1);
    if (Rdif3 == -1) thirdRotary(-1);
    Rold3 = R3;
    }//End of Rotary3
    } //End of Rotarys
    Last edited by Jim NZ; 12-30-2014 at 02:41 AM. Reason: Corrected blank 'includes'