Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 9 months ago.
Continuous Sampling of multiple buttons to debounce
Using this library for the PinDetect functionality, could you please advise what the easiest way to make these routines monitor multiple inputs would be? I don't really want to copy the full code n number of times with different variables? If it helps, I am only using the pin released and pin held released functions.
Thanks.
Question relating to:
1 Answer
9 years, 9 months ago.
PinDetect works on individual channels, so I don't see any way of batch-configuring them. Not without writing even more code, anyway.
Here's my working code for a handful of inputs, anyway, in case it is useful.
// define the PinDetect Channels PinDetect countIn (P0_4, PullUp); // PinDetect prnCmd ( P2_12, PullUp ); PinDetect pb2 ( P2_11, PullUp ); // PinDetect pb3 ( P1_26, PullUp ); // PinDetect pb4 ( P1_25, PullUp ); // PinDetect pb5 ( P3_25, PullUp ); // PinDetect pb6 ( P3_26, PullUp ); //
Each input does something unique, so we must set them up individually:
// Configure PinDetect channels for their actions: void inputsInit(void) { countIn.attach_asserted( &countInPressed ); countIn.attach_deasserted( &countInReleased ); pb2.attach_asserted( &holdPressed ); pb2.attach_deasserted( &holdReleased ); pb2.attach_asserted_held( &holdPressedHeld ); pb3.attach_asserted( &trip1Pressed ); pb3.attach_deasserted( &trip1Released ); pb3.attach_asserted_held( &trip1PressedHeld ); pb4.attach_asserted( &trip2Pressed ); pb4.attach_deasserted( &trip2Released ); pb4.attach_asserted_held( &trip2PressedHeld ); pb5.attach_asserted( &startPressed ); pb5.attach_deasserted( &startReleased ); pb5.attach_asserted_held( &startPressedHeld ); pb6.attach_asserted( &menuPressed ); pb6.attach_deasserted( &menuReleased ); pb6.attach_asserted_held( &menuPressedHeld ); prnCmd.attach_asserted( &footSwitch ); prnCmd.attach_deasserted( &startReleased ); countIn.setSampleFrequency(2300); // 2300us = 2,3ms. 10K = 1ms. Defaults to 20ms. countIn.setAssertValue(0); pb2.setSampleFrequency(); // Defaults to 20ms. pb2.setAssertValue(0); pb3.setSampleFrequency(); // Defaults to 20ms. pb3.setAssertValue(0); pb4.setSampleFrequency(); // Defaults to 20ms. pb4.setAssertValue(0); pb5.setSampleFrequency(); // Defaults to 20ms. pb5.setAssertValue(0); pb6.setSampleFrequency(); // Defaults to 20ms. pb6.setAssertValue(0); pb6.setSamplesTillHeld(21); //default is 50 prnCmd.setSampleFrequency(); // Defaults to 20ms. prnCmd.setAssertValue(0); printf("PinDetect channels initialised\r\n"); }