Help with Button

I am trying to start a loop with the press of a button, I am using the attached code. As soon as I run the rest of the loop begins, regardless of the button press. This is not the case with simple test code below, it works as I had hoped. I can't quite see what I am missing!

// Define
int led = 13;
int led2 = 12;
int led3 = 11;
int motorPin = 2;
int motorPin2 = 3;
int motorPin3 = 4;
int buttonPin = 5;     // the number of the pushbutton pin

// the setup routine runs once when you press reset:
void setup() {
 pinMode(led, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(motorPin, OUTPUT);  // setup Agitation Pump
 pinMode(motorPin2, OUTPUT);  // setup Dev IN Pump
 pinMode(motorPin3, OUTPUT);  // setup Dev OUT Pump
 pinMode(buttonPin, INPUT); // setup Button
}


void loop() {
 
 while (digitalRead(buttonPin) == HIGH) {

 delay(3000);
   
 //DEVELOPER
 digitalWrite(led, HIGH);   // Dev LED on
 
 analogWrite(motorPin2, 200);  // Dev FILL pump on for 3 seconds
 delay(3000);
 analogWrite(motorPin2, 0);    // Dev FILL pump off
 
 delay(3000); //Start Process for 3 seconds
 
 analogWrite(motorPin, 200);  // Agitate for 3 seconds
 delay(3000);
 analogWrite(motorPin, 0);    // Agitate stop
 
 delay(3000); //Continue process for 3 seconds
 
 analogWrite(motorPin3, 200);  // Dev DRAIN pump for 3 seconds
 delay(3000);
 analogWrite(motorPin3, 0);    // Dev DRAIN pump stop
 
 digitalWrite(led, LOW);  //Dev LED off
 
 }
 
}

My proof of concept seems to be working with this code:

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin


void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);
}

void loop() {

 while (digitalRead(buttonPin) == HIGH) {
 

  delay (3000);
   digitalWrite(ledPin, HIGH);  


   }
 
}

Code tags. Go read the "How to Use This Forum" post at the top of the board. The tendency to read the directions first is what separates the good coder from the crappy one.

How is your switch wired? Do you have a pull-down resistor?

It is much more common to wire the button so that it reads LOW when pressed so you can use the builtin pull-up resistor.

But if you don't have the pin pulled one way or the other then it floats when your button isn't pressed and is just as likely to read HIGH as LOW.

Working:

Not Working:

Why do you have two transistors that servo no purpose other than to make a short circuit?

Pins 3 and 4 default to LOW when the board comes on and you set them to OUTPUT. Try writing them HIGH before you make them outputs so they're turned off and you don't have the short. Maybe the short circuit is causing the board to reset.

I have a really hard time understanding the wiring of a switch with external resistors. I have to study the pictures forever.

Using the internal pullup resistor, with one leg connected to the digital pin and one leg connected to ground, the schematic is intuitive.

Why do you want to add unnecessary hardware?

I can't really figure out why you rotated the board between the two examples, and why the switch in the non-working picture doesn't have all 4 pins in holes in the bread-board.

I can't figure out why you clipped the wires off in the second picture. Didn't you think we'd want to see the whole picture?

Here is the entire circuit, I though I was using the transistor correctly for my motors.

Here is the entire circuit

What's that green wire on the top, left for?

It is going to the ground at the top of the breadboard.

jeffreyl:
It is going to the ground at the top of the breadboard.

But you cut the picture off again?

jeffreyl:
Here is the entire circuit, I though I was using the transistor correctly for my motors.

You are. But it was impossible to see that in the other pic.

Are the motors running when all this is happening? You're powering them from the 5V pin on the Arduino. That's probably not a good idea. If they pull too much current then the voltage sags and the board resets. The motors should have their own power supply.

I am currently just testing in circuits.io, would that sim reproduce the voltage sag/reset?

jeffreyl:
I am currently just testing in circuits.io, would that sim reproduce the voltage sag/reset?

Oh, I thought you were talking about a real circuit. Those simulators are often crap. I cannot attest to why it wouldn't work in some simulator program. Build a circuit and test before you decide you have a problem.