Electronic – One action per switch press with microcontroller

microcontrollerswitches

I've been trying to find out a way for quite a while now on how to have a momentary switch only latch for a short time, and then unlatch. I can't seem to wrap my head around what to do. If this helps, I was going to work on a laser tag gun, so when the trigger is pressed, only a short burst happens once, not continually. Thanks in advance, sorry if this has been posted about, I simply can't find any resources about it.

Best Answer

Here's some simple pseudo code that is not interrupt based and is not debounced.

void main( void ) {
    uint8_t last_state = 0;
    uint8_t btn_state = 0;

    for(;;) {

        btn_state = read_button();

        if( btn_state && !last_state ) {
            emit_pulse();
        }

        last_state = btn_state;
    }
}
Related Topic