Results 1 to 2 of 2
  1. #1
    75+ Posting Member vikingsraven's Avatar
    Join Date
    Dec 2012
    Location
    UK
    Posts
    89
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Question Possible new Key function

    Hi all, ive been toying with trying a different approach to the key detection, rather than using a String of 1's which will use up about 72 bytes of info and loads of variables i've managed to limit it to 8 bytes and and a lot less variables, it seems to work.
    What i've done is used bit switching, rather than changing the whole byte from 1 to 0 , just change a bit.
    A long is 32 bits long so 2 longs will give you 64 single bit registers. 6 less than before but they can be checked a s well with a bit of tweaking.
    Also you don't need a previous condition variable as its either 1 or 0.
    I've attached the code have a play see if it works ok, i have made the inputs pullup, so youll need to ground the switches to change the condition, this stops having to use loads of resistor for pull up as well.

    I've used a short hand command as well
    instead of if ( x>10 ) { do this} else { do that} , i've used a very hand shortcut which is as follows
    (x>10)? do this: do that ;
    shorter and less typing!
    It also checks 2 lots of switches one after the other , rather than a 70 count i'm using 32 and checking first the lower 32 then the upper 32 switches.
    Then rather than scanning through all the positions , i've just linked the switch conditions that i want to monitor real world using the status function at the end. If you use define to setup the info at the beginning its just a case of reading the bits your only interested in. hope that makes sense.

    Code:
    //This code is in the public domain// new idea for switching
    // For use with "Link2fs_Multi" Jimspage.co.nz  
    // by Mark Stillman 29/01/14
    
    
    #define UNO 20
    #define MEGA 64
    #define LED1 A15                          // analog 15 is now LED1
    #define LED2 A14                          // analog 14 is now LED2
    #define PowerSwitch  19                  // power switch is 19  switches from 2 to 62
    
    
    long Chan1 = 0xFFFFFFFF;              // sets all bits to 1
    long Chan2 = 0xFFFFFFFF;              // sets all bits to 1
    
    
    int Dev = MEGA;                      // sets number of inputs
    //int Dev = UNO;                    // un rem for UNO or MEGA as required not fully implemented yet!
    
    
    
    
    void setup() {
      Serial.begin(115200);              // setup serial port
      for (int i = 2; i < Dev; i++) {    // tell Arduino what pins are input
        pinMode(i, INPUT_PULLUP);        // internal pullups used so no external resisitors required use ground to switch
      }
      pinMode(LED1, OUTPUT);             // led on A15 using LED1 as marker
      pinMode(LED2,OUTPUT);
    }
    
    
    
    
    // ********************************************************* Loop
    void loop() {
    
    
      Keys();                            // check key changes
      Status();                          // output anything that needs to be monitored real world like leds
    }
    
    
    
    
    // ********************************************************* Keys Function 
    void Keys() {
      for (int i = 0; i < 32; i++) {                        // 32 bit count
        if (i + 2 != 13) {                                  // so long as its not pin 13 , we start at 2 for the switches
          if (digitalRead(i + 2) != bitRead(Chan1, i)) {    // reads the switch input and compares to bit in reigster
            Chan1 = toggle(Chan1, i, i + 2);                // if it is different got to toggle function then on return of Data change register to last
          }                                                // if not carry on
        }
        if (digitalRead(i + 32) != bitRead(Chan2, i)) {    // check for second set of switches
          Chan2 = toggle(Chan2, i, i + 32);               // if it is different got to toggle function then on return of Data change register to last
        }
      }
    }
    
    
    
    
    // ********************************************************* TOGGLE Function
    long toggle(long Data, int Dbit, int Switch) {        // Long data is returned from this function , input requires what channel, bit and switch
    
    
      (bitRead(Data, Dbit) == true) ? bitWrite(Data, Dbit, 0) : bitWrite(Data, Dbit, 1);    // short hand if then statement if 1 then make it nought  if nought make it one
    
    
      Serial.print("D");                                // so start sending data
      if (Switch < 10) {                                // its < 10 so we need to a zero
        Serial.print("0");
      }                                                  // if > 10 then skips
      Serial.print(Switch);                             // what switch changed
      Serial.println(bitRead(Data, Dbit));              // new condition
      return Data;                                       // send the updated variable back
    }
    
    
    // ********************************************************** Status Function
    void Status() {
      digitalWrite(LED1, bitRead(Chan1, PowerSwitch));      // switch led1 on if 1 off if 0
      digitalWrite(LED2, !bitRead(Chan1,PowerSwitch));      // switch LED2 on if 0 off if 1
      }
    see what you think, be gentle though!
    Mark

  2. #2
    75+ Posting Member vikingsraven's Avatar
    Join Date
    Dec 2012
    Location
    UK
    Posts
    89
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Possible new Key function

    thinking about it should have named this switches not keys;
    anyway the next step is to have momentary switches on the inputs and get them latching, so press on press off. Rather than latching switches.
    This may also work with the encoders as well, which will reduce the code size on the arduino, not a major issue on the mega.