Two Button Simultaneous Press Input

Two button simultaneous press input

Since UIButton is a subclass of UIControl, it inherits the touchInside property of UIControl. Furthermore, when UIButton sends the touch-up-inside action, it still responds to touchInside with YES. So you can just hook both buttons up to this action:

- (IBAction)buttonWasTouched:(id)sender {
if (self.button1.touchInside && self.button2.touchInside) {
[self launchNukes];
}
}

By default, Interface Builder hooks up the touch-up-inside event when you control-drag. If you would rather launch the nukes the moment the second button is simultaneously touched, hook up the touch-down events. You do this by control-clicking the buttons instead of control-dragging them.

Python PyGame press two buttons at the same time

The keyboard events (e.g. pygame.KEYDOWN) occure only once when a button is pressed.

Use pygame.key.get_pressed() to continuously evaluate the states of the buttons. e.g.:

finish = False
while not finish:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True

keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
print "A"
if keys[pygame.K_RIGHT]:
print "B"
if keys[pygame.K_LEFT]:
print "C"

Or if you would rather get a list:

finish = False
while not finish:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True

keys = pygame.key.get_pressed()
if any(keys):
kmap = {pygame.K_UP : "A", pygame.K_RIGHT : "B", pygame.K_LEFT : "C"}
sl = [kmap[key] for key in kmap if keys[key]]
print sl

Simultaneous Button Press

What i understand is you need to take some action when both the buttons are pressed. Watever you try, there is going to be a lag between the touches of these buttons. A better approach would be to check if press on both buttons is finished. Hope following works for you -

    @property(nonatomic, assign) BOOL firstButtonPressed;

@property(nonatomic, assign) BOOL secondButtonPressed;

//in init or viewDidLoad or any view delegates
_firstButtonPressed = NO;
_secondButtonPressed = NO;

//Connect following IBActions to Touch Down events of both buttons
- (IBAction)firstButtonPressed:(UIButton *)sender {
_firstButtonPressed = YES;
[self checkForButtonPress];
}

- (IBAction)secondButtonPressed:(UIButton *)sender {
_ secondButtonPressed = YES;
[self checkForButtonPress];
}

- (void)checkForButtonPress {
if (_firstButtonPressed && _secondButtonPressed) {
NSlog(@"Two buttons pressed");
}
}

How to detect two or more button press (GPIO) at the same time by a microprocessor/microcontroller?

One single ISR triggered is not sufficient to detect a single button press. Because of the electro-mechanical signal bounce you get from all buttons, you need some kind of de-bouncing algorithm.

Furthermore, you need the program to be immune to EMI so that multiple interrupts won't create havoc on the stack whenever there are lots of pulses coming from the button(s).

For example:

  1. If the buttons are connected to different ports that give different interrupts, create one interrupt per button. If they are connected to the same port, they can usually trigger the same interrupt (depending on MCU).

  2. Whenever you get an interrupt as result of any signal edge (raising or falling) from the button, then in the ISR disable the interrupt and start a hardware timer of typically 5-10ms depending on the button. The timer should preferably trigger a timer interrupt.

    Disabling the interrupt is necessary to filter out spurious interrupts caused by the bouncing and by potential EMI glitches.

    The timer is necessary for the de-bouncing. If you cannot find the exact signal bounce time in the button data sheet (you most often don't), then simply measure it using an oscilloscope.

  3. When the timer elapses, read the port and store the result in a variable. Enable the button interrupt once again.

  4. The variable needs to be declared at file scope as static volatile. Static for private encapsulation, which is needed for good program design. Volatile to prevent against common compiler optimizer bugs, where the compiler doesn't realize that a variable has been modified by an ISR.

  5. Implement the same for the first button. You'll have two different variables telling the current state of the buttons. Simply compare these two variables with each other to tell whether or not two buttons are pressed at the same time.

multiple button press management in arduino

Try combine all buttons state into one variable, like:

int allBtnStates;
unsigned long btnTimeStamp = 0;
void loop() {
A = digitalRead(ButtonA);
B = digitalRead(ButtonB);
allBtnStates = A + 2*B;
if(allBtnStates < 3){ //Any button pressed
if(btnTimeStamp == 0) btnTimeStamp = millis(); //Create timestamp
else if(millis() - btnTimeStamp > 150){
switch(allBtnStates){
case 2: doA(); break; //Only A pressed
case 1: doB(); break; //Only B pressed
case 0: doAB(); break; //Both A and B pressed
}
btnTimeStamp = 0; //Reset timestamp
}
}
//Monitor other input if needed
}

If you have button C, then change allBtnStates = A + 2*B; to allBtnStates = A + 2*B + 4*C; and work out all the conditions accordingly. Hope that helps!



Related Topics



Leave a reply



Submit