Program randomly triggering instead of turning on with button

Hello!
My program randomly triggers. It’s supposed to start with a button press but pressing it does nothing.

//Death Trooper Grenade Program
//Written by Brandon Owens
//brandonlouisowens@gmail.com
//June 2017
//Version 1

const int BUTTON = 3;
const int LEDS = 8;
const int BUZZER = 10;
const int FREQUENCY_HIGH = 4500; //Range is 31	to 65535
const int FREQUENCY_LOW = 175; //Range is 31	to 65535
const int CYCLES = 7;

int blink_delay = 600;
int blinks = 1;
int button_state = 0;

void setup()
{
  pinMode(LEDS, OUTPUT);
  pinMode(BUTTON, INPUT);
}

void loop() 
{
  button_state = digitalRead(BUTTON);
  if (button_state == HIGH)
    {
      //Outer for loop - Set how many blinks and for how long
      int i;
      for (i = 0; i < CYCLES; i += 1) 
        {
          //Inner for loop - Blink light and make noise
          int i;
          for (i = 0; i < blinks; i += 1) 
          {
            //Light up and make sound
            digitalWrite(LEDS,HIGH);
            tone(BUZZER,FREQUENCY_HIGH);
            delay(blink_delay);
            //Turn off and shut up
            digitalWrite(LEDS,LOW);
            noTone(BUZZER);
            delay(blink_delay);
          }
          //Adjust vars for next round
          blinks += 2;
          blink_delay = blink_delay * 66.0 / 100;
        }
      //Reset vars for second push
      blinks = 1;
      blink_delay = 600;
      //Light up and make sound
      delay(400);
      digitalWrite(LEDS,HIGH);
      tone(BUZZER,FREQUENCY_LOW);
      //Turn off and shut up
      delay(1250);
      digitalWrite(LEDS,LOW);
      noTone(BUZZER);
    }
}

Did you mean pinMode(BUTTON, INPUT_PULLUP);?
Also change if (button_state == HIGH) to   if (button_state == LOW) if you're using a pull-up resistor.

Pieter

Hello - indeed Explain your wiring

Hi,
How have you got your button wired?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
How have you got your button wired?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

J-M-L:
Hello - indeed Explain your wiring

Does this help? https://i.imgur.com/mHbZ8OR.png

It was working on a breadboard but when I soldered it together it doesn't work correctly.

You have a floating input. Read this. Gammon Forum : Electronics : Microprocessors : Switches tutorial

Delta_G:
You have a floating input. Read this. Gammon Forum : Electronics : Microprocessors : Switches tutorial

Thank you for your reply, is this better?

Not even close. It would probably be easier to use the internal pullups on the arduino and connect the button to be active low.

Delta_G:
Not even close. It would probably be easier to use the internal pullups on the arduino and connect the button to be active low.

Oh, well thank you. I'm attempting to use the built in ones at the moment.