Click here to Skip to main content
15,867,966 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Years ago… Pin
Mike Hankey8-Jan-23 13:29
mveMike Hankey8-Jan-23 13:29 
GeneralRe: Years ago… Pin
jmaida8-Jan-23 14:03
jmaida8-Jan-23 14:03 
GeneralRe: Years ago… Pin
Keith Barrow9-Jan-23 4:57
professionalKeith Barrow9-Jan-23 4:57 
GeneralRe: Years ago… Pin
DRHuff9-Jan-23 9:36
DRHuff9-Jan-23 9:36 
GeneralRe: Years ago… Pin
Keith Barrow10-Jan-23 3:58
professionalKeith Barrow10-Jan-23 3:58 
GeneralRe: Years ago… Pin
Single Step Debugger9-Jan-23 5:33
Single Step Debugger9-Jan-23 5:33 
QuestionWhat are some stupid-useful coding tricks you rely on? Pin
honey the codewitch8-Jan-23 5:49
mvahoney the codewitch8-Jan-23 5:49 
AnswerRe: What are some stupid-useful coding tricks you rely on? Pin
raddevus8-Jan-23 6:06
mvaraddevus8-Jan-23 6:06 
I had a number of buttons on my Arduino project that needed to do similar (but slightly different) things.
I had all this code to handle the button debouncing.
So then I discovered a nice way to pass a pointer to a function (button press handler) so that the code was the same no matter which button was pressed, but the specific function for the correct button is called.

And, all the debouncing for each button is wrapped up nicely.

C++
typedef void (*ButtonPressHandler) (void);

void checkButton(const int BUTTON,  bool &last, bool &current, ButtonPressHandler handler ){
  current = debounce(last, BUTTON);              // Read debounced state
  if (last == LOW && current == HIGH)    // If it was pressed…
  {
    // This runs the code that we want to happen 
    // when the user presses the button.  The function ptr 
    // allows us to do different types of work
     handler();
  }
  last = current;                        // Reset button value
}

boolean debounce(boolean last, int button)
{
 boolean current = digitalRead(button);    // Read the button state
 if (last != current)                      // If it's different…
 {
  delay(5);                                // Wait 5ms
  current = digitalRead(button);           // Read it again
 }
 return current;                           // Return the current value
}

Now you just call it with whichever button is pressed so it can all be handled:

C++
// My device has a button that changes the room (location)
// The last argument is the button handler code (pointer to function)
checkButton(ROOM_BTN,roomBtnPrev,roomBtnCurrent,checkChangeRoomButton);

// It also has a button that turns data writing (to SD Card) on / off
checkButton(DATA_BTN, dataBtnPrev, dataBtnCurrent, checkWriteDataButton);

Now they are both handled the same way and all the debouncing is wrapped up.

What do you think?
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
honey the codewitch8-Jan-23 6:16
mvahoney the codewitch8-Jan-23 6:16 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
raddevus8-Jan-23 6:34
mvaraddevus8-Jan-23 6:34 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
honey the codewitch8-Jan-23 7:55
mvahoney the codewitch8-Jan-23 7:55 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
raddevus8-Jan-23 11:03
mvaraddevus8-Jan-23 11:03 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
honey the codewitch8-Jan-23 11:59
mvahoney the codewitch8-Jan-23 11:59 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
honey the codewitch8-Jan-23 12:07
mvahoney the codewitch8-Jan-23 12:07 
QuestionRe: What are some stupid-useful coding tricks you rely on? Pin
CPallini8-Jan-23 7:17
mveCPallini8-Jan-23 7:17 
AnswerRe: What are some stupid-useful coding tricks you rely on? Pin
raddevus8-Jan-23 7:31
mvaraddevus8-Jan-23 7:31 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
CPallini8-Jan-23 7:48
mveCPallini8-Jan-23 7:48 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
honey the codewitch8-Jan-23 14:55
mvahoney the codewitch8-Jan-23 14:55 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
Mircea Neacsu8-Jan-23 10:27
Mircea Neacsu8-Jan-23 10:27 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
raddevus8-Jan-23 11:08
mvaraddevus8-Jan-23 11:08 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
Mircea Neacsu8-Jan-23 11:28
Mircea Neacsu8-Jan-23 11:28 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
raddevus8-Jan-23 11:40
mvaraddevus8-Jan-23 11:40 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
Marcelo Huerta9-Jan-23 6:16
Marcelo Huerta9-Jan-23 6:16 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
jmaida8-Jan-23 15:54
jmaida8-Jan-23 15:54 
GeneralRe: What are some stupid-useful coding tricks you rely on? Pin
BillWoodruff8-Jan-23 20:33
professionalBillWoodruff8-Jan-23 20:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.