home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   alt.energy.homepower      Electrical part of living of the grid      2,576 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 931 of 2,576   
   Curbie to All   
   Re: 1kW $50 E-Cat ? (1/2)   
   30 Jan 12 16:27:03   
   
   From: jim.richards65@yahoo.com   
      
   Jim.   
      
   I just do it in software, also illustrates the use of a timing   
   variable.   
      
   Curbie   
      
   /* function to determine if a button is being pressed. If so, returns   
   the button code constant representing   
    the button being pressed. If no button has been pressed, BV_NONE is   
   returned. If multiple buttons are   
    pressed (generally precluded by the mechanics of the buttons, but   
   possible nonetheless), the first button   
    pressed is returned.   
      
    The function reads the input shift register data and masks off the   
   non-switch bits. It stores the current   
    state of the switches in a static variable. It also stores a static   
   boolean representing a change in raw   
    switch data, for debouncing. When new switch data from the input   
   shift register differs from the previously   
    stored value, the boolean "debouncing" is set true and the system   
   time is stored. When the function is   
    entered with the boolean "debouncing" set true, the current time is   
   compared to the previously stored   
    time to see if the debounce period has eneded. If so, the input shift   
   register data is again sampled and   
    if the state is the same as previously, a switch activation is   
   declared and the swtich value is encoded   
    and returned.   
      
    This function is non-blocking. It returns immediately and does not   
   block on the debounce time.  Debouncing   
    is determined dynamically each time the function is called. */   
   int getButton() {                                   // get button   
   value of button curently being pressed   
   static unsigned long changeDetectionTime;           // variable to   
   hold the system time for debouncing   
   static boolean debouncing = false;                  // set true if   
   debouncing   
   static byte buttonState = 0;                        // hold the last   
   read state of the button shift register for debouncing   
   byte newbuttonState;                                // hold the   
   current data from the button switch register   
   const unsigned long DEBOUNCE_TIME = 20L;            // 20 milliseconds   
   to debounce buttons   
      
   if (debouncing) {                                   // if in the   
   process of debouncing buttons   
     if ( milliSec(changeDetectionTime) < DEBOUNCE_TIME) { // if still   
   waiting on debounce   
       return BV_NONE; }                               // return to   
   caller, no button(s) presses   
     else {                                            // if debounce   
   time expired   
       newbuttonState = readButtons();                 // read button   
   data for any button(s) pressed   
       newbuttonState = newbuttonState & 0x3F;         // mask off unused   
   2 bits   
       debouncing = false;                             // flag NOT in the   
   process of debouncing switches   
       if (newbuttonState == buttonState) {            // if button data   
   onfirmed   
         if ( (buttonState == 0) ) return BV_NONE;     // return to   
   caller, no button(s) presses   
         if ( (buttonState & 0x01) != 0 ) return BV_MENU;   
         if ( (buttonState & 0x02) != 0 ) return BV_SELECT;   
         if ( (buttonState & 0x04) != 0 ) return BV_UP;   
         if ( (buttonState & 0x08) != 0 ) return BV_DOWN;   
         if ( (buttonState & 0x10) != 0 ) return BV_LEFT;   
         if ( (buttonState & 0x20) != 0 ) return BV_RIGHT;   
         return BV_ERROR; }                            // just in case   
   something went wrong with the code!   
       else {                                          // switch action   
   not confirmed -- just noise   
         return BV_NONE;                               // return to   
   caller, no button(s) presses   
       }                                               // end if button   
   data onfirmed   
     }                                                 // end if still   
   waiting on debounce   
   }                                                   // end if in the   
   process of debouncing buttons   
   else {                                              // code if not   
   debouncing   
     newbuttonState = readButtons();                   // read button   
   data for any button(s) pressed   
     newbuttonState = newbuttonState & 0x3F;           // mask off extra   
   bits   
     if (newbuttonState == buttonState) {              // if no change in   
   the button shift register data   
       return BV_NONE; }                               // return to   
   caller, no button(s) presses   
     else {                                            // shift register   
   data has changed -- debounce   
       buttonState = newbuttonState;                   // store the new   
   switch state   
       changeDetectionTime = millis();                 // store time time   
   for debouncing   
       debouncing = true;                              // set debouncing   
   flag   
       return BV_NONE;                                 // no decision   
   until after debouncing time and re-verification   
     }                                                 // end if no   
   change in the button shift register data   
   }                                                   // end if in the   
   process of debouncing buttons   
   }                                                   // end getButtons   
   function   
      
   //   
   ................................................................   
   ...............................   
   //   
   /* function to read in the button data from the 74HC165 shift   
   register.  One unsigned byte of data is returned.   
    arguments:  none.   
    return:  one byte of data representing:   
    bit 0: BV_MENU button depressed   
    bit 1: 5-way switch BV_SELECT button depressed   
    bit 2: 5-way switch BV_UP position activated   
    bit 3: 5-way switch BV_DOWN position activated   
    bit 4: 5-way switch BV_LEFT position activated   
    bit 5: 5-way switch BV_RIGHT position activated   
    bit 6: spare - not presently connected   
    bit 7: spare - not presently connected   
    It is possible to have multiple bits set in the returned byte.  The   
   returned byte just contains the present status   
    of all 8 74HC165 parallel inputs. */   
   byte readButtons() {                                // read button   
   data to see if any and what buttons have been pressed   
   byte buttonData = 0;                                // current buttons   
   pressed data   
      
   digitalWrite (BTN_CLOCK, HIGH);                     // initialize   
   digitalWrite (BTN_LATCH, LOW);                      // start   
   handshake?? sample the buttons   
   digitalWrite (BTN_LATCH, HIGH);                     // start   
   handshake?? sample the buttons   
   buttonData = shiftIn (BTN_DATA, BTN_CLOCK, MSBFIRST); // read in the   
   shift register data   
   return buttonData;                                  // return to call   
   the button data   
   }                                                   // end readButtons   
   function   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca