Push-Button Problem With Trinket

Hello wonderful forum people. I'm in the midst of building the Adafruit Trinket "keyboard" shown here: Wiring | Trinket USB Keyboard | Adafruit Learning System

I've adapted the code given in the example so that it only handles the first button, which is wired from pin 0 to ground on the Trinket with nothing in between, and it produces only a single space with each press. The goal here is to create a simple switch interface for people with severe physical disabilities to control media and adaptive programs. However, I've run into an issue in my code or hardware - not sure which - where I will occasionally produce a second space upon releasing the switch.

I've tried to write the code so that this doesn't happen. I only allow a certain number of spaces per second and only produce a space if the switch reads "low" for a certain amount of time. I also tried using a different button or simply touching the wires together. Despite all this, the issue still persists. Anyone have any idea how to fix this? Help would be much appreciated :o

Here's my code:

    #include <TrinketKeyboard.h>
    #define PIN_BUTTON_SPACE 0
    boolean depressed = false; 
    long count3 = 30;
    long count2 = 1000;
    unsigned long time;
    long time2 = 0;
    boolean antidepressed = true;
    boolean antidepressed2 = true;
    void setup()
    
    {
      pinMode(PIN_BUTTON_SPACE, INPUT);
      // setting input pins to high means turning on internal pull-up resistors
      digitalWrite(PIN_BUTTON_SPACE, HIGH);     
      // remember, the buttons are active-low, they read LOW when they are not pressed
      // start USB stuff
      time = millis();
      TrinketKeyboard.begin();
    }
    
    void loop()
    {
      TrinketKeyboard.poll();
      unsigned long now = millis();
      unsigned long dif = now - time;
      time = now;
      count2 = count2 + dif;
  if (count2 > 1000) {
        if (digitalRead(PIN_BUTTON_SPACE) == LOW) {
            time2 = time2 + 1;
            if (time2 > 200) {
            if (depressed == false) {
              TrinketKeyboard.pressKey(0, KEYCODE_SPACE);
              TrinketKeyboard.pressKey(0, 0);               
              depressed = true;  
              antidepressed = false;
              antidepressed2 = false;
              time2 = 0;
            }
            else
              if (antidepressed2 == false) {
              //TrinketKeyboard.pressKey(0, KEYCODE_E);
              TrinketKeyboard.pressKey(0, 0);
              antidepressed2 = true;
              }
            }
          }
        else if (digitalRead(PIN_BUTTON_SPACE) == HIGH) {
          if (antidepressed == false) {
          depressed = false;
          antidepressed = true;             
          TrinketKeyboard.pressKey(0, 0);
          }
          }   
        }                                  
    }

You're probably encountering button "bouncing". You can do "debouncing" either in hardware or software. It would be worth it for you to google different debouncing techniques.

I think I would suggest you refactor your logic in many ways.

Maybe a subroutine for reading the button that also handles denouncing.
Another subroutine that sends a space but no more than once per second.

Now you could focus on getting loop to not repeat a held button, etc.

I was unable to easily follow your depressed and antidepressed flags but noticed antidepressed was always !depressed. Why not just use !depressed?

One debounce technique is to just wait 10ms or so before reporting the button state to allow any bouncing time to stop and settle on something final. You could do this but remember to call poll while you are waiting.