Diffulty getting push button to work

Apologies up front, I am pretty new to Arduino programming.

I have an Adafruit Trinket which I have managed to get working as a USB volume control. However, I wanted to add buttons on to the remaining pins 4 and 5 for track changing etc.

I added a little extra code to initialise the buttons, as pull ups, and used state change detection to prevent constant activations. However, despite this, the button is being constantly activated despite me doing nothing. Also, when I do ground the pin (press the button) and trinket starts misbehaving and windows throws up a usb malfunction warning.

I'm sure it's something simple, or I've managed to fry my chip (although the volume control and rotary encoder switch still work fine.

Any help would be greatly appreciated.

#include "TrinketHidCombo.h"
 
#define PIN_ENCODER_A 2
#define PIN_ENCODER_B 0
#define TRINKET_PINx  PINB
#define PIN_ENCODER_SWITCH 1
#define BUTTON_1 3
 
static uint8_t enc_prev_pos   = 0;
static uint8_t enc_flags      = 0;
static char    sw_was_pressed = 0;
int button1State = 0;
int lastButton1State = 0;
 
void setup()
{
  // set pins as input with internal pull-up resistors enabled
  pinMode(PIN_ENCODER_A, INPUT);
  pinMode(PIN_ENCODER_B, INPUT);
  digitalWrite(PIN_ENCODER_A, HIGH);
  digitalWrite(PIN_ENCODER_B, HIGH);
  pinMode(BUTTON_1, INPUT);
  digitalWrite(BUTTON_1, HIGH);
 
 
  pinMode(PIN_ENCODER_SWITCH, INPUT);
  // the switch is active-high, not active-low
  // since it shares the pin with Trinket's built-in LED
  // the LED acts as a pull-down resistor
  digitalWrite(PIN_ENCODER_SWITCH, LOW);

  TrinketHidCombo.begin(); // start the USB device engine and enumerate
 
  // get an initial reading on the encoder pins
  if (digitalRead(PIN_ENCODER_A) == LOW) {
    enc_prev_pos |= (1 << 0);
  }
  if (digitalRead(PIN_ENCODER_B) == LOW) {
    enc_prev_pos |= (1 << 1);
  }
}
 
void loop()
{
  int8_t enc_action = 0; // 1 or -1 if moved, sign is direction
 
  // note: for better performance, the code will now use
  // direct port access techniques
  // http://www.arduino.cc/en/Reference/PortManipulation
  uint8_t enc_cur_pos = 0;
  // read in the encoder state first
  if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_A)) {
    enc_cur_pos |= (1 << 0);
  }
  if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_B)) {
    enc_cur_pos |= (1 << 1);
  }
 
  // if any rotation at all
  if (enc_cur_pos != enc_prev_pos)
  {
    if (enc_prev_pos == 0x00)
    {
      // this is the first edge
      if (enc_cur_pos == 0x01) {
        enc_flags |= (1 << 0);
      }
      else if (enc_cur_pos == 0x02) {
        enc_flags |= (1 << 1);
      }
    }
 
    if (enc_cur_pos == 0x03)
    {
      // this is when the encoder is in the middle of a "step"
      enc_flags |= (1 << 4);
    }
    else if (enc_cur_pos == 0x00)
    {
      // this is the final edge
      if (enc_prev_pos == 0x02) {
        enc_flags |= (1 << 2);
      }
      else if (enc_prev_pos == 0x01) {
        enc_flags |= (1 << 3);
      }
 
      // check the first and last edge
      // or maybe one edge is missing, if missing then require the middle state
      // this will reject bounces and false movements
      if (bit_is_set(enc_flags, 0) && (bit_is_set(enc_flags, 2) || bit_is_set(enc_flags, 4))) {
        enc_action = 1;
      }
      else if (bit_is_set(enc_flags, 2) && (bit_is_set(enc_flags, 0) || bit_is_set(enc_flags, 4))) {
        enc_action = 1;
      }
      else if (bit_is_set(enc_flags, 1) && (bit_is_set(enc_flags, 3) || bit_is_set(enc_flags, 4))) {
        enc_action = -1;
      }
      else if (bit_is_set(enc_flags, 3) && (bit_is_set(enc_flags, 1) || bit_is_set(enc_flags, 4))) {
        enc_action = -1;
      }
 
      enc_flags = 0; // reset for next time
    }
  }
 
  enc_prev_pos = enc_cur_pos;
 
  if (enc_action > 0) {
    TrinketHidCombo.pressMultimediaKey(MMKEY_VOL_UP);
  }
  else if (enc_action < 0) {
    TrinketHidCombo.pressMultimediaKey(MMKEY_VOL_DOWN);
  }

  // Rotary Encoder Switch
  // remember that the switch is active-high
  if (bit_is_set(TRINKET_PINx, PIN_ENCODER_SWITCH)) 
  {
    if (sw_was_pressed == 0) // only on initial press, so the keystroke is not repeated while the button is held down
    {
      TrinketHidCombo.pressMultimediaKey(MMKEY_PLAYPAUSE);
      delay(5); // debounce delay
    }
    sw_was_pressed = 1;
  }
  else
  {
    if (sw_was_pressed != 0) {
      delay(5); // debounce delay
    }
    sw_was_pressed = 0;
  }

// Button 1

button1State = digitalRead(BUTTON_1);
if (button1State != lastButton1State) {
  if (button1State == LOW) {
    TrinketHidCombo.pressMultimediaKey(MMKEY_MUTE);
  }
  delay (50);
}
lastButton1State = button1State;

  TrinketHidCombo.poll(); // check if USB needs anything done
}

Draw out a schematic and show how you have the switches, resistors and power connected to the Arduino. Also the values of the resistors.

Paul

An good image of the wiring is always nice.

I have no resistors on the part in question, just a straight push switch between GND and PIN #3

Now I hope this works.

That does make sense as to why it is complaining when I am applying voltages/grounding these pins. Looks like adding more switches isn't possible with this board then.

I did try looking this up, but it wasn't completely clear, well, to a complete newbie anyway.

Thank you for your advice.